Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

I start off with m = 1000 x 5 matrix, and I would like to remove first column to get 1000 x 4 matrix and repeat again for 1000 x 3 and so on. Is there an efficient way to do this? I see Insert to add columns or rows but don't see command for removing? I see maybe use the extract but is this only for a single vector extraction?

share|improve this question
    
Have you tried Most? –  chris Dec 27 '12 at 16:31
1  
Seems like Drop works too: (tmp = {{11, 12, 13}, {21, 22, 23}, {31, 32, 33}}) // MatrixForm and then (Drop[tmp, None, 1]) // MatrixForm –  sebastian c. Dec 27 '12 at 17:29
add comment

3 Answers 3

I am not sure this deserves a full blown answer but...

 a = RandomVariate[NormalDistribution[], {15, 5}];

 Rest /@ a // Dimensions


 (* {15,4} *)

And to operate recursively

 Dimensions /@ NestList[Rest /@ # &, a, 3]

 (* 
  15    5
  15    4
  15    3
  15    2
  *)

EDIT

Replaced Most by Rest since as noticed by Mr Wizard this was actually the OP question!

share|improve this answer
    
In truth, this is one of those canonical questions that has several approaches. –  rcollyer Dec 27 '12 at 16:48
    
Why not continue the process? NestList[Most /@ # &, a, 5] Then look at Dimensions /@ NestList[Most /@ # &, a, 5], whose output is {{15, 5}, {15, 4}, {15, 3}, {15, 2}, {15, 1}, {15, 0}}, as expected. But look at: MatrixForm /@ NestList[Most /@ # &, a, 5]. I find the final entry in the output of that surprising: I would expect to see just the big 15-row brackets with nothing inside them; instead, I see an empty list {} in each of the 15 rows. –  murray Dec 27 '12 at 17:12
add comment

As has been shown there are a number of ways to do this. To summarize:

m = RandomInteger[9, {6, 4}]

Mathematica graphics

All of these:

Drop[m, 0, 1]

Rest /@ m

m[[All, 2 ;;]]

{##2} & @@@ m

Produce:

Mathematica graphics

Each has a place. For the specific operation Rest is especially clear. Drop can easily drop columns besides the first, e.g. Drop[m, 0, {3}], and it is very fast. Part is also usually very fast, and allows assignments which is both flexible and efficient (when applicable). SlotSequence is simply fun and can be quite useful when you also want to do something with the elements.

Timings with larger matrix:

m = RandomInteger[9, {15000, 100}];

Drop[m, 0, 1]  // timeAvg
Rest /@ m      // timeAvg
m[[All, 2 ;;]] // timeAvg
{##2} & @@@ m  // timeAvg

0.0010224

0.004496

0.0011728

0.03992

(The timeAvg function has been repeatedly posted before. Use Search.)

share|improve this answer
add comment

Here is a way to do it with Table:

mat = RandomReal[{0, 1}, {3, 5}]
Table[mat = mat[[All, 2 ;; -1]], {Dimensions[mat][[2]] - 1}];

MatrixForm /@ %

Mathematica graphics

share|improve this answer
    
+1 for an efficient mutable method. Of note is that one can use the slightly shorter mat[[All, 2 ;;]]. –  Mr.Wizard Dec 28 '12 at 14:18
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.