why does transposing a sparse matrix change its memory requirements

조회 수: 5 (최근 30일)
Hal Caswell
Hal Caswell 2012년 9월 6일
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

답변 (2개)

Matt Fig
Matt Fig 2012년 9월 6일
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?
  댓글 수: 1
Hal Caswell
Hal Caswell 2012년 9월 7일
hmm. Posted a reply to you, but I did it as a separate answer. Sorry about that.

댓글을 달려면 로그인하십시오.


Hal Caswell
Hal Caswell 2012년 9월 7일
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?

카테고리

Help CenterFile Exchange에서 Sparse Matrices에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by