问题
I am trying to delete both a matrix in mathematica. An inelegant way of doing it is as I do below, i.e specifying it in a new matrix as
S = Table[
Ss[[If[i < t, i, i + 1]]][[If[j < t, j, j + 1]]], {i, q}, {j, q}];
where the goal is to eliminate row and column t.
Indeed delete a line is easy Delete[Ss,t]. For the column column I suppose I could do
Transpose[Delete[Transpose[Ss,t]]]
My primary concern is to do it in a way that executes the fastest way possible.
More generally, is there a Mathematica operator that makes it as easy to slice and dice matrix columns as it is to do for rows without resorting to transpose?
回答1:
I think you are looking for:
Drop[Ss,{t},{t}]
Timings:
ClearAll["Global`*"];
First@Timing[a = RandomInteger[1000, {5000, 5000}];]
0.34
First@Timing[Drop[a, {2}, {2}]]
0.11
While
First@Timing[Transpose@Delete[Transpose@Delete[a, 2], 2]]
0.5
来源:https://stackoverflow.com/questions/5289350/simple-way-to-delete-a-matrix-column-in-mathematica