Dang Khoa
Dang Khoa

Reputation: 5823

How do I "stretch" (interpolate) an array but keep its underlying data intact?

I have two splines that I've generated with (a bit of modification of) getcurve() which represent an XY and XZ of a trajectory. I would like to combine the two splines together to generate a 3d plot, via plot3(). I modified getcurve() to output the XData and YData of the line drawn on the plot.

Since the splines are graphically generated, the number of elements won't necessarily be the same between XY and XZ. Suppose length(XY)>length(XZ). I'm trying to create a new vector XZ_2 that has the same length as XY and has the same x-values as XY.

My first idea was to interpolate as follows:

XZ_2(:,2) = interp1(XZ(:,1), XZ(:,2), XY(:,1))

but I get an error:

Error using griddedInterpolant
The grid vectors are not strictly monotonic increasing.
Error in interp1 (line 191)
    F = griddedInterpolant(X,V,method); 

The spline XZ looks like this:

XZ spline

I don't understand why I can't interpolate given this spline. It doesn't look like anything special. You can rebuild this spline yourself by doing the following (you'll need the Curve Fitting Toolbox):

>> xz = [0.0288    0.0518    0.1071    0.1763    0.2707    0.3583    0.4988    0.5864    0.7339    0.8191    0.9182    0.9781
         1.8070    1.3626    0.9766    0.4152   -0.0643   -0.3684   -0.9181   -1.1637   -1.4795   -1.6667   -1.8070   -1.9474];
>> fnplt(cscvn(xz));

Is there a way to "resize" XZ to use the same x-values as XY? I realize that some information about XZ will be lost when I do this, but that's okay.

Upvotes: 2

Views: 4316

Answers (1)

Serg
Serg

Reputation: 14108

it says that in interp1(x, y, xi);, the x and xi must be monotonic increasing, i.e. sorted.

Upvotes: 3

Related Questions