%{
This is a set of demos to show the usage of growdata and growdata2.
Dynamic growth of an array in matlab is an easy thing to do. Its
also a terrible waste of cpu cycles. The problem is that every time
you append new rows (or columns) to an existing array, Matlab needs
to re-allocate you entire array. If you append new rows to a matrix,
then its even worse, since it needs to shuffle all the numbers around.
Far better is to pre-allocate your arrays to their final dimension.
Now you just use indexing to replace the old rows of zeros with their
final values. This is MUCH better. But sometimes you just don't know
how many rows or colums you will have in the end. So you get stuck.
A better solution for small numbers of appendations is to use a cell
array, coupled with one big concatenation at the end. This actually
works acceptably. The problem is not to pass that array back and
forth constantly. This takes much time, over thousands of iterations.
So any function that does this appending operation is best written to
keep it permanently in memory.
The much better solution is to use growdata or growdata2. These
came from a discussion on comp.soft-sys.matlab. Growdata works by
keeping its data as a persistent variable. Growdata2 uses a nested
function to store its data until it is time to unpack the array.
Both growdata and growdata2 require 3 steps for growth. They allow
you to append any sizes of new elements, as long as they will be
conformable for the final concatenation step.
- An intialization step
- Growth inside a loop
- An unpacking step
Growdata2 is more sophisticated. It allows you to append as either
rows or columns, whereas growdata only allows you to append new
rows to your data. Growdata does have one virtue though, as it
should run on somewhat older releases of matlab. Growdata2 will
require at least R14 of matlab, since it uses nested functions.
%}

Simple concatenation of 10000 blocks, using growdata

In this case, yes, we know exactly the final size of the array. This example is merely a time test, plus it shows that growdata
produces the correct size result.

Growth with larger chunks

You can grow any size chunks you want. If the chunks are really large, then it might make sense to specify a larger blockwize
for growdata2, but these tools automatically adjust their blocksizes internally if you append large chunks of data.