why does transposing a sparse matrix change its memory requirements

6 Ansichten (letzte 30 Tage)
Hal Caswell
Hal Caswell am 6 Sep. 2012
I am dealing with a large sparse matrix. I need to turn it into a vector and then transpose it. Because the matrix is sparse, it takes up much, much less memory than it would in full mode. When I use (:) to turn it into a vector, it still takes up little memory. But when I transpose it, it eats up as much memory as it would if it was not sparse. Even though "whos" shows that it is sparse, and nnz() shows that it has the same number of non-zero elements.
Here is a piece of code that will generate the problem. It generates a sparse random matrix, here of dimension 400x400. Then it turns it into a vector, then transposes the vector, then calls whos to see the memory requirements. Compare the memory required for xs_vec and xs_vec_t.
x=randn(400,400); pick=find(x<2); x(pick)=0;
xs=sparse(x);
xs_vec=xs(:); xs_vec_t=xs_vec';
whos

Antworten (2)

Matt Fig
Matt Fig am 6 Sep. 2012
Yes, MATLAB uses compressed column storage, not compressed row storage. Notice:
>> x = sparse(zeros(1,10000));
>> whos x
Name Size Bytes Class Attributes
x 1x10000 80024 double sparse
>> x = sparse(zeros(10000,1));
>> whos
Name Size Bytes Class Attributes
x 10000x1 32 double sparse
Why do you need to use a row vector?

Hal Caswell
Hal Caswell am 7 Sep. 2012
Matt -- thanks. That explains the source of the problem. The row vector appears in a step of a calculation.
Let x be a nx1 sparse vector (column), where n is very large, and A be a nxm sparse matrix, where n>>m. The calculation needed is
y=x'*A
The result y will be 1 x m, which creates no memory problems.
I have tried two ways to do this. One was to create the transposed vector; that is where I discovered the problem that you explained. The other was to do the multiplication as written above without creating the transposed vector first. That freezes my system in a way that makes me suspect that Matlab is creating x', and overflowing memory, in the course of the multiplication, but I don't know if that's true. (I am working on a Mac, so there is no memory function to help investigate this).
I could transpose the whole expression, but that just transfers the memory problem from x to A.
Any ideas?

Kategorien

Mehr zu Sparse Matrices finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by