I'm sure this question is a duplicate but I cannot find it; I think it may only be duplicated on StackOverflow (I know one duplicate is there).
Basic idea
A key statement in your question is:
I want to drop 3,4,7,9 rows and columns in a single time, then how to do it quickly?
I believe this calls for Part
which can extract (and by inverse, drop) rows and columns at the same time. You must extract the parts you don't want to drop so use Complement
.
m = Partition[Range@100, 10];
ranges = Complement[Range@#, {3, 4, 7, 9}] & /@ Dimensions[m]
new = m[[##]] & @@ ranges;
new // MatrixForm

As a self-contained function
This is written to handle arrays of arbitrary depth.
drop[m_, parts__List] /; Length@{parts} <= ArrayDepth[m] :=
m[[##]] & @@ MapThread[Complement, {Range @ Dimensions[m, Length @ {parts}], {parts}}]
drop[m, {3, 4, 7, 9}, {4, 5, 6}] // MatrixForm

Timings
user asked how to test the performance of the solutions presented. Here is a very basic series of tests that one might start with. First a custom timing function based on code from Timo:
SetAttributes[timeAvg, HoldFirst]
timeAvg[func_] := Do[If[# > 0.3, Return[#/5^i]] & @@ Timing@Do[func, {5^i}], {i, 0, 15}]
Then the functions to test:
dropCarlos[m_, rows_, cols_] /; ArrayDepth[m] > 1 :=
Delete[Delete[m, List /@ rows]\[Transpose], List /@ cols]\[Transpose]
(* copy drop from above *)
Then a test function with three parameters. (The parameters are the number of randomly selected rows and columns to drop and dimensions of the array.)
test[n_, r_, c_] :=
With[{
a = RandomReal[9, {r, c}],
rows = RandomSample[Range@r, n],
cols = RandomSample[Range@c, n]
},
{#, timeAvg @ #[a, rows, cols]} & /@ {dropCarlos, drop}
] // TableForm
We can now probe a few shapes and sizes of data. (Another option would be plotting these tests but that's best left to a separate question if there is interest.)
test[5, 1000, 1000] (* delete only 5 rows and columns from a 1000x1000 array *)
dropCarlos 0.006608
drop 0.001248
test[900, 1000, 1000] (* delete all but 100 rows and columns from a 1000x1000 array *)
dropCarlos 0.00047904
drop 0.00017472
test[50, 10000, 100] (* delete 50 from a very tall array *)
dropCarlos 0.005368
drop 0.001072
test[50, 100, 10000] (* delete 50 from a very wide array *)
dropCarlos 0.002496
drop 0.0008992
These tests cover a variety of cases to see if the relative performance of the functions under test change significantly; if they do a method may have hidden strengths or weaknesses.
A final test that must be included in the suite is using non-packed data (all the test above were with packed arrays). This is because there are often significant internal optimizations for packed arrays that cannot be used on unpacked data. Because of this a function that is fast on packed arrays might become suddenly slow elsewhere. For this test replace the expression RandomReal[9, {r, c}]
with RandomChoice["a" ~CharacterRange~ "z", {r, c}]
in the definition of test
. This creates an array of strings which cannot be packed. Now run another test
:
test[250, 1000, 1000] (* this time with an unpacked String array *)
dropCarlos 0.009232
drop 0.00424
Notice that while my function is still faster it is not as much faster; this is because Part
is particularly well optimized for packed arrays.
matrix[[All, Complement[Range[10], {3, 4, 7, 9}]]]
$\endgroup$