randomising a given matrics

hello there i need to randomize a matrix and after that i need to get it back original matrix ... can any one help me

2 个评论

Please explain, what "randomize" and "get back" mean. Do you want to add a random matrix, or sort the rows and/or columns randomly and store the sorting index? A small example might be helpful also.
It is always a good idea to read an own question again and trying to imagine, if a foreign reader has enough information to understand the computational part of the problem. without understanding the problem, a meaningful answer is not possible.
thanx for replying
actually i want to swap the element of a given matrix and and then after than i want to get it back in original matrix
for e.g.
a=[1 3 4;5 6 7]
i want to swap its element to get for e.g [1 4 6;3 7 5]
then i want again the original matrix a=[1 3 4;5 6 7]

请先登录,再进行评论。

 采纳的回答

I'll try to answer :)
% test matrix
a = reshape(1:100,10,10)
% randomize - mixing
idx = reshape(randperm(numel(a)),10,10);
b = a(idx)
[~ c] = sort(idx(:));
% restore matrix a
b = b(reshape(c,10,10))

8 个评论

thanx but its not working
use
[~, c] = sort(idx(:));
or
[non,c] = sort(idx(:));
What is wrong?
Can you paste an error?
Try replace the line:
[~ c] = sort(idx(:));
with:
[ignore, c] = sort(idx(:));
Hi Grzegorz!
% test matrix
a = reshape(1:100,10,10)
% randomize - mixing
b = a;
idx = randperm(numel(a));
b(:) = a(idx)
[~, c] = sort(idx(:));
% restore matrix a
b(:) = b(c)
??? Error: File: randomising1.m Line: 7 Column: 2
An array for multiple LHS assignment cannot contain expressions.
??? Error: File: randomising1.m Line: 7 Column: 2
An array for multiple LHS assignment cannot contain expressions.
this was the error message
thanx it seems to be working .....let me check on my problem ...thanx a lot...
replacing [~c]with[~,c] is working thanx

请先登录,再进行评论。

更多回答(0 个)

类别

帮助中心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