rearrange matrix elements randomly

13 visualizaciones (últimos 30 días)
Mireia Boneta Camí
Mireia Boneta Camí el 9 de Oct. de 2020
Comentada: Ameer Hamza el 9 de Oct. de 2020
I have a matrix and what i want to do is reorder the matrix elements/entries in a random order. for example if the matrix was :
A= [ 1 2 3;
4 5 6;
7 8 9]
i would like the elements randomly re-arranged, for example:
A = [ 6 9 1
2 5 7
3 8 4]
I would like to do it without using fors to do it more efficiently.

Respuesta aceptada

Ameer Hamza
Ameer Hamza el 9 de Oct. de 2020
A_new = reshape(A(randperm(numel(A))), size(A))
  2 comentarios
Mireia Boneta Camí
Mireia Boneta Camí el 9 de Oct. de 2020
thanks, it's perfect!
Ameer Hamza
Ameer Hamza el 9 de Oct. de 2020
I am glad to be of help!

Iniciar sesión para comentar.

Más respuestas (1)

KSSV
KSSV el 9 de Oct. de 2020
Editada: KSSV el 9 de Oct. de 2020
Read about randperm. With this you can get random indices/ numbers. Let A be your matrix.
[m,n] = size(A) ;
idx = randperm(m*n) ;
A = reshape(A(idx),m,n) ;

Categorías

Más información sobre Creating and Concatenating Matrices en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by