How to generate random matrix from another one?
9 次查看(过去 30 天)
显示 更早的评论
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
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
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(:)))
更多回答(2 个)
Adam
2019-9-19
newMatrix = reshape( oldMatrix( randperm( numel( oldMatrix ) ) ), size( oldMatrix ) );
should work, though I'm sure there are neater ways.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Creating and Concatenating Matrices 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!