Delete
is not suitable to delete the rows of a matrix, as illustrated below:
SeedRandom[0];
; r = 100
; rows = RandomReal[{0, 1}, {r, 2}]
; toDrop = Select[Range[r], PrimeQ]
; reducedRows = Delete[rows, toDrop]
I know that I can always do something like
toKeep = Complement[Range[r], toDrop]
; reducedRows = rows[[toKeep]]
...but this strikes me as inefficient, at least whenever toKeep
is large (IOW, whenever r
is much greater than Length[toDrop]
).
Is there any other built-in that achieves what Delete[rows, toDrop]
aspires to?
toDrop = Transpose[{Select[Range[r], PrimeQ]}]
$\endgroup$Delete[rows, List /@ toDrop]
? $\endgroup$Complement
I feel that this problem is already described in other questions: (17002), (20228), (43785), (108336). Note that the answers below largely duplicate methods already given in answer to these. I shall probably mark this as a duplicate unless someone makes a compelling argument against that. $\endgroup$