About interp1 function

Hi,
i have a data set like shown in figure1,
I try to create a new interpolate data between each sequential two data points with interp1 (one point for each sequential two data points) and the result are given in figure 2. Although i have 100 data the interpolated data are 57. It have to be 99.
It is obviously shown from figure 2 that it did not calculate interpolate data for sequential two data points. Especially sides do not have any interpolated data. What will be the solution
Thank you..
Vxi = min(Vx):0.1:max(Vx);
Vyi = interp1(Vx,Vy,Vxi, 'nearest');
plot(Vxi,Vyi, 'o')
FIGURE 1
FIGURE 2

7 Comments

Attach the data...not at all the type of data interp1 is designed to handle; frankly I'm surprised it didn't error.
You didn't ask for "a new interpolate data between each sequential two data points" but for the value of the nearest existing point at the input points.
Don't know what the actual min/max values are, but if I just take a guess from the plot scaling:
>> vxi=-3.3:0.1:2.3;
>> whos vxi
Name Size Bytes Class Attributes
vxi 1x57 456 double
>>
which matches the 57 points you got. Don't know what 100 points you had in mind, but linespace would let you spread n points over the range but again, note that "between successive points" in interp1 is probably going to draw lines between the upper and lower branches because interp1 requires the x-input vector be strictly monotonically increasing.
Jan
Jan on 23 Jul 2019
The code does exactly what it is expected to do. You select 57 X values between min(Vx) and max(Vx) and choose the nearest y value as output. The data are interpreted as a curve, which switches between the two values.
I do not understand, what you want to achieve instead. Do you want to handle the data points as border in 2D? This would not be a 1-dimensional interpolation, but it would work in 2D.
Thank you for your helps. But i realized that the logic behind my calculation should change. I should find the coordinates of point which is anintersection of two line.The first line is from O (center) to X, the other line is from * to neighbor *. I try to show it on picture. I need to find yellow points for all shape.
sr.jpg
dpb
dpb on 24 Jul 2019
What defines "x"?
I had wondered where that circle came from -- didn't look possible to have been generated by the code presented.
All "x" and "*" are coordinates (x,y). The shape is a cross section. Actually the full code is like given below. The code that i wrote above is a sample code to realize the interpolation. I tried it on my current code according to my variable names but it did not work. On the other hand as i sad in last comment i realized that i do not need interpolation code. I should find the intersection of lines..
clc;clear;
x=xlsread('ping1.xlsx', 'A:A');
y=xlsread('ping1.xlsx', 'B:B');
z=xlsread('ping1.xlsx', 'C:C');
a=xlsread('ping2.xlsx', 'A:A');
b=xlsread('ping2.xlsx', 'B:B');
c=xlsread('ping2.xlsx', 'C:C');
xyz=[x y z];
abc=[a b c];
rng(1);
[idx1,C1] = kmeans(xyz,100,'distance','sqEuclidean','MaxIter',500, 'Replicates', 10);
[idx2,C2] = kmeans(abc,100,'distance','sqEuclidean','MaxIter',500, 'Replicates', 10);
for i=1:100
t(i)=i
end
t=t';
for i=1:100
u(i)=i;
end
u=u';
[dist,idx3] = pdist2(xyz, C1, 'euclidean', 'Smallest',1);
newVar = xyz(idx3 ,:);
plot(newVar(:,1),newVar(:,3), 'bx');
text(newVar(:,1)+0.02,newVar(:,2),newVar(:,3),num2str(t),'FontSize',7);
hold on;
xlabel ('x - axis', 'fontsize', 12);
ylabel ('y - axis', 'fontsize', 12);
zlabel ('z - axis', 'fontsize', 12);
grid
[dist2,idx4] = pdist2(abc, C2, 'euclidean', 'Smallest',1);
newVar2 = abc(idx4 ,:);
plot(newVar2(:,1),newVar2(:,3), 'r*');
hold on;
%text(newVar2(:,1),newVar2(:,2),newVar2(:,3),num2str(u),'FontSize',7);
axis equal;
newVar3 = mean (newVar);
newVar4 = mean (newVar2),
newVar5 = (newVar3 + newVar4)/ 2;
plot(newVar5(:,1),newVar5(:,3), 'go');
You can simplify
for i=1:100
t(i)=i
end
t=t';
to the loop-free:
t = (1:100).';
Maybe it is worth to mention, that the data used to draw the diagram in the original question seem to be a 1D line. I assume they are ordered according to their X values, and not according to the outer shape, which is nearly a rectangle.
Without the input files I cannot run your code. So I still do not know, what your inputs are and what you want as output.
Actually, input data is 3D then i used Kmeans alg. and then get 2D data for newVar ("x") and newVar2 ("*") variable, and newVar5 (O) is the mean of newVar and newVar2. They are all 2D coordinates. "X" forms the orginal cross section data of a rectangular shape, "*" is the deformed shape of same rectangular cross section. I try to find the deformation of original data ("X"). So i wanted to draw a line between O-X and * to neighbour * and then find the intersection point of these lines.And i wanted to do this for all cross section. This intersection points give me the where the original data ("X") go in second cross section.
Also you can find the data in attachment.
Thank you...

Sign in to comment.

Answers (1)

Roshni Garnayak
Roshni Garnayak on 5 Aug 2019

0 votes

The interp1 function performs 1D interpolation and computes one y-value for the corresponding x-value. Due to this only one point is computed in the x : x+0.1 range even when a number of data points are clustered in that range.
A possible solution is to use variable interval size for Vxi. The intervals where more data points are located can be assigned smaller interval size and the intervals with lesser number of points can be allotted larger interval size.

Categories

Find more on Interpolation in Help Center and File Exchange

Tags

Asked:

on 22 Jul 2019

Answered:

on 5 Aug 2019

Community Treasure Hunt

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

Start Hunting!