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

Learn moreOpportunities for recent engineering grads.

Apply Today**New to MATLAB?**

Asked by Martin
on 19 Jul 2013

Hi,

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?

Thank!

*No products are associated with this question.*

## 3 Comments

## Jan Simon

Direct link to this comment:http://www.mathworks.com/matlabcentral/answers/82546#comment_160594

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

## James Tursa

Direct link to this comment:http://www.mathworks.com/matlabcentral/answers/82546#comment_160684

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.

## Jan Simon

Direct link to this comment:http://www.mathworks.com/matlabcentral/answers/82546#comment_160750

@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: