interpolation vector with a lot of duplicate values
56 次查看(过去 30 天)
显示 更早的评论
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 个评论
Star Strider
2014-6-3
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.
采纳的回答
José-Luis
2014-6-3
编辑:José-Luis
2014-6-3
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 个评论
judy abbott
2017-9-21
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
Chad Greene
2017-9-24
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.
更多回答(4 个)
Chad Greene
2014-6-3
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);
0 个评论
Stefano
2014-6-3
1 个评论
Chad Greene
2014-6-3
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.
Chad Greene
2014-6-3
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')

0 个评论
Stefano
2014-6-3
1 个评论
Chad Greene
2014-6-3
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.
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!