8

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?

pb2q
  • 58,613
  • 19
  • 146
  • 147
Phil
  • 815
  • 1
  • 8
  • 15

1 Answers1

14

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
Dr. belisarius
  • 60,527
  • 15
  • 115
  • 190
  • 7
    This looks about right for dropping a row and a column (which I think is what was requested). To drop just a col, do Drop[mat,None,{colnum}]. – Daniel Lichtblau Mar 13 '11 at 15:04
  • @Daniel Thanks. Re-reading the last part of the question I wonder if your comment is what the OP is after. Not sure, though. – Dr. belisarius Mar 13 '11 at 22:39
  • @belisarius, @daniel, many thanks. because of character limitations, I am making a new question to expand on the question I asked here – Phil Mar 14 '11 at 13:48
  • 1
    Check: http://stackoverflow.com/questions/5299798/efficient-way-to-pick-delete-a-list-of-rows-columns-in-a-matrix-in-mathematica – Phil Mar 14 '11 at 14:29
  • On the other hand, if I want to delete **non-consecutive** columns from a matrix this may be done reasonably intuitively using `Transpose` & `Delete`. If, say, I have a {10x10} matrix and I want to delete columns 2, 4 & 8,one possibility is: `mydata = Array[Subscript[a, ##] &, {10, 10}]; Transpose@Delete[Transpose@mydata, {{2}, {4}, {8}}]`. It can also be done using Drop,for example, `Drop[Drop[Drop[mydata, None, {2}], None, {3}], None, {6}]`, but this is awkward. Is there a better way to drop non-consecutive columns from a matrix? – 681234 Mar 14 '11 at 16:14
  • 1
    check [here](http://stackoverflow.com/questions/5299798/efficient-way-to-pick-delete-a-list-of-rows-columns-in-a-matrix-in-mathematica/5300892#5300892) for a very nice method by wreach – 681234 Mar 14 '11 at 18:09