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

1 visualización (últimos 30 días)
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

Respuesta aceptada

dpb
dpb el 17 de Feb. de 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...
  1 comentario
Adam Kevin Francis Baker
Adam Kevin Francis Baker el 17 de Feb. de 2019
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.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Loops and Conditional Statements en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by