Comparing and locating string values

1 vue (au cours des 30 derniers jours)
Jose Grimaldo
Jose Grimaldo le 16 Fév 2020
Modifié(e) : Stephen23 le 16 Fév 2020
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 commentaire
Walter Roberson
Walter Roberson le 16 Fév 2020
Look at the second output of ismember()

Connectez-vous pour commenter.

Réponse acceptée

stozaki
stozaki le 16 Fév 2020
Modifié(e) : stozaki le 16 Fév 2020
Please try following command.
ret = find(ismember(x,a))
  1 commentaire
Stephen23
Stephen23 le 16 Fév 2020
Modifié(e) : Stephen23 le 16 Fév 2020
Simpler and more efficient without the superfluous find:
>> [~,idx] = ismember(a,x)
idx =
1 3

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Cell Arrays dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by