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

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 Kommentare

And regarding your previous question: don't use globals, you will save someone (probably yourself) a world of hurt in the future.
I will remember this advice!

Melden Sie sich an, um zu kommentieren.

 Akzeptierte Antwort

MatrixAo = MatrixA(abs(MatrixA) > eps(100));

4 Kommentare

eps(100) strikes me as arbitrary. Is there any reason you picked that value?
Hi. thanks for your answer. As Jose Luis already asked: Why did you picked the abs and eps?
Because of numerical precision issues.
Hi, Jose! Yes, it as arbitrary, from my experience.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (2)

José-Luis
José-Luis am 26 Okt. 2012
Bearbeitet: José-Luis am 26 Okt. 2012
your_mat = A(A~=0);
And if you want a sparse matrix:
your_mat = sparse(A);

Kategorien

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by