MATLAB: How to find out the time interval between two consecutive events

cell arrays

sir, i need to find out the time interval between two consecutive events. i have around 4000 data with increasing numbers but not consecutive. i have to find out the events of 4 consecutive numbers or more and the gap between two consecutive events. how is it possible?

Best Answer

This would work:
A = [1,2,5,6,7,8,9,20,21,22,30,31,32,33, 34,35, 40,41,42,43,44];
runs = diff(A) == 1; %which numbers are part of a run
edges = diff([0 runs 0]); %find edges of run (1 = start, -1 = end)
startruns = find(edges == 1); %get indices of start of runs == indices in A
endruns = find(edges == -1); %get indices of end of runs == indices in A
lengthruns = endruns - startruns; %get lengths of runs
startruns = startruns(lengthruns >= 4); %only keep runs of 4 or more

endruns = endruns(lengthruns >= 4); %only keep runs of 4 or more
groupdiff = A(startruns(2:end)) - A(endruns(1:end-1))
Related Question