How to generate random matrix from another one?

6 ビュー (過去 30 日間)
IM
IM 2019 年 9 月 19 日
コメント済み: IM 2019 年 9 月 20 日
Hi!
I have a 18 x 57x11 matrix .
How can I make a new random matrix (the same size) , which is generated by "shuffling" the values of the original matrix?
A lot thanks in advance!
  1 件のコメント
Adam Danz
Adam Danz 2019 年 9 月 19 日
編集済み: Adam Danz 2019 年 9 月 19 日
Ha! You got 3 different approaches that use randperm() and reshape().
By the way, your data is not a matrix. A matrix is 2 dimensional, with rows and columns. Your data contains a 3rd dimension which makes it an array. All 3 answers will work no matter how many dimensions are in the input.

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

採用された回答

Adam Danz
Adam Danz 2019 年 9 月 19 日
編集済み: Adam Danz 2019 年 9 月 19 日
You can use randperm() to shuffle the values of array 'm'.
% Original array
m =rand(180, 57, 11);
% Random permutation index
randpermIdx = randperm(numel(m));
% m shuffled
mShuffle = reshape(m(randpermIdx), size(m));
To convince yourself that mShuffle is merely a random reorganization of m with no additional or missing values or duplicate samples, use this line below which should return True.
isequaln(sort(m(:)),sort(mShuffle(:)))
  1 件のコメント
IM
IM 2019 年 9 月 20 日
Thanks Adam ;)!!!

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

その他の回答 (2 件)

Bruno Luong
Bruno Luong 2019 年 9 月 19 日
A is your input matrix
Ashuffle = reshape(A(randperm(numel(A))),size(A))
  1 件のコメント
IM
IM 2019 年 9 月 20 日
Thanks Bruno ;) !!!

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


Adam
Adam 2019 年 9 月 19 日
newMatrix = reshape( oldMatrix( randperm( numel( oldMatrix ) ) ), size( oldMatrix ) );
should work, though I'm sure there are neater ways.
  1 件のコメント
IM
IM 2019 年 9 月 20 日
Thanks Adam ;)!!!

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

カテゴリ

Help Center および File ExchangeCreating and Concatenating Matrices についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by