フィルターのクリア

interpolation vector with a lot of duplicate values

61 ビュー (過去 30 日間)
Stefano
Stefano 2014 年 6 月 3 日
コメント済み: Chad Greene 2017 年 9 月 24 日
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
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
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
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
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
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);

Stefano
Stefano 2014 年 6 月 3 日
Thank you Chad
I applied your suggestion and then
plot(x1,y1,x2,y2),'ro');
but it does not match.
  1 件のコメント
Chad Greene
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
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')

Stefano
Stefano 2014 年 6 月 3 日
Thank you very much Chad,
I don't understand the xrange's value
  1 件のコメント
Chad Greene
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.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeInterpolation についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by