how to find blocks of values within array where difference is less than X for a specific number of values without losing the last value?
1 visualización (últimos 30 días)
Mostrar comentarios más antiguos
Oscar
el 19 de Nov. de 2015
Respondida: Oscar
el 19 de Nov. de 2015
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 for at least three testtimes in a row. 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 because I don't know how to solve that also position 5 is in the answer?
Regards
PS I know that there are more topics about this but I couldn't find the one which specifically adressess this question.
0 comentarios
Respuesta aceptada
Thorsten
el 19 de Nov. de 2015
The code computes the differences between positions such that the final position is dropped. Just add it, using y+1:
allInSpan = cell2mat(arrayfun(@(x,y) x:1:y+1, startPos, endPos, 'uni', false))
0 comentarios
Más respuestas (4)
Ingrid
el 19 de Nov. de 2015
if you also want the last value why don't you just write
endPos = falling(wideEnough);
instead of what you currently have
endPos = falling(wideEnough)-1;
to solve your other problem of not giving the last two arrays you need to put
wideEnough = spanWidth >= 2;
just check the output of belowBIthreshold to see why this will give you three consecutive threshold agreements
0 comentarios
Thorsten
el 19 de Nov. de 2015
The code computes the differences between positions such that the final position is dropped. Just add it, using y+1:
allInSpan = cell2mat(arrayfun(@(x,y) x:1:y+1, startPos, endPos, 'uni', false))
0 comentarios
Ver también
Categorías
Más información sobre Entering Commands en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!