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.

 채택된 답변

Ameer Hamza
Ameer Hamza 2020년 10월 9일

1 개 추천

A_new = reshape(A(randperm(numel(A))), size(A))

댓글 수: 2

Mireia Boneta Camí
Mireia Boneta Camí 2020년 10월 9일
thanks, it's perfect!
Ameer Hamza
Ameer Hamza 2020년 10월 9일
I am glad to be of help!

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

KSSV
KSSV 2020년 10월 9일
편집: KSSV 2020년 10월 9일

1 개 추천

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) ;

카테고리

도움말 센터File Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

질문:

2020년 10월 9일

댓글:

2020년 10월 9일

Community Treasure Hunt

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

Start Hunting!

Translated by