Operands to the || and && operators must be convertible to logical scalar values.

조회 수: 23 (최근 30일)
Mehdi Jaiem
Mehdi Jaiem 2022년 5월 5일
편집: Mehdi Jaiem 2022년 5월 6일
So I don't understand the reason why I am getting the following error:
riskUserId=[];
if (data.A>5) || (data.B==1)
C=data.D;
end
data is a table
A is of type double
B is of type logical (0s and 1s)
I am still getting used to this table logic and cell content type of data.
  댓글 수: 2
Geoff Hayes
Geoff Hayes 2022년 5월 5일
@Mehdi Jaiem - if data is a table, then what does this mean to you
data.Age>60
? Should there be one age greater than 60 or all ages greater than 60?
Mehdi Jaiem
Mehdi Jaiem 2022년 5월 5일
Well actually data.Age accesses all the column Age in the table Data
Name First Name User_ID Contact_ID Infection Preexisting_Conditions Age
____________ ______________ __________ ____________ _________ ______________________
{'Maier' } {'Alfred' } 1e+05 {1×5 double} true false 70
{'Schulz' } {'Petra' } 1e+05 {[ 100017]} false false 64
{'Schmidt' } {'Dieter' } 1e+05 {1×2 double} false false 58
{'Meier' } {'Benjamin' } 1e+05 {[ 100012]} false false 52
{'Gross' } {'Michael' } 1.0001e+05 {1×5 double} true false 46
{'Klein' } {'Nadja' } 1.0001e+05 {1×2 double} false false 40
when I use data.Age I should get the column age as output.
What I am trying to do is to save all User_IDs fulfilling the above mentioned 2 conditions with an "OR" statement in between.

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

답변 (2개)

Mitch Lautigar
Mitch Lautigar 2022년 5월 5일
riskUserId=[];
%MATLAB is expecting a 1 or 0 for preexisting conditions. IF the values in the table are true/false, use strcmpi(). This would look like (strcmpi(data.Preexisting_Conditions,"true"))
if (data.Age>60) || (data.Preexisting_Conditions==1)
riskUserId= [riskUserId;data.User_ID]; %Stack all values into an array for easy viewing.
end
  댓글 수: 1
Mehdi Jaiem
Mehdi Jaiem 2022년 5월 5일
The content of the column Preexisting_Conditions is already boolean:
Name First Name User_ID Contact_ID Infection Preexisting_Conditions Age
____________ ______________ __________ ____________ _________ ______________________
{'Maier' } {'Alfred' } 1e+05 {1×5 double} true false 70
{'Schulz' } {'Petra' } 1e+05 {[ 100017]} false false 64
{'Schmidt' } {'Dieter' } 1e+05 {1×2 double} false false 58
{'Meier' } {'Benjamin' } 1e+05 {[ 100012]} false false 52
{'Gross' } {'Michael' } 1.0001e+05 {1×5 double} true false 46
{'Klein' } {'Nadja' } 1.0001e+05 {1×2 double} false false 40
I tried using strcmp but it only gives me zeros and ones instead of true and false. Instead I used the followwing line.
data.Preexisting_Conditions=categorical(data.Preexisting_Conditions)=='yes';
(originally I had 'yes' and 'no' instead of 'true' and 'false')
Now the columns Preexisting_Conditions and Infection are of type
class(data.Preexisting_Conditions)
ans =
'logical'

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


Cris LaPierre
Cris LaPierre 2022년 5월 5일
편집: Cris LaPierre 2022년 5월 5일
Use || and && when you are comparing a single (scalar) value. Use | and & when you are comparing arrays (when there is more than 1 output of the comparison).
a = 1:4;
b = 4:-1:1;
% Works
c = a>2 & b<=3
c = 1×4 logical array
0 0 1 1
% Error you are seeing
d = a>2 && b<=3
Operands to the logical AND (&&) and OR (||) operators must be convertible to logical scalar values. Use the ANY or ALL functions to reduce operands to logical scalar values.

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by