필터 지우기
필터 지우기

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

조회 수: 3 (최근 30일)
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.

채택된 답변

madhan ravi
madhan ravi 2021년 3월 6일
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
Walter Roberson
Walter Roberson 2021년 3월 6일
'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 2021년 3월 6일
편집: Hamza Imtiaz 2021년 3월 7일
Walter,
Thank you, that makes sense.

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Resizing and Reshaping Matrices에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by