Why are my matrices changing size in a for loop?

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

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

Thanks for your help! I just tried that but the only thing is my little netbook runs out of memory! Is there any other way of setting the matrix size in the loop, so maybe the grid size (x,y) can be preallocated, and then the loop can add the time? I really don't know how you would do this though.
Unless you're discarding the values (saving, clearing or overwriting) you'll just run out of memory slightly later when t gets to the point your memory is maxed. Buying more memory is your easiest option. You could also look into downsampling your matrices. If you downsample by two in each dimension it'll save you 8x the memory:
e.g:
for x = 1:2:m
for y = 1:2:n
Or you could just downsample in a choice dimension. (You'll have to add new variables to account for the change in x/y value as it corresponds to indices; i.e: xidx = 0; for x = 1:2:m; xidx = xidx+1; etc.
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');
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 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!