On trying to write this answer I reached the frustrating realization that I didn't have an efficient way to delete a list of columns or deeper level components in a simple way as Part
gives.
Given
MatrixForm[m = Partition[Partition[Range[4 4 3], 3], 4]]
I can Delete
rows {2,3}
by
Delete[m, List /@ {2, 3}] // MatrixForm
But to delete the columns or deeper levels I would need to Transpose
twice. For instance using something like this
rDelete[m_, row_, col_] := Delete[
Transpose[
Delete[
Transpose[m]
, List /@ col
]
], List /@ row
]
On the other hand to get a Part
at any level I can easily use
Part[m, All, {1, 4}, {2, 3}] // MatrixForm
Unfortunately, All
and Span
are not available for Delete
.
Question:
How can we delete columns or whole higher levels elegantly and efficiently, as we do with Part
?
m
:Drop[m, None, {2}, None]
.Drop[m, None, None, {2}]
gives the complement of yourPart[]
example. You can judiciously combine this withMap[]
/MapAt[]
if you need to do several positions, since unlikeDelete[]
,Drop[]
can only remove from either a single dimension or a range of dimensions. $\endgroup$Drop
is the fact that you need toMap
over the list of indexes to delete, as you say. $\endgroup$