How do I use one IF-END instead of nested IF-ENDs?
Mostra commenti meno recenti
How can I rewrite the following statement so that the nested IF-END structure is not needed. And also replace the code with an IF - ELSEIF - END control structure that accomplishes the same outcome.
if x < y
if z < 10
w = x*y*z
end
else
w = (x*y*z).^2
end
Risposta accettata
Più risposte (1)
Paulo Silva
il 2 Mar 2011
w=(x*y*z)^(~((x<y) & (z<10))+1)
2 Commenti
Matt Fig
il 2 Mar 2011
Pualo, this is not correct. This will always define w, whereas the IF statements above will not. Specifically, look at x=2,y=4,z=15. This could make a difference where w is defined as a default prior to the IF statement, which has a chance to redefine based on specific conditions.
Paulo Silva
il 2 Mar 2011
I knew it wasn't perfect, I tried to add that situation but got tired and distracted by something else, maybe you or other person can improve it.
Categorie
Scopri di più su Orange in Centro assistenza e File Exchange
Prodotti
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!