How can I select all the nonzero elements of a matrix and give out a matrix?
Show older comments
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!
Accepted Answer
More Answers (2)
your_mat = A(A~=0);
And if you want a sparse matrix:
your_mat = sparse(A);
Categories
Find more on Logical in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!