Arrays 排序后无法保留第二个属性
我设置了4个变量(4 x 2双精度)。当我对它进行排序时,每个元素的第二个属性也会被排序。我只需要对第一个属性进行排序。 比如说,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] =
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