Effacer les filtres
Effacer les filtres

Extract non-zero elements from matrix with double precision numbers

2 vues (au cours des 30 derniers jours)
Peter
Peter le 15 Mai 2013
How can I extract all of the non-zero number from a matrix? I tried using the function nonzero(M), but since the values in the matrix are all decimals, I got the error "Undefined function 'nonzero' for input arguments of type 'double' ".
I just wand a row vector (or column vector, or whatever is easiest) of all non-zero elements. Obviously I can just loop through each element, but this is insanely inefficient.

Réponse acceptée

Azzi Abdelmalek
Azzi Abdelmalek le 15 Mai 2013

Plus de réponses (1)

Matt Kindig
Matt Kindig le 15 Mai 2013
Modifié(e) : Matt Kindig le 15 Mai 2013
notZeros= M(M~=0);
Keep in mind that due to numerical precision, some of your matrix elements may be small but not identically zero. You might want to use some sort of tolerance instead, such as:
notZeros = M(abs(M)<10*eps); %for example

Catégories

En savoir plus sur Logical 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