MATLAB: I have large matrix and i want to find the number of consecutive zeros if there is more than 10 disply(hole) and give the index of the start and end of this vector of zeros,

10 consecutive zerosconsecutive elementssequence

x=[1 0 1 4 8 4 3 5 9 0 0 0 0 0 0 0 0 0 0 0 0 0 4 4 0 0 0 0 2 4 4 4 4 0 0 0 0 0 0 0 0 0 3 5 6 3 0 0 0 0 4 4 4 0 0 0 0 5 4 1 0 0 0 0 0 4 6 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 9 ]
how can i find the start and end index of sequence of zeros if the length more than 10?
x=[1 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 2 2 0 0 0 1 1 0 0 0 0 4 4 ];
c=0;k=0;z=0;
for i=1:length(x);
if x(i)==0;
c=c+1;
else
k=k+x(i);
if c>10;
disp(c);
disp('hole');
else
c=0;
end
end
end

Best Answer

x=[1 0 1 4 8 4 3 5 9 0 0 0 0 0 0 0 0 0 0 0 0 0 4 4 0 0 0 0 2 4 4 4 4 0 0 0 0 0 0 0 0 0 3 5 6 3 0 0 0 0 4 4 4 0 0 0 0 5 4 1 0 0 0 0 0 4 6 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 9 ]
transitions = find(diff([1, x ~= 0, 1]));
runstarts = transitions(1:2:end);
runends = transitions(2:2:end) - 1;
runlengths = runends - runstarts + 1;
tokeep = runlengths >= 10;
runstarts = runstarts(tokeep)
runends = runends(tokeep)