# Detecting values in a vector that are different but very close to each other

12 views (last 30 days)
Paramonte on 3 Feb 2020
Commented: Paramonte on 3 Feb 2020
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]
Regards
Paramonte on 3 Feb 2020
sorry I did a mistake: i want to keep 7.1. I edited to correct this. Cheeers

Steven Lord on 3 Feb 2020
Use uniquetol to unique-ify the data with a tolerance. uniquetol can return a vector of indices that indicate to which of the unique values each original value corresponds. Then use accumarray to accumulate the corresponding values of the second vector together.
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 ];
[t2, ~, ind2] = uniquetol(t, 0.11);
ynew = accumarray(ind2, y);
uniqueItemsWithValues = [t2.', ynew]
##### 2 CommentsShow 1 older commentHide 1 older comment
Paramonte on 3 Feb 2020
I must thak Image Analist and Steven Lord for your time and effot.
many thanks indeed

Paramonte on 3 Feb 2020
Yes I want to keep the 4.1 since the next valu is 5 so, 5-4.1=0.9 which is over 0.1

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.
goodIndexes = [1, find(bigDiff) + 1]
yCopy = y;
t_new = t(goodIndexes)
y_new = yCopy(goodIndexes)
Paramonte on 3 Feb 2020
you are right, only needed to transpode a vector. Cheers!!

### Categories

Find more on Get Started with MATLAB in Help Center and File Exchange

R2019a

### Community Treasure Hunt

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

Start Hunting!