for loop produces a 103x1 array I need a 1x103 array.
1 view (last 30 days)
Show older comments
Adam Kevin Francis Baker
on 17 Feb 2019
Commented: Adam Kevin Francis Baker
on 17 Feb 2019
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 Comments
Accepted Answer
dpb
on 17 Feb 2019
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...
More Answers (0)
See Also
Categories
Find more on Resizing and Reshaping Matrices in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!