`Part` like `Delete`: How to delete list of columns or arbitrarily deeper levels
You can actually use Part
for that:
ClearAll[delete];
delete[expr_, specs___] :=
Module[{copy = expr},
copy[[specs]] = Sequence[];
copy
];
So that for example
delete[m, All, All, 2]
(*
{
{{1, 3}, {4, 6}, {7, 9}, {10, 12}},
{{13, 15}, {16, 18}, {19, 21}, {22, 24}},
{{25, 27}, {28, 30}, {31, 33}, {34, 36}},
{{37, 39}, {40, 42}, {43, 45}, {46, 48}}
}
*)
Note that this is not exactly equivalent to Delete
in all cases, since sequence splicing is an evaluation-time effect, so the results will be different if you delete inside held expressions - in which case the method I suggested may not work.
Here is a version that would probably be free of the mentioned flaw, but will be slower:
ClearAll[deleteAlt];
deleteAlt[expr_, specs___] :=
Module[{copy = Hold[expr], tag},
copy[[1, specs]] = tag;
ReleaseHold@Delete[copy, Position[copy, tag]]
];
You can test both on say, Hold[Evaluate[m]]
, with the spec 1, All, All, 2
, to see the difference.
You may use ReplacePart
and Nothing
with Blank
(_
) for All
.
Delete second entry of all submatrices
ReplacePart[m, {_, _, 2} -> Nothing] // MatrixForm
Delete last column
ReplacePart[m, {_, -1} -> Nothing] // MatrixForm
and so on.
Hope this helps.