Could anyone help me to solve the following issue

1 view (last 30 days)
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.

Answers (2)

KSSV
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
jaah navi
jaah navi on 3 Jun 2019
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.
KSSV
KSSV on 3 Jun 2019
[m,n] = size(a) ;
b = a ;
for i = 1:m
idx = randperm(n) ;
b(i,:) = a(i,idx) ;
end

Sign in to comment.


TADA
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
TADA
TADA on 3 Jun 2019
KSSVs reahape seems the best solution for your problem, unless you are looking for some other output
jaah navi
jaah navi on 3 Jun 2019
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.

Categories

Find more on Language Fundamentals in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!