COMPGROUPS.NET | Search | Post Question | Groups | Stream | About | Register

### randperm in m x n array with different randperm(n) by row

• Email
• Follow

```Hello All
I'd like to randomly reorder an array of arbitrary size m x n but keep the elements that are in row m_i in that same row.
in a for loop it would be something like

for i=1:size(array,1)
holder=array(i,:)
array(i,:)=holder(randperm(size(holder,2)));
next i

is it possible to use randperm to generate m rows of randomised integers 1 to n? Would some other method be more appropriate?
```
 0

See related articles to this posting

```"Al " <alek.maxwell@eon.com> wrote in message <hqn6pg\$mrh\$1@fred.mathworks.com>...
> Hello All
> I'd like to randomly reorder an array of arbitrary size m x n but keep the elements that are in row m_i in that same row.
> in a for loop it would be something like
>
> for i=1:size(array,1)
> holder=array(i,:)
>  array(i,:)=holder(randperm(size(holder,2)));
> next i
>
> is it possible to use randperm to generate m rows of randomised integers 1 to n? Would some other method be more appropriate?

This FEX submission might help:

http://www.mathworks.com/matlabcentral/fileexchange/27076-shuffle

James Tursa
```
 0

```% Data
A=reshape(1:50,5,10)'

% Engine
[m n] = size(A);
[~, col] = sort(rand(m,n),2);
B = A(bsxfun(@plus,(col-1)*m,(1:m).'))

% Bruno
```
 0

```James, thanks for the link I will take a look over the material.
Bruno, thanks for that clever code.
Much appreciated!
```
 0

3 Replies
890 Views

Similar Articles

12/6/2013 2:23:57 PM
page loaded in 4674 ms. (0)