複数の比較対象に関し​てどのインデックスが​一致しているかを知る​方法

たとえば,配列
11:20
に対して,
[11 12 14 14]
のように比較対象を用意します.
11は11:20の1番目の値に対応しています.同様に12は2番目,14は4番目の値です.
したがって,11:20と[11 12 14 14]を比較したとき,一致するインデックスの番号は
[1 2 4 4]
となります.
この結果を得るために,
rem((find((11:20==[11;12;14;14]).')),10)
とすることで所望の結果を得ることができましたが,もっと簡単な方法はないでしょうか?

 Réponse acceptée

Akira Agata
Akira Agata le 10 Mai 2020

2 votes

ismember 関数を使うと、簡単に見つけることができます。たとえばご質問の例ですと、以下のようになります。
A = 11:20;
B = [11 12 14 14];
[~,loc] = ismember(B,A);
>> loc
loc =
1 2 4 4

1 commentaire

Yuki Koyama
Yuki Koyama le 10 Mai 2020
ありがとうございます.まさに求めていたものです.活用していきます.

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

Produits

Community Treasure Hunt

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

Start Hunting!