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);

 채택된 답변

stozaki
stozaki 2020년 2월 16일
편집: stozaki 2020년 2월 16일

1 개 추천

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개)

카테고리

도움말 센터File Exchange에서 Characters and Strings에 대해 자세히 알아보기

질문:

2020년 2월 16일

편집:

2020년 2월 16일

Community Treasure Hunt

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

Start Hunting!

Translated by