フィルターのクリア

Counting a groupe of zeros

1 回表示 (過去 30 日間)
Nick van der maat
Nick van der maat 2015 年 5 月 5 日
コメント済み: Image Analyst 2015 年 5 月 5 日
Hello,
I am trying to count a group of zeros with a for loop.
But it keeps giving me the error Index must be a positive integer or logical.
Stops=0;
for i=1:size(TachographVehicleSpeed)
if (TachographvehicleSpeed(i)==0&&TachographvehicleSpeed(i-1)~=0)
Stops = Stops + 1;
end
end
  1 件のコメント
Azzi Abdelmalek
Azzi Abdelmalek 2015 年 5 月 5 日
Give an example

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

採用された回答

Image Analyst
Image Analyst 2015 年 5 月 5 日
If you want to count groups of zeros, like the number of groups and number of zeros in each group, you can use regionprops() if you have the Image Processing Toolbox.
m = [1 0 0 0 1 1 0 0 1 0 0 0 0]; % Sample data
[labeledArray, numberOfZeroGroups] = bwlabel(m == 0);
stats = regionprops(labeledArray, 'Area');
zerosInEachGroup = [stats.Area]
Results:
numberOfZeroGroups = 3
zerosInEachGroup =
3 2 4
  2 件のコメント
Nick van der maat
Nick van der maat 2015 年 5 月 5 日
Thanks ! this works just fine!
Image Analyst
Image Analyst 2015 年 5 月 5 日
You're welcome. If this answers your question best, then maybe you can officially "Accept" the answer and/or vote for it.

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

その他の回答 (1 件)

Hooman Habibi
Hooman Habibi 2015 年 5 月 5 日
The index starts from zero so you must start the loop from i=2. you can also count the consecutive zeros in row vector z by the following one liner
numberOfZerosInZ=sum(abs([z 1])+abs([1 z])==0)

カテゴリ

Help Center および File ExchangeGet Started with MATLAB についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by