interpolation vector with a lot of duplicate values

Hi to all! Maybe my question might seem a little strange.
I have 3 vectors x1, y1, and x2
x1 and x2 have a lot of duplicate values, I would like to get y2 by interpolation with the same lenght of y1
y1 = [350 770 800 920 970 990 1020 1054 1080 1100];
x1=[10 10 11 14 13 12 10 10 10 7];
x2 = [10 10 13 13 15 13 13 10 10 10];
(actually have greater length, but always the same for all)
Is it impossible or a non-sense question? (in any case, my problem would remain unsolved) Thank you in advance

1 Comment

All your vectors are the same size, and duplicates in x2 and x2 aren’t problems with respect to interpolating them.
What do you want y2 to be? You haven’t told us, other than you want it to be the same length.

Sign in to comment.

 Accepted Answer

If there are several values for one coordinate, then you could, e.g. take the mean value of the abscissas for that coordinate and then perform a linear interpolation:
y1 = [350 770 800 920 970 990 1020 1054 1080 1100];
x1=[10 10 11 14 13 12 10 10 10 7];
x2 = [10 10 13 13 15 13 13 10 10 10];
[C,ia,idx] = unique(x1,'stable');
val = accumarray(idx,y1,[],@mean); %You could take something other than the mean.
your_vals = interp1(C,val,x2,'linear','extrap'); %see interp1() for other interpolation methods. Extrapolation is dangerous.
plot(x1,y1,'b*',x2,your_vals,'r+');

15 Comments

