Finding series of values within array
2 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
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:
% 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).
Can somebody help me out?
Regards
Risposte (1)
Stephen23
il 12 Gen 2016
Modificato: Stephen23
il 12 Gen 2016
There is no need to use find when logical indexing is much faster. The following code extracts all values from X where the difference with an adjacent element is less than R:
>> V = [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];
>> R = 0.01;
>> D = diff(V)<R;
>> X = [false,D] | [D,false];
>> V(X)
ans =
1.2000 1.2020 1.2050 1.2090 2.6000 2.6040 2.6060 3.6010 3.6030 3.6050
But of course you can still use find if you need to:
>> find(X)
ans =
2 3 4 5 8 9 10 11 12 13
0 Commenti
Vedere anche
Categorie
Scopri di più su Logical in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!