Comparing Two String Arrays

조회 수: 80 (최근 30일)
xRobot
xRobot 2019년 11월 19일
댓글: Sean de Wolski 2019년 11월 19일
Hello,
I am trying to compare two string arrays to each other to see if any of the elements in one string array are in the other string array. I assumed the best approach was to use "any" and "strcmp".
This is my code. I was able to make it work if i placed "dad" as the first elment in 'stringMe'. However when I set the first element in stringMe to "mom", it did not work. Any suggestions or advice are greatly appreciated.
string = ["dad" "hey" "mom"];
stringMe= ["mom" "bob" "ted"];
if any(strcmp(string,stringMe))
disp('hello');
end

채택된 답변

Image Analyst
Image Analyst 2019년 11월 19일
Try intersect():
string = ["dad" "hey" "mom"];
stringMe= ["mom" "bob" "ted"];
inBoth = intersect(string, stringMe)
if ~isempty(inBoth)
message = sprintf('"%s" is in both', inBoth);
uiwait(helpdlg(message));
end
  댓글 수: 1
xRobot
xRobot 2019년 11월 19일
Very helpu! Thanks!

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

David Hill
David Hill 2019년 11월 19일
if sum(ismember(string,stringMe))>0
disp('hello');
end

카테고리

Help CenterFile Exchange에서 Polymers에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by