matlab out of memory error when multiplying 2 matrix

I have a X = 3*120744 double matrix and I am trying to calculate X'X but getting out of memory error. I have a system with 16G ram. So not sure what's going on. Any idea?

 Réponse acceptée

Stephan
Stephan le 5 Mar 2019
Modifié(e) : Stephan le 5 Mar 2019

1 vote

Hi,
consider:
X = ones(3,120744);
result_1 = X*X'; % --> Result = 3 x 3 Matrix (72 Byte)
result_2 = X'*X; % --> Result = 120744 x 120744 Matrix (108.6 GB)
Result 2 will throw error:
Requested 120744x120744 (108.6GB) array exceeds maximum array size preference. Creation of
arrays greater than this limit may take a long time and cause MATLAB to become
unresponsive. See array size limit or preference panel for more information.
Error in UASB (line 4)
result_2 = X'*X;
So buy more RAM or think about if you do the right matrix multiplication...
Best regards
Stephan

Plus de réponses (1)

With the specified dimensions, the result of X'*X will be size [120744 120744]. Assuming X is a real, full, double matrix that will require a contiguous block of memory of this size:
>> resultsize = [120744 120744];
>> numberOfElements = prod(resultsize);
>> bytes = 8*numberOfElements; % a real double requires 8 bytes
>> gb = bytes/(1024^3)
gb =
108.622860431671
You likely don't have a contiguous block of memory that large.
If you tell us more about the properties of X and why you're trying to compute X'*X (what your ultimate goal is) we may be able to suggest an alternative. If X is sparsely populated, for example, storing it as a sparse matrix may help.

Catégories

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by