Find index of cell values in another cell array without a loop

32 ビュー (過去 30 日間)
Vic
Vic 2024 年 11 月 3 日 2:39
コメント済み: Vic 2024 年 11 月 3 日 13:25
Hi all,
I have 2 cell arrays. Cell array B is always smaller than A and its values are always contained in A.
I want to find the indexes of each value contained in B within A.
Here is what I have with a loop and it works.
A = {'Q' 'W' 'E' 'R' 'T' 'Y' 'U' 'I' 'O' 'P'};
B = {'W' 'R' 'O'};
for i=1:length(B)
Index(i) = find(strcmpi(A,B(i)));
end
I want to remove the loop for efficiency purposes.
What would you suggest?
Thanks,

採用された回答

Akira Agata
Akira Agata 2024 年 11 月 3 日 3:24
It's time to use ismember function!
A = {'Q' 'W' 'E' 'R' 'T' 'Y' 'U' 'I' 'O' 'P'};
B = {'W' 'R' 'O'};
[~, index] = ismember(B, A)
index = 1×3
2 4 9
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
  1 件のコメント
Vic
Vic 2024 年 11 月 3 日 13:25
Wonderful!
I knew it was something simple but I could not phrase my question properly on Google.
Thanks a lot

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by