Performing calculation with multiple conditions with for loop and nested if statements
显示 更早的评论
I have a column of data (temperatures) and want to create a second column based on these temperatures. In Excel, it is accomplished with a simple nested if statement. My loop in Matlab, however, works for a few rows, then begins to return 0's. The conditions are:
- If temperature (T1) is < 0, then RHCrit = 0
- If T1 is between 0 and 20, then RHCrit = -0.00267*(T1^3)+0.160*(T1^2)-3.13*T1+100
- If T1 is >20, then RHCrit = 80
My code right now is:
RHcrit = zeros(size(T1));
for i = 1:length(T1)
if T1(i)>0
if T1(i)<20
RHcrit(i)=-0.00267*(T1(i)^3)+0.160*(T1(i)^2)-3.13*T1(i)+100
else
RHcrit(i)=80
end
else
RHcrit(i)=0
end
end
2 个评论
Nicolas Schmit
2017-12-5
Please share the value of T1 so that your code can be tested.
Kevin Zhang
2017-12-5
采纳的回答
更多回答(0 个)
类别
在 帮助中心 和 File Exchange 中查找有关 Common Operations 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!