フィルターのクリア

Find number of zeros in a part of a matrix

2 ビュー (過去 30 日間)
Panouoilis
Panouoilis 2013 年 8 月 5 日
コメント済み: Jan 2018 年 3 月 16 日
I need to find the number of zeros that are contained in a part of a matrix. For example if the matrix is [1 2 2 2 2 2 0 2 2 2 0 3 0 0 4 5 6 7 0 0 8] is there a fast way to get the number of zeros between the fifth and tenth element? In this case the answer would be 1.
Thank you!

回答 (3 件)

Evan
Evan 2013 年 8 月 5 日
編集済み: Evan 2013 年 8 月 5 日
A = [1 2 2 2 2 2 0 2 2 2 0 3 0 0 4 5 6 7 0 0 8];
numZeros = sum(~A(5:10));

Image Analyst
Image Analyst 2013 年 8 月 5 日
Three ways that I can think of off the top of my head:
data = [1 2 2 2 2 2 0 2 2 2 0 3 0 0 4 5 6 7 0 0 8];
numberOfZeros = length(data) - nnz(data)
numberOfZeros = sum(data==0)
numberOfZeros = nnz(~data)
  1 件のコメント
Image Analyst
Image Analyst 2013 年 8 月 5 日
Note: this does the whole array because I think it's a dangerous assumption to look just between the 5th and 10th elements, which works well for your example but not in general.

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


Sarumathi C
Sarumathi C 2018 年 3 月 15 日
編集済み: Image Analyst 2018 年 3 月 15 日
How do I find the zeros locations in a array by using "for loop" and store in the new array matrix?
Example:
[1 2 3 0;
4 5 6 0;
7 8 0 9.....]
I need the output like
[ 4
4
3]
It's a zeros Location
If zeros location is same,we should find the difference between the two rows...
Like,
In the above Matrix we have a 4. & 4 there is zeros locations are same so we should find out the difference between that two rows like
4-1=3;
5-2=3....
So the output like
[3 3 3
7 8 0 9....]
  2 件のコメント
Image Analyst
Image Analyst 2018 年 3 月 15 日
Use find() and a for loop:
m=[1 2 3 0;
4 5 6 0;
7 8 0 9
4 0 0 7
0 5 0 0]
for row = 1 : size(m, 1)
zeroLocations{row} = find(m(row, :) == 0)
end
celldisp(zeroLocations)
Jan
Jan 2018 年 3 月 16 日
@Sarumathi C: Please open a new thread for a new question. This is better than hijacking an existing thread by attaching a new question in the section for answers. Thanks.

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

カテゴリ

Help Center および File ExchangeMatrix Indexing についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by