matrix - change columns randomly

Hi, how can I change randomly the positions of entire columns in a matrix [nxm]? For example, I want to change my matrix
A=[ 1 2 3 5 4 ;
2 2 2 2 2 ;
4 5 6 7 7 ;
8 9 8 8 8 ;
9 9 9 9 9 ]; to change into
B=[ 3 5 4 2 1;
2 2 2 2 2;
6 7 7 5 4;
8 8 8 9 8;
9 9 9 9 9];
Thank you :)

Respuestas (1)

Wayne King
Wayne King el 16 de Oct. de 2011

1 voto

A random shuffling of the columns of A. One way:
cols = size(A,2);
P = randperm(cols);
B = A(:,P);

1 comentario

Ruchi Agarwal
Ruchi Agarwal el 7 de En. de 2020
how to restore original columns after randperm?

Iniciar sesión para comentar.

Categorías

Más información sobre Random Number Generation en Centro de ayuda y File Exchange.

Preguntada:

el 16 de Oct. de 2011

Comentada:

el 7 de En. de 2020

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by