how to take the non zero values from one matrix and put them in another??

조회 수: 3 (최근 30일)
I have a matrix [640 640] with few non zero values and many zeros. I want to put all the non zero values in a new matrix. Something like:
for i=1:640
for j=1:640
if A(i,j)~=0
B(i,j)=A(i,j)
end
end
end
But this doesn't work.. Any suggestions?
  댓글 수: 1
Fangjun Jiang
Fangjun Jiang 2011년 7월 25일
What do you mean doesn't work. There is an easy way to do it which uses logic index. What value do you want for other elements in B?

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

채택된 답변

Fangjun Jiang
Fangjun Jiang 2011년 7월 25일

추가 답변 (1개)

Florin Neacsu
Florin Neacsu 2011년 7월 25일
Try :
ind1=A~=0;
B(ind1)=A(ind1);
Regards, Florin

카테고리

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