1
$\begingroup$

This question already has an answer here:

Surely there is a simple answer to this:

Suppose $ R = [3\,1\,2] $ and $ A $ is any matrix with three rows.

$$ A = \begin{bmatrix} 2 & 4 \\ 5 &2 \\ 0 & 3 \\ \end{bmatrix} $$

Is there a way to create a new matrix, $ B $, whose rows are specified by entries of $ R $? In other words the first row of $ B $ is the third row of $ A $, the second row of $ B $ is the first row of $ A $, and the third row of $ B $ is the second row of $ A $.

I've tried B = A[[R, All]], which is analogous to what I'd do in Matlab, B=A(I,:).

$\endgroup$

marked as duplicate by Mr.Wizard Feb 8 '17 at 3:38

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • $\begingroup$ Permute[A, InversePermutation@R] or Extract[A, List /@ R]. $\endgroup$ – Marius Ladegård Meyer Feb 7 '17 at 20:26
  • 6
    $\begingroup$ Doesn't A[[R]] give what you want? $\endgroup$ – bill s Feb 7 '17 at 20:28
  • $\begingroup$ Thanks--all three of those seem to work! $\endgroup$ – fishbacp Feb 7 '17 at 20:37
  • $\begingroup$ See also: (2323), (73110) $\endgroup$ – Mr.Wizard Feb 8 '17 at 3:41
1
$\begingroup$

As pointed out in the comments, there are several ways to do that. The easiest method is

A[[R]]

Although you could use

Extract[A, List /@ R]

or

Permute[A, InversePermutation@R]

as well.

$\endgroup$

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