0
$\begingroup$

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?

$\endgroup$
4
$\begingroup$

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}}}

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

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]]
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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