Based on your explanation and code, I understand that you are trying to find out all the elements in an array that have a difference from its consecutive element less than a specified reference value.
Since the 'diff' function calculates differences as x(i+1)-x(i), 'x' being an array and 'i' being an index; the 'endPos' in your code misses to capture index 5 (i.e. the last index of any valid series).
This is because the 'edges' variable marks index 5 'falling' (with value -1) as testtimes(6)-testtimes(5)>0.01. However, index 5 should still be a part of the series because testtimes(5)-testtimes(4)<0.01, 0.01 being the 'reference'.
Hence, there is no need to eliminate the ending index of 'falling'. This inherently forces the 'spanWidth' to change as well.
The following changes will make the code work as desired.
spanWidth = falling - rising + 1; %since the last element of 'falling' should always be added
endPos = falling(wideEnough) %no -1;
With the changes, the windows come as:
% 2 3 4 5
% 8 9 10
% 11 12 13
To know more about the working of 'diff' function, you can refer to the documentation page by executing the following command from MATLAB Command Window.
doc diff
Thanks.
