Allocating elements in a large matrix takes a VERY long time. Why?

19 views (last 30 days)
MATLAB is doing something I have never seen before. Can anyone explain to me the following behaviour:
I have some code:
function myFuncMain()
myMat = NaN(1E5, 5E2);
for t = 1: T
%carry out some tests
%allocate elements to the matrix, eg
myMat(i, j) = 10;
myMat(z,y) = now;
%Now go into an external function
myMat = myFunc(myMat, variables);
function myMat = myFunc(myMat, variables)
%carry out some more tests
%allocate some more elements to the matrix, eg
myMat(ii, jj) = 10;
myMat(zz,yy) = now;
When I look at this code in the profiler, I see that >99.99% of the time is spent on the first allocation to the matrix in myFunc.m
Everytime, this function is called it takes a HUGE amount of time, according to the profiler, for the first element to be assigned to the matrix. Then according to the profiler, all the other assignations are instantaneous as expected.
whats going on? Is this something to do with the size of the matrix? Is it be copied somewhere behind the scenes?
I use a 64bit, 16GB machine.

Accepted Answer

Matt J
Matt J on 14 Feb 2013
Edited: Matt J on 14 Feb 2013
Because you are changing myMat inside myFunc, MATLAB assumes that a new copy of the entire myMat matrix is required. This happens the instant myFunc makes the first change, resulting in the large memory allocation time that you see.
If MATLAB knew that you were planning to overwrite your original myMat in the calling workspace with the one generated inside myFunc, it would not need to create a new copy, but its parsing mechanism isn't smart enough to know that.

Sign in to comment.

More Answers (1)

James Tursa
James Tursa on 14 Feb 2013
You might also consider using this fast matrix allocation function UNINIT from the FEX:
The UNINT function allocates arrays of uninitialized values and can be useful in cases where you know the values will be overwritten downstream before their use.


Find more on MATLAB Coder 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!

Translated by