MATLAB Answers

0

Could anyone help me to solve the following issue

Asked by jaah navi on 3 Jun 2019
Latest activity Commented on by KSSV
on 3 Jun 2019
Code:
a = [1 2 3 4 5;
6 7 8 9 10]
a_rand = a(randperm(length(a)))
When i run the above two commands it executes and gives the following result
a_rand = 3 6 2 7 1
But in this result i can get only one row .
Coud anyone help me how to get two rows with the left out numbers displaying at the second row.

  0 Comments

Sign in to comment.

Tags

2 Answers

Answer by KSSV
on 3 Jun 2019

a = [1 2 3 4 5;
6 7 8 9 10] ;
idx1 = (randperm(length(a))) ;
idx2 = setdiff(1:numel(a),idx1) ;
iwant = [a(idx1) ; a(idx2)]

  4 Comments

Show 1 older comment
iwant = reshape(a(randperm(numel(a))),size(a)) ;
reshape should be done with respect to the elements present in its own column.But when i used the above command it reshapes with respect to different column.could you please help me to solve this.
[m,n] = size(a) ;
b = a ;
for i = 1:m
idx = randperm(n) ;
b(i,:) = a(i,idx) ;
end

Sign in to comment.


Answer by TADA
on 3 Jun 2019

a = [1 2 3 4 5; 6 7 8 9 10]
a_rand = a(randperm(length(a)))
a_rand = [a_rand; a(~ismember(a(:),a_rand(:)))']

  3 Comments

It works fine for two rows,but how it can done for n number of rows with repect to the above mentioned matrix
a=[4.7908 5.0104 5.1329 5.3186 5.3094 5.3070 5.4452 5.5001;
0.0958 0.0994 0 0 0.1848 0.2020 0.1851 0.2146;
0.0848 0 0.1469 0.1859 0.1767 0.1854 0.1856 0.1648;
0 0 0 0 0 0 0 0;
0 0.0896 0.1619 0.1351 0 0 0 0;
0 0 0 0 0 0 0 0];
Could you please help me on this.
KSSVs reahape seems the best solution for your problem, unless you are looking for some other output
but reshape makes use of reshaping from different column elements.
I want to reshape the elements present in its respective column.
For example
if
a= [4.7908 5.0104 5.1329 5.3186 5.3094 5.3070 5.4452 5.5001;
0.0958 0.0994 0 0 0.1848 0.2020 0.1851 0.2146;
0.0848 0 0.1469 0.1859 0.1767 0.1854 0.1856 0.1648;
0 0 0 0 0 0 0 0;
0 0.0896 0.1619 0.1351 0 0 0 0;
0 0 0 0 0 0 0 0]
the the output shoud be
a= [4.7908 0 0 5.3186 5.3094 5.3070 5.4452 0;
0 0.0994 0 0 0 0 0.1851 0.2146;
0.0848 0 0.1469 0.1859 0 0.1854 0 0.1648;
0.0958 0 0 0 0 0 0 5.5001;
0 0.0896 0.1619 0 0.1848 0.2020 0 0;
0 5.0104 5.1329 0.1351 0.1767 0 0.1856 0]
in the following random manner.

Sign in to comment.