Using If and Ifelse to create a variable

5 次查看(过去 30 天)
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 个评论
Evan
Evan 2013-7-3
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.

请先登录,再进行评论。

采纳的回答

Evan
Evan 2013-7-3
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.

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Get Started with MATLAB 的更多信息

产品

Community Treasure Hunt

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

Start Hunting!

Translated by