MATLAB Answers


A few questions on memory management of sparse matrices...

Asked by Martin
on 19 Jul 2013


I have a bit of trouble with the space used by sparse matrices.

I use two ways of creating the same matrix, say P:

(1) allocating some space and filling the matrix like P(4,2) = 89; the space allocated at the beginning is exactly the space used.

(2) or creating 3 arrays of rows and columns indices and matrix values, and making P in just one command line.

What I observed: - when I create twice the same matrix with both methods, the space used in bigger for the first one (until half much more space).

- the second method was faster when I created one big matrix. but the first is faster when I create a relatively large cell array filled with many smaller sparse matrices. Actually, when using the first method, I receive this matlab warning (that advises me to use the second method) when I want to create the big sparse matrix but not when I create the cell arrays of matrices.

- with the second method, I obtain 3 arrays of 8MB but my matrix is 500MB!

I can't explain all these things. Could you?



Please post the code instead of tell what it is intended to do. Show us how you measure the timings and post a copy of the warning message you get. Then an answer requires less guessing.

Please post the new information by editing the question, not by hiding important information in an (pseudo-)answer or in a comment (which scrolls out of sight soon). Thanks

I agree with Jan ... we need to see the code. A simple expression like P(4,2) = 89 may cause the entire existing sparse matrix memory to be copied to new memory. So understanding exactly how you are doing the sparse matrix build is critical to the memory usage and timing issues.

@James: I had a discussion about why Matlab required more than 8GB free RAM to create q [1x1e9] UINT32 vector, although 4GB should be enough. The description as a text did not reveal the cause, but the seeing the code did:

uint32(1:1e9)          % 8GB used for the temporary double array! Better:
uint32(1):uint32(1e9)  % 4GB


No products are associated with this question.

0 Answers

Join the 15-year community celebration.

Play games and win prizes!

Learn more
Discover MakerZone

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

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!