Implementation if statement with two different conditions
조회 수: 3 (최근 30일)
이전 댓글 표시
I have a code using if and else with two different condition 1. if FS1 <= 1 else FS1>1 2. if depth < 20 else depth>20
This is detail of my code: LPi_all=[ ]; for i=1:length(FS1);
depth_allD=[ ];
if FS1 <= 1
for j=1:length(depth);
if depth < 20 % F=1-Fs
w=(10-0.5*depth(j))
else depth > 20
w=0
LPi= (1-FS1(i))*w*3
end
else FS1>1
LPi= 0
end
end
I have a wrong position in the placement the second else.
Error: File: Input_RegLPI.m Line: 335 Column: 5
Illegal use of reserved keyword "else".
Is there any solution for my case
Thanks
댓글 수: 0
채택된 답변
BhaTTa
2024년 10월 21일
Hey @Skydriver, I see that the else condition inside the for loop does not have any if condition, Please refer to the below corrected code:
LPi_all = [];
for i = 1:length(FS1)
depth_allD = [];
if FS1(i) <= 1
for j = 1:length(depth)
if depth(j) < 20
w = (10 - 0.5 * depth(j));
else % This handles the case where depth(j) >= 20
w = 0;
end
LPi = (1 - FS1(i)) * w * 3;
% Collect LPi value for each depth(j)
depth_allD = [depth_allD, LPi];
end
else % This handles the case where FS1(i) > 1
LPi = 0;
depth_allD = [depth_allD, LPi];
end
LPi_all = [LPi_all; depth_allD];
end
Hope it helps.
추가 답변 (1개)
Walter Roberson
2024년 10월 28일
for j=1:length(depth);
if depth < 20 % F=1-Fs
w=(10-0.5*depth(j))
else depth > 20
w=0
LPi= (1-FS1(i))*w*3
end
should probably be
for j=1:length(depth)
w = nan;
if depth(j) < 20
w=(10-0.5*depth(j));
elseif depth(j) > 20
w=0;
end
LPi= (1-FS1(i))*w*3;
Notice the initialization of w to nan in order to catch the case where depth(j) is exactly 20, which is a case that your code does not define.
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Startup and Shutdown에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!