How to link two arrays?

2 ビュー (過去 30 日間)
Meshooo
Meshooo 2017 年 1 月 30 日
コメント済み: Meshooo 2017 年 1 月 31 日
Dear all,
I have two arrays:
A = [1,2,3,4,5,6,7,8,9,10];
B = [11,12,13,14,15,16,17,18,19,20];
If A became New_A = [4,3,2,1,10,9,8,7,6,5];
Then how to apply the same change to B such that
New_B = [14,13,12,11,20,19,18,17,16,15].
Any help will be appreciated.
Meshoo

採用された回答

Stephen23
Stephen23 2017 年 1 月 30 日
編集済み: Stephen23 2017 年 1 月 30 日
Assuming that the elements are unique, then ismember will work:
>> A = [1,2,3,4,5,6,7,8,9,10];
>> B = [11,12,13,14,15,16,17,18,19,20];
>> New_A = [4,3,2,1,10,9,8,7,6,5];
>> [~,idx] = ismember(New_A,A);
>> New_B = B(idx)
New_B =
14 13 12 11 20 19 18 17 16 15
  1 件のコメント
Meshooo
Meshooo 2017 年 1 月 31 日
Thank you very much.

サインインしてコメントする。

その他の回答 (1 件)

Jan
Jan 2017 年 1 月 30 日
If A contains the sorted numbers from 1 to n as in your example, the solution is easy:
B_new = B(A_new);
But maybe this matters the example data for the forum only.
  1 件のコメント
Meshooo
Meshooo 2017 年 1 月 31 日
Thank you Jan. Yeah I have more complex situations and it is not always serial numbers.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeCharacters and Strings についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by