MATLAB: Trying to handle a large matrix of nx101 dimensions where n is unknown at start of loop, and I only need the previous vector to calculate the current vector

memory optimization

So, I am writing a code for calculating the temperature curve along a 1 dimensional rod as a function of time until steady state, and the numerical method I am using only needs the temperature curve from the previous time step (which is stored in the matrix), as well as the previous iteration within the current time step (which is thrown out after calculation), but I need to keep the values from all previous time steps even though they are no longer relevant to the calculation. The matrices that I am storing get extremely large, and therefore Matlab starts to slow down, I was wondering if there were any tricks I could use.

Best Answer

Easy, peasy (with the right tools.)
As an example, I'll simulate 101 parallel random walks in 1 dimension, stopping only when one of them goes outside of a preset bound, but I'll store all the walks in one array. At the very end, I'll unpack the function handle the data was stored in.
tic
% initialization step
funh = growdata2;
n = 101;
walklimit = 1000;
vec = zeros(1,n);
funh(vec);
theMoonIsBlue = true;
steps = 0;
while theMoonIsBlue
steps = steps + 1;
% Each step uses only the previous step.
vec = vec + randn(1,n);
funh(vec)
if any(abs(vec) > walklimit)
theMoonIsBlue = false;
end
end
allwalkdata = funh();
toc
Elapsed time is 9.192757 seconds.
9 seconds is way better than the same computation would have taken had I grown an array dynamically, adding one new row to the array at every step.
steps
steps =
189995
So almost 200k steps in the random walk.
Where did these walks terminate?
allwalkdata(end,:)
ans =
Columns 1 through 11
-636.58 -541.39 -1000.9 -436.61 356.79 -157.98 -161.44 478.41 -65.325 -624.95 -795.86
Columns 12 through 22
-569.76 -169.58 37.705 -373.35 -193.1 -316.91 -69.306 -430.6 -140.92 247.08 -143.95
Columns 23 through 33
-339.97 -59.332 173.08 288.98 -78.927 -546.23 26.17 81.455 25.087 399.69 83.686
Columns 34 through 44
520.92 -158.04 690.7 430.35 837.11 604.51 -459.4 369.44 47.953 17.497 265.34
Columns 45 through 55
-73.708 171.93 -502.52 -436.02 213.63 -327.65 439.4 -595.36 -689.5 -609.44 -487.53
Columns 56 through 66
56.602 -341.29 391.56 -345.06 104.16 324 407.17 396.58 -186.36 320.52 127.96
Columns 67 through 77
-126.18 515.95 326.52 -160.58 -291.39 -671.77 375.42 161.05 -215.69 -709.72 -169.1
Columns 78 through 88
442.65 206.96 -366.62 296.44 -68.06 85.838 180.54 93.865 343.9 36.668 196.27
Columns 89 through 99
3.2942 253.73 503.08 482.23 -498.44 149.07 -615.69 144.99 55.706 501.4 -208.14
Columns 100 through 101
-306.1 -342.34