if else if statement problem

조회 수: 1 (최근 30일)
Sally Sakr
Sally Sakr 2023년 5월 12일
이동: Atsushi Ueno 2023년 5월 12일
Please help me if you can. I wrote this but the answer still gives me the final condition only "not identified". What is the mistake ?
for PAs = 9
DAs = 8;
As = 131;
Bs = 3;
if PAs==0
DAs==0;
As==0;
Bs==0;
disp('specimen is sound')
elseif PAs==0:10
DAs==0:10;
As<100;
Bs<2;
disp('cap undercut')
else
disp('not identified')
end
end
not identified

답변 (1개)

Cris LaPierre
Cris LaPierre 2023년 5월 12일
The following conditional is seen as 'false' by the elseif statement.
elseif PAs==0:10
This will check PAs against every number in 0:10. It will only evaluate true (and execute the corresponding statement) if all conditions evalue 'true'
PAs=9;
PAs==0:10
ans = 1×11 logical array
0 0 0 0 0 0 0 0 0 1 0
If you want the elseif statement to execute if any of the comparisons is true, use any
any(PAs==0:10)
ans = logical
1
  댓글 수: 1
Atsushi Ueno
Atsushi Ueno 2023년 5월 12일
이동: Atsushi Ueno 2023년 5월 12일
PAs = 9;
PAs == 0:10
ans = 1×11 logical array
0 0 0 0 0 0 0 0 0 1 0
if PAs == 0:10
disp('identified')
else
disp('not identified')
end
not identified
if any(PAs == 0:10)
disp('identified')
else
disp('not identified')
end
identified

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

카테고리

Help CenterFile Exchange에서 Antennas, Microphones, and Sonar Transducers에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by