2

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

Pillsy
  • 18,498
  • 2
  • 46
  • 92
fishbacp
  • 183
  • 8

1 Answers1

3

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.

halirutan
  • 112,764
  • 7
  • 263
  • 474