Why does contains(var1, var2) find matches in two different string arrays?

3 Ansichten (letzte 30 Tage)
I have two string arrays, var1 and var2. When I use the contains function, I get a match, despite these two strings having zero common values.
String 1:
ch_names = {'FP1','FPz','FP2','AF7','AF3','AF4','AF8','F7','F5','F3',...
'F1','Fz','F2','F4','F6','F8','FT7','FC5','FC1','FC2','FC6','FT8',...
'T7','C5','C3','C1','Cz','C2','C4','C6','TP7','CP3','CP1','CP2',...
'CP4','TP8','P7','P5','P3','P1','Pz','P2','P4','P6','P8','PO7',...
'PO3','PO4','PO8','O1','Oz','O2','Iz'};
String 2:
MEG_ch = {'EOG061','EOG062','ECG063','STI101','STI201','STI301',...
'MISC201','MISC202','MISC203','MISC204','MISC205','MISC206','MISC301',...
'MISC302','MISC303','MISC304','MISC305','MISC306'};
Code used to compare the variables:
contains(string(MEG_ch), string(ch_names))
% Output:
%
% ans =
% 1×18 logical array
% 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1 1 1 1
The purpose of this code is to see if any string in ch_names (var2) occurs in MEG_ch (var1). If it does occur, then var1 gets categorized a certain way.
Also, if i switch var2 and var1's position in the contains function, I get the expected behavior of no matches. i.e contains(string(ch_names), string(MEG_ch)).
If there are more efficient ways to do this or I can explain a certain point in more detail, I welcome your suggestions.

Akzeptierte Antwort

madhan ravi
madhan ravi am 6 Mär. 2021
ch_names = {'FP1','FPz','FP2','AF7','AF3','AF4','AF8','F7','F5','F3',...
'F1','Fz','F2','F4','F6','F8','FT7','FC5','FC1','FC2','FC6','FT8',...
'T7','C5','C3','C1','Cz','C2','C4','C6','TP7','CP3','CP1','CP2',...
'CP4','TP8','P7','P5','P3','P1','Pz','P2','P4','P6','P8','PO7',...
'PO3','PO4','PO8','O1','Oz','O2','Iz'};
MEG_ch = {'EOG061','EOG062','ECG063','STI101','STI201','STI301',...
'MISC201','MISC202','MISC203','MISC204','MISC205','MISC206','MISC301',...
'MISC302','MISC303','MISC304','MISC305','MISC306'};
any(ismember(ch_names, MEG_ch)) % any matches at all 0 means false and vice versa
ans = logical
0
  3 Kommentare
Walter Roberson
Walter Roberson am 6 Mär. 2021
'MISC202' (8th element)
'C2' (28th element of pattern)
'MISC202' contains 'C2' so so that is a match.
The contains() operator does not compare for entire strings: it compares for substrings.
Hamza Imtiaz
Hamza Imtiaz am 6 Mär. 2021
Bearbeitet: Hamza Imtiaz am 7 Mär. 2021
Walter,
Thank you, that makes sense.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Resizing and Reshaping Matrices 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