elseif using specific sets

4 vues (au cours des 30 derniers jours)
Dave
Dave le 4 Nov 2014
Réponse apportée : Dave le 4 Nov 2014
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

Réponse acceptée

Dave
Dave le 4 Nov 2014
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

Plus de réponses (0)

Catégories

En savoir plus sur App Building dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by