How do I count the number of zeros in a matrix?

491 ビュー (過去 30 日間)
Franchesca
Franchesca 2014 年 4 月 18 日
コメント済み: Walter Roberson 2021 年 1 月 19 日
I have data imported from excel into a matrix and want to count the number of zeros in the file to work out the time.

採用された回答

Azzi Abdelmalek
Azzi Abdelmalek 2014 年 4 月 18 日
編集済み: Azzi Abdelmalek 2014 年 4 月 18 日
A=[0 0 1;0 2 4;7 8 1]
idx=A==0
out=sum(idx(:))
or
out=nnz(~A)
  3 件のコメント
rehan anwar
rehan anwar 2017 年 9 月 24 日
it will not works when matrice is [0 90 180]
Walter Roberson
Walter Roberson 2017 年 9 月 24 日
Azzi's code suggestions both output 1 for [0 90 180], which would appear to be the correct answer; what result were you expecting?

サインインしてコメントする。

その他の回答 (4 件)

Mischa Kim
Mischa Kim 2014 年 4 月 18 日
編集済み: Image Analyst 2021 年 1 月 14 日
Caroline, use
numberOfZeros = sum(data(:)==0)
  3 件のコメント
Gabriela Garcia
Gabriela Garcia 2021 年 1 月 19 日
Can this be modified so that it searches for the number of zeros in a specific column of the matrix? I want to see the number of zeros each column has.
Walter Roberson
Walter Roberson 2021 年 1 月 19 日
sum(data(:,COLUMN)==0)
If you want to know column-by-column, then
sum(data == 0, 1)
or most compactly (but less clear)
sum(~data,1)
If you are sure that there are more than one rows, then
sum(~data)
is even more compact.

サインインしてコメントする。


Image Analyst
Image Analyst 2014 年 4 月 18 日
The way I thought of is different still:
numberOfZeros = numel(data) - nnz(data);
Now how that allows you to "to work out the time", I have no idea. What do you mean by that? Is one of the rows or columns a time or date number?
  2 件のコメント
Franchesca
Franchesca 2014 年 4 月 20 日
I will end up calculating jump height however to do this I need to calculate the time each participant was off the force plate (so when the reading is 0) in one of the columns. How would I specify a column for matlab to count the number of zeros. Would it simply be:
numberOfZeros = numel(data(7,1)) - nnz(data);
Image Analyst
Image Analyst 2014 年 4 月 20 日
Nope. That's not a good idea. You need to count the number of zero stretches, not the number of zeros. Good thing you explained the larger context so we can see that what you asked for is probably not what you need. What if a person jumped 3 times: she jumped high the first time so that there were 3 zeros the first jump, 2 zeros at the second jump, and just a single zero for the third jump? Well that's 3 jumps, not 6 like you'd get if you counted zeros.
That's why it's important to give the context at the start so people don't give you wrong answers, like we all did up til now. Often a person asks very specifically "How do I do X?" and then later it becomes known that the person wanted outcome Z and thought X was the best approach. Then people realize that approach X is not the correct or best approach, and the poster should use approach Y. I think that's the situation here.
If you have the Image Processing Toolbox, you can simply count the number of jumps this way:
[labeledRegions, numberOfJumps] = bwlabel(data == 0);
If you don't have that toolbox you can count the number of "jump starts" by taking the difference and counting how many are more than 0.
diffs = diff(data); % data(k+1)-data(k);
jumpStarts = (diffs > 0) & data(1:end-1) == 0;
numberOfJumps = sum(jumpStarts);
or something like that.

サインインしてコメントする。


palaniraj p
palaniraj p 2017 年 11 月 22 日
x=[ 1 4 0; 0 3 3; 5 0 0]
nr=size(x,1);
nc=size(x,1);
xz=0;
for ir=1:nr
for ic=1:nc
if x(ir,ic)==0
xz=xz+1;
end
end
end
xz

Salam Ismaeel
Salam Ismaeel 2020 年 1 月 21 日
% for a vector
sum(A == 0)
% for a 2D matrix
sum(sum(A == 0)
  6 件のコメント
Walter Roberson
Walter Roberson 2020 年 1 月 21 日
In order to use your code, you need
if isvector(A)
out = sum(A == 0);
elseif ismatrix(A)
out = sum(sum(A == 0));
elseif ndims(A) == 3
out = sum(sum(sum(A == 0)));
elseif ndims(A) == 4
out = sum(sum(sum(sum(A == 0))));
else
error('sorry, code only handles up to 4 dimensions')
end
Where-as Mischa's code would be simply
out = sum(A(:) == 0);
which would handle all number of dimensions including more than 4 dimensions.
Under what circumstances do you see your code as being advantageous compared to Mischa's ?
Salam Ismaeel
Salam Ismaeel 2020 年 1 月 21 日
If you read my comments correctly, I said for a vector and for a 2D matrix only!

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeCreating and Concatenating Matrices についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by