Extract non-zero elements from matrix with double precision numbers
3 ビュー (過去 30 日間)
古いコメントを表示
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.
0 件のコメント
採用された回答
Azzi Abdelmalek
2013 年 5 月 15 日
out=M(M~=0)
1 件のコメント
Azzi Abdelmalek
2013 年 5 月 15 日
編集済み: Azzi Abdelmalek
2013 年 5 月 15 日
nonzeros(M)
also should work (nonzeros with s)
その他の回答 (1 件)
Matt Kindig
2013 年 5 月 15 日
編集済み: Matt Kindig
2013 年 5 月 15 日
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
0 件のコメント
参考
カテゴリ
Help Center および File Exchange で Resizing and Reshaping Matrices についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!