# 如何在MATLAB中按其他向量的顺序对矩阵的列进行排序？

``````A=[50936
332680
107430
167940
185820
99732
198490
201250
27626
69375];
``````

``````B=[0    0   0   0   0   0   0   0   0   0
0    0   0   0   0   0   0   0   0   0
1    0   1   0   0   1   0   1   1   1
1    0   1   0   0   1   0   1   1   1
0    0   1   0   0   0   0   1   0   1
0    0   0   0   0   0   0   1   0   0
0    0   0   0   0   0   0   0   0   0
0    0   0   0   0   0   0   0   0   1];
``````

``````A2=[185820
198490
69375
167940
99732
332680
27626
107430
50936
201250];
``````

``````A=A'; A2=A2';

for i=1:size(A,2)
A(2:size(B,1)+1,i)=B(:,i);
end

A2(2:size(B,1)+1,:)=zeros(size(B,1),size(B,2));

for i=size(A2,2)
for j=size(A,2)
if A2(1,i)==A(1,j)
A2(2:end,i)=A(2:end,j);
end
end
end

B2 = A2(2:end,:);
``````

rahnema1

``````[~ ,idx] = ismember(A2,A);
B2 = B(:,idx);
``````

``````disp('-----ISMEMBER:-------')
tic
[~,idx]=ismember(A2,A);
toc
disp('-----SORT:-----------')
tic
[~,idx1] = sort(A);
[~,idx2] = sort(A2);
map = zeros(1,size(idx2));
map(idx2) = idx1;
toc
``````

``````-----ISMEMBER:-------
Elapsed time is 0.00157714 seconds.
-----SORT:-----------
Elapsed time is 4.41074e-05 seconds.
``````

0 条评论