MATLAB: Counting a groupe of zeros

counting zeros

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

Best Answer

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