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 Commenti

Jan
Jan il 20 Set 2011
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 il 20 Set 2011
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]

Accedi per commentare.

 Risposta accettata

Grzegorz Knor
Grzegorz Knor il 20 Set 2011
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 Commenti

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

Accedi per commentare.

Più risposte (0)

Categorie

Scopri di più su Random Number Generation in Centro assistenza e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by