Why are my matrices changing size in a for loop?

14 views (last 30 days)
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

Sean de Wolski
Sean de Wolski on 30 Nov 2011
You're referencing t,x,y as indices in each loop iteration. As they grow (as the for-loop progresses) the data will change size to accomodate the new dimensions. To avoid this, preallocate anything that will have it's value set by indices t,x,y.
E.g:
T_a = zeros(m,n,datasize);
Sdown = T_a;
%etc for the remainder of your variables
for t
for x
for y
T_a(x,y,t) = something;
%etc
end
end
end
  4 Comments
Sean de Wolski
Sean de Wolski on 30 Nov 2011
Also, could use class single instead of double (will save half the memory) if you don't need the full precision.
T_a = zeros(m,n,datasize,'single');
Catriona
Catriona on 30 Nov 2011
Hi, thanks they're good ideas, I will probably try setting the classes to single precision first and then see how I get on.
Thanks again for your help!
Catriona

Sign in to comment.

More Answers (0)

Categories

Find more on Creating and Concatenating 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!