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

5 views (last 30 days)
Yuki Koyama
Yuki Koyama on 10 May 2020
Commented: Yuki Koyama on 10 May 2020
たとえば,配列
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)
とすることで所望の結果を得ることができましたが,もっと簡単な方法はないでしょうか?

Accepted Answer

Akira Agata
Akira Agata on 10 May 2020
ismember 関数を使うと、簡単に見つけることができます。たとえばご質問の例ですと、以下のようになります。
A = 11:20;
B = [11 12 14 14];
[~,loc] = ismember(B,A);
>> loc
loc =
1 2 4 4
  1 Comment
Yuki Koyama
Yuki Koyama on 10 May 2020
ありがとうございます.まさに求めていたものです.活用していきます.

Sign in to comment.

More Answers (0)

Categories

Find more on Resizing and Reshaping Matrices in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!