Number of zeros in a matrix
19 ビュー (過去 30 日間)
古いコメントを表示
I need to create a function where I have a matrix and the function returns the number of zeros in that matrix. I need to do this using the if condition.
Then write in a .txt file which columns had more than 12 zeros.
Can someone help me out with this?
Thank you very much.
2 件のコメント
採用された回答
Matt Fig
2012 年 11 月 17 日
編集済み: Matt Fig
2012 年 11 月 17 日
The number of zeros in the matrix A is:
sum(~A(:))
So we can make this a function:
f = @(x) sum(~x(:));
Now test it:
x = [1 2 3;0 0 0;3 0 9]
f(x)
5 件のコメント
Matt Fig
2012 年 11 月 18 日
M = [1 0 1 1;1 1 1 0]
fid = fopen('mytext.txt','wt')
fprintf(fid,'%i ',find(sum(~M) >= 1))
fclose(fid)
その他の回答 (2 件)
参考
カテゴリ
Help Center および File Exchange で Logical についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!