2

I am trying to convert a MATLAB code to Python where I am stuck of how to import this line to Python:

YDFA_xa_p = interp1(data(:,1),data(:,2),YDFA_lam_p*1e9,'linear')*1e-24;

Now for Python I have changed it as:

YDFA_xa_p = numpy.interp(data[:, 1], data[:, 2], YDFA_lam_p * 1e9) * 1e-24

data[:,1] and data[:,2] and YDFA_lam_p values are:

[ 2.  2.  2.  2.  2.  2.  2.  2.  2.  2.] [ 3.  3.  3.  3.  3.  3.  3.  3.  3.  3.] 915.0

The issue I see is that the variable YDFA_lam_p is a float variable while it is expecting an array of float of 10 elements?

If I am correct in my understanding how can I correct it ? I tried ways I find in google but it just isn't working.

1
  • 1
    Can you post some sample data? The Docs say that the result has the same length as the given interpolation range. Commented Dec 29, 2014 at 13:50

1 Answer 1

3

When I use the same sort of numbers in Octave I get a similar error:

octave:32> interp1([2,2,2,2],[3,3,3,3],900)
warning: interp1: multiple discontinuities at the same X value
error: mkpp: at least one interval is needed

You've given it one point (repeatedly) and are asking it to interpolate some value way off in left field.

A correct sample use is:

octave:32> interp1([1,2,3,4,5],[3,3.5,2,2.5,1],2.33,'linear')
ans =  3.0050

the equivalent Python (note different order of variables):

In [364]: np.interp(2.33,[1,2,3,4,5],[3,3.5,2,2.5,1])
Out[364]: 3.005

Read help(np.interp) to see more about its inputs.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.