Counting a groupe of zeros
Show older comments
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 Comment
Azzi Abdelmalek
on 5 May 2015
Give an example
Accepted Answer
More Answers (1)
Hooman Habibi
on 5 May 2015
0 votes
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)
Categories
Find more on Logical in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!