Comparing and locating string values

1 回表示 (過去 30 日間)
Jose Grimaldo
Jose Grimaldo 2020 年 2 月 16 日
編集済み: Stephen23 2020 年 2 月 16 日
I have a x=1x10 string of colors and the user enters a a=1x3 string of colors. Im trying to get the index of the location in the x-variable. How can i achieve that?
% my code
x=["Brown","Blue","Red","Orange","Purple","Black","Yellow","White"];
% User input
a=["Brown","Red"];
% I try this but it does not work
idx=strcmpi(a(1:2),x);
  1 件のコメント
Walter Roberson
Walter Roberson 2020 年 2 月 16 日
Look at the second output of ismember()

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

採用された回答

stozaki
stozaki 2020 年 2 月 16 日
編集済み: stozaki 2020 年 2 月 16 日
Please try following command.
ret = find(ismember(x,a))
  1 件のコメント
Stephen23
Stephen23 2020 年 2 月 16 日
編集済み: Stephen23 2020 年 2 月 16 日
Simpler and more efficient without the superfluous find:
>> [~,idx] = ismember(a,x)
idx =
1 3

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

その他の回答 (0 件)

カテゴリ

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

Community Treasure Hunt

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

Start Hunting!

Translated by