Azzera filtri
Azzera filtri

Using If and Ifelse to create a variable

1 visualizzazione (ultimi 30 giorni)
Andrew
Andrew il 3 Lug 2013
I am trying to create a single variable using if and ifelse. I can do this in excel and it comes up fine, so I know it must be something I am doing wrong. This is referencing two variables to create a new variable Theta_dist. This variable has four different conditions shown below. Does anyone know what I am doing wrong?
if Rad_cold>0 & Rad_hot>0 Theta_dist= atan(Hot_Side./Cold_Side); elseif Rad_cold<=0 & Rad_hot>0 Theta_dist = 1/2*pi + atan(-1*Cold_Side./Hot_Side); elseif Rad_cold<=0 & Rad_hot<=0 Theta_dist = pi + atan(Hot_Side./Cold_Side); else Rad_cold>0 & Rad_hot<=0 Theta_dist = 3/2*pi + atan (-Cold_Side./Hot_Side); end
  1 Commento
Evan
Evan il 3 Lug 2013
if Rad_cold>0 & Rad_hot>0
Theta_dist= atan(Hot_Side./Cold_Side);
elseif Rad_cold<=0 & Rad_hot>0
Theta_dist = 1/2*pi + atan(-1*Cold_Side./Hot_Side);
elseif Rad_cold<=0 & Rad_hot<=0
Theta_dist = pi + atan(Hot_Side./Cold_Side);
else Rad_cold>0 & Rad_hot<=0
Theta_dist = 3/2*pi + atan (-Cold_Side./Hot_Side);
end
I formatted your code to make it a little more readable.

Accedi per commentare.

Risposta accettata

Evan
Evan il 3 Lug 2013
The last else statement is incorrect. If you use else instead of elseif you are basically saying "all other conditions not yet covered." So, if you use else, you can't follow it with any conditions. It's just the catch-all for any "other" situations.
If looks like you're trying to cover all possible combinations of the signs of rad_cold and rad_hot, so removing the condition after else (the part that reads "Rad_cold>0 & Rad_hot<=0" is required. Alternatively, you could change that last else to an elseif.

Più risposte (0)

Categorie

Scopri di più su Get Started with MATLAB in Help Center e File Exchange

Prodotti

Community Treasure Hunt

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

Start Hunting!

Translated by