How to efficiently sort a vector relative to a reference vector
조회 수: 1 (최근 30일)
이전 댓글 표시
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))',:);
댓글 수: 0
채택된 답변
Andrei Bobrov
2017년 11월 24일
편집: Andrei Bobrov
2017년 11월 24일
[~,ii] = ismember(RefMat,ToSort(:,1));
relative2REF = ToSort(ii,:);
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Shifting and Sorting Matrices에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!