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.

This should be an easy question but I only found a partial solution using All.

Say I have a matrix A = 1000 x 5, how to get only a part of that matrix, say B = n x 5, where n can be any positive integer that's less than 1000 or the original matrix A row?

share|improve this question
3  
Please have a good long look at Part (preferrably in conjunction with Span). –  Yves Klett Nov 11 '12 at 19:14
7  
Could you please consider accepting some answers by clicking the checkmark sign` ? –  chris Nov 11 '12 at 22:53
1  
You might find this Q/A useful: How to extract and replace submatrix.... –  István Zachar Nov 12 '12 at 11:03

2 Answers 2

I guess this should work, if I need the second and third rows of a matrix :

{{a, b, c}, {d, e, f}, {g, h, i}}[[2 ;; 3]]
share|improve this answer

You can make good use of Span in multiple dimensions:

m = Table[{y, x}, {y, 1, 5}, {x, 1, 10}];

m[[2 ;; 3, 5 ;; 6]]

{{{2, 5}, {2, 6}}, {{3, 5}, {3, 6}}}

(verbatim @J.M.)...Take[] works nicely as well:

Take[m, {2, 3}, {5, 6}]

{{{2, 5}, {2, 6}}, {{3, 5}, {3, 6}}}

share|improve this answer
1  
Take[] works nicely as well: Take[m, {2, 3}, {5, 6}] –  J. M. Nov 12 '12 at 9:27
    
@J.M. another answer? Strangely, I almost never use Take in my code... so much for cerebral plasticity. –  Yves Klett Nov 12 '12 at 13:50
    
I'd prefer that you just append it to your answer... :) –  J. M. Nov 12 '12 at 14:03
    
@J.M. Ta(l)king about low-hanging fruit :-) –  Yves Klett Nov 12 '12 at 14:10

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.