for loop produces a 103x1 array I need a 1x103 array.
조회 수: 1 (최근 30일)
이전 댓글 표시
How do I get this for loop to produce a 103X1 array? I do not want to use the transpose function.
for ii = 1:numel(H)
if H(ii) <= 11
P_S(ii) = (101.325)*((288.15/(STemp_K(ii)))^-5.255877);
elseif H(ii) >= 11 && H(ii) <= 20
P_S(ii) = (22.632)^(-0.1577*(H(ii)-11));
elseif H(ii) >= 20 && H(ii) <=32
P_S(ii) = (5.4749)*((216.65/(STemp_K(ii)))^34.16319);
elseif H(ii) >= 32 && H(ii) <=47
P_S(ii) = (0.868)*((228.65/(STemp_K(ii)))^12.2011);
elseif H(ii) >= 47 && H(ii) <=51
P_S(ii) = (0.1109)^(-0.1262*(H(ii)-47));
end
end
댓글 수: 0
채택된 답변
dpb
2019년 2월 17일
Write
P_S(ii,1)...
But, why use a loop at all--use the vector functions in Matlab instead. I've not tried to decipher the RHS to see what simplifications might be made in writing a more general functional form, but at worst you simply write
ix=H<11;
P_S(ix) = 101.325*288.15./STemp_K(ix)^-5.255877;
ix=iswithin(H,11,20);
P_S(ix) = 22.632.^-0.1577*(H(ix)-11);
...
where iswithin is my "syntactic sugar" utility routine
>> type iswithin
function flg=iswithin(x,lo,hi)
% returns T for values within range of input
% SYNTAX:
% [log] = iswithin(x,lo,hi)
% returns T for x between lo and hi values, inclusive
flg= (x>=lo) & (x<=hi);
>>
NB: the "dot" operators to do element-wise operations on the vector sections...
You might note that your conditions are not written exclusve in that you have the "=" on both bounds so the lower limit of the second conditional overlaps the upper limit of the first, and so on...this may not be an issue, just noting...
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!