for loop produces a 103x1 array I need a 1x103 array.

조회 수: 1 (최근 30일)
Adam Kevin Francis Baker
Adam Kevin Francis Baker 2019년 2월 17일
댓글: Adam Kevin Francis Baker 2019년 2월 17일
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

채택된 답변

dpb
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...
  댓글 수: 1
Adam Kevin Francis Baker
Adam Kevin Francis Baker 2019년 2월 17일
Thank you for answering my original question. Your notes were also appreciated. The reason the conditions are set up like that is because I got them from a table which had them written out...see attached. Also, I'm going to continue using the for loop because I'm still learning and thats what I was taught in class.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Resizing and Reshaping Matrices에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by