How to quickly find the indecies of elements in an array?

2 ビュー (過去 30 日間)
Benson Gou
Benson Gou 2021 年 5 月 23 日
コメント済み: Torsten 2021 年 5 月 23 日
Dear All,
I have an array A which contains integers. I have another array B. I want to find out the indecis of B in A.
For example, A = [1 9 2 8 3 7 4 6 5], B = [5 6 7]. The indecis of B in A is [9 8 6].
Thanks.
Benson

採用された回答

per isakson
per isakson 2021 年 5 月 23 日
編集済み: per isakson 2021 年 5 月 23 日
A = [1 9 2 8 3 7 4 6 5];
B = [5 6 7];
[~,ix] = ismember( B, A )
ix = 1×3
9 8 6

その他の回答 (1 件)

Torsten
Torsten 2021 年 5 月 23 日
indices = find(A==B)
  2 件のコメント
Benson Gou
Benson Gou 2021 年 5 月 23 日
Hi, Torsten,
I got an error message: Matrix dimensions must agree.
Thanks.
Benson
Torsten
Torsten 2021 年 5 月 23 日
Ok, seems a loop is required to find the indices for more than one element using the "find" command.

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

カテゴリ

Help Center および File ExchangeDownloads についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by