Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby-on-rails-4/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Arrays 排序后无法保留第二个属性_Arrays_Matlab_Sorting_Matrix - Fatal编程技术网

Arrays 排序后无法保留第二个属性

Arrays 排序后无法保留第二个属性,arrays,matlab,sorting,matrix,Arrays,Matlab,Sorting,Matrix,我设置了4个变量(4 x 2双精度)。当我对它进行排序时,每个元素的第二个属性也会被排序。我只需要对第一个属性进行排序。 比如说, set4=[ 10 1; 20 1; 5 2; 15 2]; sort(set4) 输出: ans = 5 1 10 1 15 2 20 2 但我的预期产出是, ans= 我该怎么做 set4=[ 10 1; 20 1; 5 2; 15 2]; %example data [set,in] =

我设置了4个变量(4 x 2双精度)。当我对它进行排序时,每个元素的第二个属性也会被排序。我只需要对第一个属性进行排序。 比如说,

set4=[ 10 1; 20 1; 5 2; 15 2];
sort(set4)
输出:

ans =

     5     1
    10     1
    15     2
    20     2
但我的预期产出是, ans=

我该怎么做

set4=[ 10 1; 20 1; 5 2; 15 2];  %example data
[set,in] = sort(set4(:,1));   %sort just the first column and get the indices
set(:,2)= set4(in,2)          %use the indices to re-order the second column
set =

 5     2
10     1
15     2
20     1
set4=[ 10 1; 20 1; 5 2; 15 2];  %example data
[set,in] = sort(set4(:,1));   %sort just the first column and get the indices
set(:,2)= set4(in,2)          %use the indices to re-order the second column
set =

 5     2
10     1
15     2
20     1
set4=[ 10 1; 20 1; 5 2; 15 2];
sortrows(set4)

ans=

 5     2
10     1
15     2
20     1