MATLAB: How to permute one matrix columns according to rows in different matrix

MATLABpermutationsrandomrandperm

Hi,
I have a matrix A 60*31 , and i want to create a new matrix of B 1000*60, as followed:
Each row in B will include a single random value from each row in A. in total each row will be the size of 60 values. for example,
A=magic (4)
A =
16 2 3 13
5 11 10 8
9 7 6 12
4 14 15 1
than matrix B could be
B=
16 5 7 4
16 11 12 4
3 5 6 15
...
..
..
i tried using randperm, or even creating an additional random index matrix, but apparently i'm doing something wrong. How can i do this?
Thanks!

Best Answer

To select 4 elements from A, one from each column:
index = randi([1,4], 1, 4) + (0:4:12) % linear indexing
A(index)
For a [1000 x 4] matrix:
index = randi([1,4], 1000, 4) + (0:4:12) % Arithmetic expanding: >= R2016b
B = A(index);
For older Matlab versions use:
index = bsxfun(@plus, randi([1,4], 1000, 4), (0:4:12))
[EDITED] If you want rows instead of columns, simply transpose A at first.