Replacing a LOT of data by zero (in a matrix)

조회 수: 2 (최근 30일)
Herwig Peters
Herwig Peters 2011년 8월 31일
I have a matrix M1, below you see the whos information for that matrix:
Name Size Bytes Class Attributes M1 29624x29624 108387640 double sparse
I want to replace quite a bit of data by zeros, using logical indexing: M1(abs(M1)<1e-1) = 0;
This needs more than 16GB memory, there must be a more efficient way. I also tried the other way round, that is M1 = M1(abs(M1)>1e-1);
Same problem. What's the trick?

채택된 답변

Herwig Peters
Herwig Peters 2011년 8월 31일
4 way.
Find the indices of the desired matrix elements and the desired matrix elements themselves. With these 3 vectors, rebuild the sparse Matrix M1.
[i1,~,s1] = find(M1(abs(M1)>1e-1));
[i1,j1] = ind2sub(size(M1),i1);
M1 = sparse(i1,j1,s1,m,n);
  댓글 수: 2
Fangjun Jiang
Fangjun Jiang 2011년 8월 31일
Nicely done! +1
Herwig Peters
Herwig Peters 2011년 8월 31일
Actually, I just found it can be done even nicer:
M1 = (abs(M1)>1e-1).*M1;

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

추가 답변 (1개)

Dmitry Borovoy
Dmitry Borovoy 2011년 8월 31일
1 way. Use vectorization (your way). fast but requires a lot of RAM 2 way. Use loops. Very slow but no need a lot of RAM 3 way. For perverts. Write your code on C/C++/Fortran with loop and call it from Matlab. If you have restriction on RAM or on calculation speed, I suppose, it's your way.
  댓글 수: 1
Herwig Peters
Herwig Peters 2011년 8월 31일
Yes, that sums it up pretty well. 1 needs too much RAM, 2 needs too much time (CPU) and 3 needs too much time as well, my time.
See my own answer for another possibility.

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

카테고리

Help CenterFile Exchange에서 Matrix Indexing에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by