필터 지우기
필터 지우기

interpolation vector with a lot of duplicate values

조회 수: 54 (최근 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 CenterFile Exchange에서 Interpolation에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by