Could anyone help me to solve the following issue

1 次查看(过去 30 天)
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.

回答(2 个)

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

请先登录,再进行评论。


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

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Language Fundamentals 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by