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

3 vues (au cours des 30 derniers jours)
Dimitris
Dimitris le 25 Juil 2011
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 commentaire
Fangjun Jiang
Fangjun Jiang le 25 Juil 2011
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?

Connectez-vous pour commenter.

Réponse acceptée

Fangjun Jiang
Fangjun Jiang le 25 Juil 2011

Plus de réponses (1)

Florin Neacsu
Florin Neacsu le 25 Juil 2011
Try :
ind1=A~=0;
B(ind1)=A(ind1);
Regards, Florin

Catégories

En savoir plus sur Matrix Indexing dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by