# Rearranging matrix rows using a list [duplicate]

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,:).

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

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.