Detecting values in a vector that are different but very close to each other
Show older comments
Hello there:
I have a t vector (time, increasing values) like this:
t=[ 1 1.1 2 3 3.1 4.1 5 6 7.1 7.2]
to which corresponds y values
y=[ 10 12 10 9 1 12 12 4 9 12 ]
I would like to remove in x the values whose difference to the next one is <= 0.1, so I get a
t_new=[ 1 2 3 4.1 5 6 7.1] and then to make a corrspondenece to the new y, in a way that the y values correspondent to the similar x values are added, so:
y_new=[10+12 10 9+1 12 12 4 9+12]
Thanks in advance!!
Regards
3 Comments
J. Alex Lee
on 3 Feb 2020
To clarify, you want to keep 4.1 as 4.1, but you want to combine 7.1 and 7.2 into 7?
I think the problem is simpler if you declare you want to aggregate all into round number times, so that your new t will be
t_new = [1 2 3 4 5 6 7]
J. Alex Lee
on 3 Feb 2020
Then what is the rationale for rounding the 7.1 down to 7?
Might want to move your comment up to this thread
Paramonte
on 3 Feb 2020
Accepted Answer
More Answers (2)
Paramonte
on 3 Feb 2020
0 votes
Image Analyst
on 3 Feb 2020
This works:
t=[ 1 1.1 2 3 3.1 4.1 5 6 7.1 7.2]
y=[ 10 12 10 9 1 12 12 4 9 12 ]
dt = diff(t)
bigDiff = dt >= 0.11 % Change according to what you think is a big enough difference.
badIndexes = find(~bigDiff) + 1
goodIndexes = [1, find(bigDiff) + 1]
yCopy = y;
yCopy(badIndexes - 1) = yCopy(badIndexes - 1) + yCopy(badIndexes)
t_new = t(goodIndexes)
y_new = yCopy(goodIndexes)
Adapt as needed.
4 Comments
Image Analyst
on 3 Feb 2020
Note: I didn't check it for a run of 3 or more that are close together so you might need to adapt it for that if this code doesn't work in that situation.
Paramonte
on 3 Feb 2020
Image Analyst
on 3 Feb 2020
Not until you give me the t and y you used. Because for the original ones, as in my code, it works beautifully.
Paramonte
on 3 Feb 2020
Categories
Find more on Multirate Signal Processing in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!