我的问题是:
我有一个矩阵2888x16
,我想用它计算一些条件
我希望结果符合下列条件的新矩阵:
问题是:
当我运行代码时,只计算最后'else'
。第一个'if'
和'elseif'
未计算。
这是我的代码:
for i = 1:iw;
ii=1:16;
if w_speed80_win(i,ii) < 3;
eout_win(i,ii) = 0;
elseif w_speed80_win(i,ii) >= 13;
eout_win(i,ii) = 2050;
else
eout_win(i,ii) = (0.13966*(w_speed80_win(i,ii).^5))-(6.1113*(w_speed80_win(i,ii).^4))...
+(97.045*(w_speed80_win(i,ii).^3))-(676.62*(w_speed80_win(i,ii).^2))+(2193.5*w_speed80_win(i,ii))-2632.2;
end
end
答案 0 :(得分:1)
试试这个:
ind_LT_3 = w_speed80_win < 3 ;
ind_GT_13 = w_speed80_win > 13 ;
ind_REST = ~(ind_LT_3 | ind_GT_13 ) ;
w_speed80_win(ind_LT_3 ) = 0;
w_speed80_win(ind_GT_13 ) = 2050;
w_speed80_win(ind_REST ) = (0.13966*(w_speed80_win.^5)) - ..
答案 1 :(得分:0)
保持矢量化可能更简单:
eout_win = (0.13966*(w_speed80_win.^5))-(6.1113*(w_speed80_win.^4))...
+(97.045*(w_speed80_win.^3))-(676.62*(w_speed80_win.^2))+(2193.5*w_speed80_win)-2632.2;
eout_win(w_speed80_win<3)=0
eout_win(w_speed80_win>=13)=2050
您还可以通过以下方式检查w_speed80_win
确实包含小于3的值:
i, ii = find(w_speed80_win<3)
将为您提供低于3的值的索引。