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

조회 수: 8 (최근 30일)
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
J. Alex Lee
J. Alex Lee 2020년 2월 3일
Then what is the rationale for rounding the 7.1 down to 7?
Might want to move your comment up to this thread
Paramonte
Paramonte 2020년 2월 3일
sorry I did a mistake: i want to keep 7.1. I edited to correct this. Cheeers

댓글을 달려면 로그인하십시오.

채택된 답변

Steven Lord
Steven Lord 2020년 2월 3일
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
J. Alex Lee
J. Alex Lee 2020년 2월 3일
Cool, never knew about uniquetol, but on looking at the doc, isn't there an ambiguity about which value (within a tolerance) is returned? If this can be used as answer to the original question, it suggests that uniquetol will always return the lowest value in the tolerance-group as "the unique" value...running the example, this appears true, and it does not appear to be a side effect of the original order of t. This decision doesn't seem like a unique obvious choice to me...I could imagine wanting the average of the tolerance-group, or the max...or in general wanting to apply some custom function.
Paramonte
Paramonte 2020년 2월 3일
I must thak Image Analist and Steven Lord for your time and effot.
Steven reply worked perfectely.
many thanks indeed

댓글을 달려면 로그인하십시오.

추가 답변 (2개)

Paramonte
Paramonte 2020년 2월 3일
Thakn you for your reply.
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
Image Analyst 2020년 2월 3일
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
Image Analyst
Image Analyst 2020년 2월 3일
Not until you give me the t and y you used. Because for the original ones, as in my code, it works beautifully.
Paramonte
Paramonte 2020년 2월 3일
you are right, only needed to transpode a vector. Cheers!!

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

제품


릴리스

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by