Comparing Two String Arrays

109 Ansichten (letzte 30 Tage)
xRobot
xRobot am 19 Nov. 2019
Kommentiert: Sean de Wolski am 19 Nov. 2019
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

Akzeptierte Antwort

Image Analyst
Image Analyst am 19 Nov. 2019
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

Weitere Antworten (1)

David Hill
David Hill am 19 Nov. 2019
if sum(ismember(string,stringMe))>0
disp('hello');
end

Kategorien

Mehr zu Simulink finden Sie in Help Center und File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by