Why are my matrices changing size in a for loop?
Show older comments
Hi, I am trying to calculate values across a grid that is 239 by 216 in size, and again through time, so I'm using a nested for loop. Only thing is when it is stopped for de-bugging the matrices I'm trying to make are stange sizes (not 239 by 216), but the new matrices are all the same size, why does this happen? Am I mis-understanding how for loops work? It is important that the matrices stay the same size because they are used as inputs to functions that are in the loop where say they are divided by another matrix that has already been defined as 239 by 216. So I'm getting errors later on because the matrix dimensions don't agree (or at least I think that's why....)I can't define the array dimensions before the loop because they are bigger than my memory can handle(239 by 216 by 2280).
for t = 1:datasize
for x = 1:m
for y = 1:n
snowfall = snowdata (:,:,(snowday(t)));
%makes a snowfall matrix for each hour that corresponds with the correct matrix for the day from the snowdata array.
if outline (x,y) == 1
%Calculate met variables
%Air Temperature
if altitude (x,y)< UPel
T_a (x,y,t) = LOT_a (t)+ (LO_UP_T_a_lapse(t)*(altitude(x,y)-LOel));
elseif altitude (x,y)> ICEel
T_a (x,y,t) = ICET_a (t)+ (ICE_SKY_T_a_lapse*(altitude(x,y)-ICEel));
else
T_a (x,y,t)= UPT_a (t) + (UP_ICE_T_a_lapse (t)*(altitude(x,y)-UPel));
end
%Calculate saturation vapour pressure of air, using
%Teten's equation (uses air temp in Celsius
e_s_air (x,y,t)= 610.8*exp(17.27 * T_a(x,y,t)/ (237.3+ T_a(x,y,t)));
%Wind Speed
u (x,y,t) = LOu (t) + (LO_UP_u_lapse (t)*(altitude(x,y)-LOel));
%Air relative humidity
RH_a (x,y,t) = LORH_a (t) + (LO_UP_RH_lapse (t)*(altitude(x,y)-LOel));
%Calculate actual vapour pressure of air, from RH and
%saturation vapour pressure
e_a (x,y,t)= RH_a(x,y,t)*e_s_air(x,y,t)/100;
%Calculate specific humidity of air, assuming actual
%vapour pressure
q_a (x,y,t)= 0.622*e_a(x,y,t)/(p_a(x,y)-(0.378*e_a(x,y,t)));
%Downwelling longwave radiation
Ldown (x,y,t) = LOLdown (t) + (Ldown_lapse*(altitude(x,y)-LOel));
%Downwelling shortwave radiation
if elevation (x,y)< C6_el
Sdown (x,y,t) = LOSdown (t);
else
Sdown (x,y,t) = UPSdown (t);
end
%Rainfall
r (x,y,t) = LOr_m (t) + (r_lapse*(altitude(x,y)-LOel));
%Surface Relative humidity this needs to be a grid
RH_sfc (x,y,t)= LORH_s (t);
end
end
end
end
Any ideas much appreciated! It's probably something obvious but I'm a bit new to this.
Thanks!
Accepted Answer
More Answers (0)
Categories
Find more on Mathematics 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!