MATLAB Answers

Matt J

Matrix multiplication result - where does it go?

Asked by Matt J
on 23 Jan 2013

I've been curious, after various recent observations, about whether matrix multiplication always allocates fresh memory for its output. For example, suppose I do something like


Is this equivalent to

    z=B*x; %memory allocated here

Or, does the output of B*x get directly generated in the memory locations occupied by A(:,1)? Obviously, the latter would be more efficient, but I wasn't sure how it worked. I know for example that this


is equivalent to

 z=B(1,:); %memory allocated here

so obviously not everything is as well optimized as it could be.

  1 Comment

Cedric Wannaz
on 24 Jan 2013

I used Process Lasso to set a watchdog on MATLAB memory usage at 2GB.

In the following, n=1.36e4 is the size needed for building a matrix that will put MATLAB virt. mem usage right below 2GB on my system.

This is OK (usage ~1.8GB):

 >> clear all ; n = 1.36e4 ; v = ones(n,1) ; A = zeros(n) ;

This is also OK (usage ~1.8GB):

 >> clear all ; n = 1.36e4 ; v = ones(n,1) ; A = v * v.' ;

This is killed:

 >> clear all ; n = 1.36e4 ; v = ones(n,1) ; A = zeros(n) ; A = v * v.' ;

I could perform more tests that fit better your initial question, but I wanted to start with something that would generate a brutal increase in memory usage if temp. memory was allocated.. which seems to be the case!

(PS: but not before tomorrow, because tonight I have to perform computations using a.. an Excel spreadsheet! ;-/)


No products are associated with this question.

1 Answer

Answer by James Tursa
on 24 Jan 2013
Edited by James Tursa
on 24 Jan 2013
 Accepted answer

Interesting question. The answer will probably depend on MATLAB version and JIT settings. TMW likely keeps stuffing more & more optimization like this into their JIT code with each version release. (E.g., the in-place operations that can sometimes take place for certain function call syntaxes.) Probably the only way to get any insight into it would be to do some timing and/or memory tests using very large arrays.

(That reminds me ... I need to finish testing & upload the new version of MTIMESX which allows in-place operations)

  1 Comment

Matt J
on 24 Jan 2013

I guess that's the thing to do. Well, the following experiment seems to confirm that temporary variables are created (in 2012a), the first time measurement being pretty close to the sum of the 3rd and 4th measurements.

   M=1e6; N=1;

Elapsed time is 0.043764 seconds.

    z=B*x; %memory allocated here

Elapsed time is 0.030972 seconds.


Elapsed time is 0.014324 seconds.

Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi test

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

MATLAB Academy

New to MATLAB?

Learn MATLAB today!