elseif using specific sets

조회 수: 3 (최근 30일)
Dave
Dave 2014년 11월 4일
답변: Dave 2014년 11월 4일
Hello, I need to evaluate a function for specific sets, ideally don't want to evalute the function for all sets so once it is above the condition to stop. I try to use the "elseif" statement but it never actually enters the elseif and terminates, even though it should enter.
Example
xa=[1 2] ; xb=[0 1] ; xc=[-1 4]; xd=[4 7]; xe=[10 10]; xf=[0 0];
y=xa(1)+xa(2)
if y<10
y=xb(1)+xb(2)
elseif y<10
y=xc(1)+xc(2)
elseif y<10
y=xd(1)+xd(2)
elseif y<10
y=xe(1)+xe(2)
else
y=xf(1)+xf(2)
end
Above, in the "xa" case, y=3 so it enters the if statament, evaluates the "xb" case and gets y=1 so it should enter the elseif of case "xc", but it terminates.
In this example, I need the final y to be the case xd so y=11. This is the first instance the criterion is not satisfied.
Do you know what I'm doing wrong and how to fix it? Thanks

채택된 답변

Dave
Dave 2014년 11월 4일
I'll use "while" instead, thanks though
X=[1 2; 0 1; -1 4; 4 7; 10 10; 0 0]
ii=1
y=1e-10
while y<10
y=X(ii,1)+X(ii,2)
ii=ii+1
end

추가 답변 (0개)

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by