In Matlab, arrays are **dynamic** in size, meaning,
whenever you need a larger array, you can just index based on
this larger number and Matlab will automajically resize your
original array to be the new bigger size. There is a cost
with doing this, which is that your program will slow down, if
you do it often enough. Thus, if you know (or have a good
guess) at how big the array needs to be in the first place,
you can "pre-allocate" it, meaning pre-size the array.

If you know approximately how big an array will be when your program is done, you can ask Matlab to give you an "empty" array of that size to begin with. This will make your program much faster for large data sets. It will have almost no affect for small data sets.

To pre-allocate an array (or matrix) of numbers, you can use the "zeros" function. To pre-allocate an array (or matrix) of strings, you can use the "cells" function.

```
``````
grade_list = zeros(1,100); % approximately 100 students in class
names = cell(1,100); % approximately 100 names for the students in class
```

Here is an example of a script showing the speed difference. Make sure you "clear" the array variable if you try the code more than once.

` ````
% lets time the below code:
start_time = clock();
% large amount of data, no pre-allocation (we "create" the array as we go)
large_data_size = 100000;
for i=1:large_data_size
random_number_array_1(i) = rand();
end
% look at how long that took
fprintf('it took %d seconds\n', etime(clock(), start_time));
```

` ````
% lets time the below code:
start_time = clock();
% large amount of data, pre-allocation (we specifiy approximately how many value)
large_data_size = 100000;
random_number_array_2 = zeros(1,large_data_size);
for i=1:large_data_size
random_number_array_2(i) = rand();
end
% look at how long that took
fprintf('it took %d seconds\n', etime(clock(), start_time));
```

` ````
% lets time the below code:
start_time = clock();
% no pre-allocation, but with a small number of items
large_data_size = 1000;
for i=1:large_data_size
random_number_array_3(i) = rand();
end
% look at how long that took
fprintf('it took %d seconds\n', etime(clock(), start_time));
```

For your likely programming purposes, less than
10000 is likely to be "small" and greater than 10000 is likely
to be "large". These numbers will change based on how much memory
your computer has, **NOT** on how fast your computer is!

Back to Topics List