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

1 件のコメント

Mireia Boneta Camí
Mireia Boneta Camí 2020 年 10 月 9 日
thanks, randperm it's a good discovery

サインインしてコメントする。

カテゴリ

ヘルプ センター および File Exchange で Random Number Generation についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by