interpolation vector with a lot of duplicate values

26 visualizaciones (últimos 30 días)
Stefano
Stefano el 3 de Jun. de 2014
Comentada: Chad Greene el 24 de Sept. de 2017
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 comentario
Star Strider
Star Strider el 3 de Jun. de 2014
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.

Iniciar sesión para comentar.

Respuesta aceptada

José-Luis
José-Luis el 3 de Jun. de 2014
Editada: José-Luis el 3 de Jun. de 2014
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 comentarios
judy abbott
judy abbott el 21 de Sept. de 2017
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 el 24 de Sept. de 2017
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.

Iniciar sesión para comentar.

Más respuestas (4)

Chad Greene
Chad Greene el 3 de Jun. de 2014
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 el 3 de Jun. de 2014
Thank you Chad
I applied your suggestion and then
plot(x1,y1,x2,y2),'ro');
but it does not match.
  1 comentario
Chad Greene
Chad Greene el 3 de Jun. de 2014
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.

Iniciar sesión para comentar.


Chad Greene
Chad Greene el 3 de Jun. de 2014
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 el 3 de Jun. de 2014
Thank you very much Chad,
I don't understand the xrange's value
  1 comentario
Chad Greene
Chad Greene el 3 de Jun. de 2014
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.

Iniciar sesión para comentar.

Categorías

Más información sobre Interpolation en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by