How can I select all the nonzero elements of a matrix and give out a matrix?

1 vue (au cours des 30 derniers jours)
How can I select all the nonzero elements of a matrix and give out a matrix?
MatrixAo = find(MatrixA(:)~=0)
This only gives the indices back... Is there any better command?
Thanks a lot!
  2 commentaires
José-Luis
José-Luis le 26 Oct 2012
And regarding your previous question: don't use globals, you will save someone (probably yourself) a world of hurt in the future.
Simon
Simon le 26 Oct 2012
I will remember this advice!

Connectez-vous pour commenter.

Réponse acceptée

Andrei Bobrov
Andrei Bobrov le 26 Oct 2012
MatrixAo = MatrixA(abs(MatrixA) > eps(100));
  4 commentaires
José-Luis
José-Luis le 26 Oct 2012
Because of numerical precision issues.
Andrei Bobrov
Andrei Bobrov le 26 Oct 2012
Hi, Jose! Yes, it as arbitrary, from my experience.

Connectez-vous pour commenter.

Plus de réponses (2)

José-Luis
José-Luis le 26 Oct 2012
Modifié(e) : José-Luis le 26 Oct 2012
your_mat = A(A~=0);
And if you want a sparse matrix:
your_mat = sparse(A);

Sachin Ganjare
Sachin Ganjare le 26 Oct 2012
Try this:
MatrixAo = MatrixA(MatrixA~=0)

Catégories

En savoir plus sur Logical dans Help Center et File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by