필터 지우기
필터 지우기

logical operation with categorical data

조회 수: 3 (최근 30일)
Doron Joffe
Doron Joffe 2021년 12월 1일
편집: Stephen23 2021년 12월 1일
I have 6 csv files with one of the variables in the csv file containing names of players. I want to perform operations only if the name in the particular file is the name of a certain player. When I perform the logical operation in the 'if' line, I get an error. Am I performing the logical operation incorrectly? Is there a different way to do logical operations with categorical data?
for i = 1:6
csv = readtable(file(i).name);
K=categorical(csv.Var2(2)); %% reads the player name as categorical data.
if K=='Ronaldo'||'Messi'||'Neymar' % Only enter the loop if K is one of these players names
m=0;
m=m+1;
end
end
  댓글 수: 2
Stephen23
Stephen23 2021년 12월 1일
편집: Stephen23 2021년 12월 1일
This will not work because you are attempting logical operations on character arrays of different sizes:
'Ronaldo'||'Messi'||'Neymar'
Use ISMEMBER or CONTAINS instead.
Doron Joffe
Doron Joffe 2021년 12월 1일
Thank you very much for all the help.

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

채택된 답변

Rik
Rik 2021년 12월 1일
This depends a bit on your source data, but you should look at the ismember function. Especially when combined with any, it really helps in these situations. Alternatively you could look at switch, which also allows multiple case definitions.

추가 답변 (0개)

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by