Thank you Josè-Luis, I tried your solution with my other vectors (longer of these) and in some cases the interpolation does not match. Do you have a suggest?
At: val = accumarray(idx,y1,[],@mean); what are others options about "@mean"
Well, you could take the minimum @min, the maximum @max, the median @median... whatever tickles your fancy. You should decide based on the type of data you have and what you want to achieve.
I don't know what you mean by it does not match. Maybe you could use something other than a linear interpolation between points. For that, please refer to the documentation.
doc interp1
The result of my linear-interpolation of others vectors with duplicate:
Well, it looks like you should take unique() values of the x values you use for interpolation and also sort them, otherwise your graph is bound to look like that.
I don't understand what you are trying to achieve, sorry.
I applied your code above (it take unique() and sort) at different vectors from the previous but they also have a lot of duplicate. I'm trying to active an adequate "your_vals" vector (by interpolation).
Thanks for your time.
No worries. Keep in mind that unique() returns sorted values, provided you don't use the 'stable' option.
Please accept an answer once your question has been solved.
Well, only with unique() in the following code:
[C,ia,idx] = unique(x1);
val = accumarray(idx,y1,[],@mean);
your_vals = interp1(C,val,x2,'linear'); %extrap is dangerous for my data
plot(x1,y1,x2,your_vals,'go');
I achieve this graph, that is the same of the previous:
I would to be sure that I have not made mistakes and there is not another solution
There are plenty of solutions. Which ones are reasonable depend on what your data is and what you are trying to achieve.
What I mean by unique() values is more like this (untested code):
your_x2 = unique(x2);
your_vals = interp1(C,val,your_x2,'linear');
plot(x1,y1,'b*',your_x2,your_vals,''b-');
I accept your answer Josè and I thank you for your time.
I don't understand how to use the last code (your_x2..) with the above.
Unfortunately I still have not solved my problem and I don't have ideas how to do
Did you try using it with the data you posted?
Yes, I tried the following code with the data that I posted:
[C,ia,idx] = unique(x1,'stable');
val = accumarray(idx,y1,[],@mean);
your_x2 = unique(x2);
your_vals = interp1(C,val,your_x2,'linear','extrap');
plot(x1,y1,'-',your_x2,your_vals','r+');
where 'your_vals' has only 3 elements ( due to unique(x2) ) but I need 10 as y1
If you want I could to post my real data, are 3 vectors of 50 elements, but I think that they would be enough 20 or 30
I you don't order your data, the line they draw is bound to look like that. If the problem is one of visualization then I would suggest you ditch the line and plot your results as points only.
I try to explain what I should to do with my vectors referring to the real data. I have four vectors: x1,y1 and x2,y2 where only y1,y2 are always monotonic.
x1 = [6 5 5 10 13 16 17 21 21 21 21 18 18 16 13 15 16 19 14 14];
y1 = [32 115 174 468 818 1067 1268 1399 1446 1484 1503 1588 1608 1665 1761 1879 1918 2037 2138 2148];
x2 = [4 4 3 12 12 24 22 17 17 18 20 16 13 12 12 12 13 15 18 18];
y2 = [32 49 137 782 791 1171 1255 1461 1471 1538 1683 1781 1860 1890 1910 1960 2102 2268 2467 2563];
I would like to relate x2 with y1 by two different linear interpolations.
The first interpolation:
y2interp = interp1(x1,y1,x2,'linear');
The second interpolation:
x2interp = interp1(y1,x1,y2interp,'linear');
then
plot(x1,y1,'b-',x2interp,y2interp,'ro');
Whereas that the vectors x1,x2 contain measures, in this case the new x2interp can not contain negative numbers or "NaN" therefore I can not to do linear interpolation with 'extrap'.
I have not been able yet to find a solution, I'm stuck at the first step of linear interpolation. Could I do also a different way to interpolate? Thank you in advance for your interest and attention.
Please how i can use the code on Matlab R2010
y1 = [350 770 800 920 970 990 1020 1054 1080 1100];
x1=[10 10 11 14 13 12 10 10 10 7];
x2 = [10 10 13 13 15 13 13 10 10 10];
[C,ia,idx] = unique(x1,'stable');
val = accumarray(idx,y1,[],@mean); %You could take something other than the mean.
your_vals = interp1(C,val,x2,'linear','extrap'); %see interp1() for other interpolation methods. Extrapolation is dangerous.
plot(x1,y1,'b*',x2,your_vals,'r+');
i get ??? Error using ==> unique Unrecognized option
Hi Judy, Just a heads up, you'll typically have better luck getting an answer to a question if you start a new question. Most people on the forum focus on the unanswered questions, so your comment at the bottom of a list of comments in the answer to a years-old question is likely to go unseen. If you still need an answer I suggest asking a new question.

Sign in to comment.

More Answers (4)

I don't think interpolation would be appropriate here. You could fit a best-fit line using a least-squares solution, then plug your x2 values into your equation. For example for a linear fit:
P = polyfit(x1,y1,1);
y2 = P(1)*x2 + P(2);
Thank you Chad
I applied your suggestion and then
plot(x1,y1,x2,y2),'ro');
but it does not match.

1 Comment

It cannot match exactly. You have 5 different values of y1 when x1 equals 10, so it's impossible to come up with a single accurate value of y2 when x2 equals 10. Least squares minimizes the errors. If a linear least squares solution is insufficient for your full data set, try quadratic, cubic, or higher.

Sign in to comment.

y1 = [350 770 800 920 970 990 1020 1054 1080 1100];
x1 = [10 10 11 14 13 12 10 10 10 7];
x2 = [10 10 13 13 15 13 13 10 10 10];
plot(x1,y1,'bo'); hold on
P = polyfit(x1,y1,1);
y2 = P(1)*x2 + P(2);
xrange = 7:15;
bestFit = P(1)*xrange + P(2);
plot(xrange,bestFit,'k-')
plot(x2,y2,'r*')
legend('y1','linear-fit','y2','location','southeast')
Thank you very much Chad,
I don't understand the xrange's value

1 Comment

I added the xrange so I could include the bestFit line. It's only to show that any x values you put in x2 will give y2 values along the black line.

Sign in to comment.

Categories

Find more on Interpolation in Help Center and File Exchange

Asked:

on 3 Jun 2014

Commented:

on 24 Sep 2017

Community Treasure Hunt

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

Start Hunting!