Sorting a vector by another vector with same terms

Let's say we have a vector A = [1,4,2] and we wish to sort it according to the order given by B = [4,5,1,2] The end result should give A = [4,1,2]. Is there an existing way in Matlab to accomplish this?

 採用された回答

Stephen23
Stephen23 2016 年 7 月 26 日
編集済み: Stephen23 2016 年 7 月 26 日

1 投票

Method 1 using intersect:
>> intersect(B,A,'stable')
ans =
4 1 2
Method 2 using intersect's returned indices:
>> [~,~,X] = intersect(B,A,'stable');
>> A(X)
ans =
4 1 2
Method 3 using ismember:
>> B(ismember(B,A))
ans =
4 1 2

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeShifting and Sorting Matrices についてさらに検索

タグ

質問済み:

D L
2016 年 7 月 26 日

編集済み:

2016 年 7 月 26 日

Community Treasure Hunt

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

Start Hunting!

Translated by