Finding series of values within array

4 visualizaciones (últimos 30 días)
Oscar
Oscar el 18 de Nov. de 2015
Respondida: Rajanya el 30 de En. de 2025 a las 7:12
Hi, I am up for a problem which I cannot totally solve.
Imagine I have an array: testtimes=[0.2 1.2 1.202 1.205 1.209 1.8 2.1 2.6 2.604 2.606 3.601 3.603 3.605]; Now I need to find all series of values where the difference between values is less than 0.01. So for now I did this:
if true
% code
end
% reference = 0.01
% isitesttimes=diff(testtimes); belowBIthreshold=(isitesttimes<reference);
% belowBIthreshold = [false, belowBIthreshold, false];
% edges = diff(belowBIthreshold);
% rising = find(edges==1); falling = find(edges==-1); spanWidth = falling - rising; wideEnough = spanWidth >= 3;
% startPos = rising(wideEnough);
% endPos = falling(wideEnough)-1;
% allInSpan = cell2mat(arrayfun(@(x,y) x:1:y, startPos, endPos, 'uni', false))
However, the answer now is
allInSpan = 2 3 4
Which means that on position 2,3,4 in testtimes the values are postioned which have a difference less than 0.01. However, the correct answer in this example would be 2,3,4,5 (since also position 5 has a difference of less than 0.01 compared to the one on position 4) (apart from that 8,9,10,11,12,13 should also be in the answer but that has to do (I guess) with the cut-off/Span-Width of 3).
Preferably I would get a variable which has per row the positions of the testtimes, so:
Answer = 2,3,4 8,9,10 11,12,13 (because between 10 and 11 there is more than 0.01 difference)
Can somebody help me out?
Regards
PS I know that there are more topics about this but I couldn't find the one which specifically adressess this question.

Respuestas (1)

Rajanya
Rajanya el 30 de En. de 2025 a las 7:12
Hi @Oscar,
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.

Categorías

Más información sobre Entering Commands en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by