Understanding memory preallocation in automatic expansion

8 views (last 30 days)
Hello everyone, I'm one of the users that really love the new automatic expansion with the arithmetic operators introduced in matlab 2016b. I use it heavily, for instance when I want to compute the 3rd order derivative of an vector function F(x) = [f1(x),f2(x),...], I just have to define an array where the first dimension correspond to the function fi, the second dimension to the derivative with respect the variable xj, the third to the derivative with respect to variable, xk, etc (so if I have a quantity that is constant with respect to xi, then its matrix has a singleton in that dimension...
Well, let go to my question, I have a function where this operation is happening:
M = sum(A .* B + C .* D,2);
This line involve an important singleton expansion since the previous matrices are big. Moreover, that line will be executed over and over because the function that contains it will be called thousands of times. So I suppose matlab preallocates the required memory for such expansion every time that function is called, however it does not know that the function will be called again just afterwards and the same amound of memory will be required, so I guess that memory is used for other stuff in the meantime, Am I right?
Is there a way to do that in a more efficient way?
Thanks in advance,
David

Answers (0)

Categories

Find more on Performance and Memory 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!