필터 지우기
필터 지우기

Number of zeros in a matrix

조회 수: 13 (최근 30일)
Bob Choy
Bob Choy 2012년 11월 17일
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
Azzi Abdelmalek
Azzi Abdelmalek 2012년 11월 17일
Why did you need to do it with if condition?
Bob Choy
Bob Choy 2012년 11월 17일
Because thats what I usually work with but if thats impossible nevermind it.

댓글을 달려면 로그인하십시오.

채택된 답변

Matt Fig
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
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)
Bob Choy
Bob Choy 2012년 11월 18일
Thank you so much! Keep up the good work, cheers!

댓글을 달려면 로그인하십시오.

추가 답변 (2개)

Andrei Bobrov
Andrei Bobrov 2012년 11월 17일
a - your matrix
number_columns = find(sum(~a) > 12);

Walter Roberson
Walter Roberson 2012년 11월 18일
numel(M) - nnz(M)

카테고리

Help CenterFile Exchange에서 Characters and Strings에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by