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 件のコメント

Jan
Jan 2011 年 9 月 20 日
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.
sumit choubey
sumit choubey 2011 年 9 月 20 日
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]

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

 採用された回答

Grzegorz Knor
Grzegorz Knor 2011 年 9 月 20 日

0 投票

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 件のコメント

sumit choubey
sumit choubey 2011 年 9 月 20 日
thanx but its not working
Andrei Bobrov
Andrei Bobrov 2011 年 9 月 20 日
use
[~, c] = sort(idx(:));
or
[non,c] = sort(idx(:));
Grzegorz Knor
Grzegorz Knor 2011 年 9 月 20 日
What is wrong?
Can you paste an error?
Try replace the line:
[~ c] = sort(idx(:));
with:
[ignore, c] = sort(idx(:));
Andrei Bobrov
Andrei Bobrov 2011 年 9 月 20 日
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)
sumit choubey
sumit choubey 2011 年 9 月 20 日
??? 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.
sumit choubey
sumit choubey 2011 年 9 月 20 日
this was the error message
sumit choubey
sumit choubey 2011 年 9 月 20 日
thanx it seems to be working .....let me check on my problem ...thanx a lot...
sumit choubey
sumit choubey 2011 年 9 月 20 日
replacing [~c]with[~,c] is working thanx

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

その他の回答 (0 件)

カテゴリ

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

Community Treasure Hunt

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

Start Hunting!

Translated by