Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Matlab: How to random shuffle columns of matrix

Tags:

I have a matrix like:

 A=     4 7 8 9     3 3 5 7     6 4 8 6 

and wants to random shuffle columns and do it something like:

 A=     8 4 9 7     5 3 7 3     8 6 6 4 

does anyone have any idea?

like image 271
Mahyar Avatar asked Sep 12 '12 09:09

Mahyar


People also ask

How do you randomly shuffle a matrix in Matlab?

randperm(n) returns a row vector that contains a random permutation of the integers from “1” to “n” without of any repetition. randperm(n,k) returns a row vector that contains “k” number of unique integers that are selected randomly from 1 to n.

How do you randomize data in Matlab?

Use the rand , randn , and randi functions to create sequences of pseudorandom numbers, and the randperm function to create a vector of randomly permuted integers. Use the rng function to control the repeatability of your results.

How do you randomly permute a list in Matlab?

p = randperm( n ) returns a row vector containing a random permutation of the integers from 1 to n without repeating elements.


1 Answers

You can shuffle columns using indexing:

A(:,[3 1 4 2]) 

If you want to do it randomly, you can create a random permutation:

A(:,randperm(size(A,2))); 
like image 183
Andrey Rubshtein Avatar answered Oct 09 '22 09:10

Andrey Rubshtein