How to efficiently sort a vector relative to a reference vector

1 回表示 (過去 30 日間)
balandong
balandong 2017 年 11 月 24 日
コメント済み: balandong 2017 年 11 月 24 日
Dear Coder, The objective is to sort a matrix B with respect to the number in a vector A. The following code was implement to achieve the objective. However, I wonder if another more compact (w/o the FOR loop) can also be realize?. I really appreciate for any suggestion.
load('help_sort.mat');
RefMats=RefMat;
ToSorts=ToSort;
c_m=1;
row_locv_CELL=zeros(1,size(RefMats,1));
for f_x=1:size(RefMats,1)
row_locv_CELL(c_m)=find(ToSorts(:,1)==RefMats(f_x));
c_m=c_m+1;
end
relative2REF=ToSorts(((row_locv_CELL))',:);

採用された回答

Andrei Bobrov
Andrei Bobrov 2017 年 11 月 24 日
編集済み: Andrei Bobrov 2017 年 11 月 24 日
[~,ii] = ismember(RefMat,ToSort(:,1));
relative2REF = ToSort(ii,:);

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeShifting and Sorting Matrices についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by