Question

In Matlab, we can do

x = -10:.1:10;
f = inline('normpdf(x,3,2) + normpdf(x,-5,1)','x');
t = plot(x,f(x))

Do we have a similar function like inline in Python?

Was it helpful?

Solution

I think the python equivalent of "Inline" would be lambda

 Matlab:
 f = inline('normpdf(x,3,2) + normpdf(x,-5,1)','x');

 python:
 f = lambda x : normpdf(x,3,2) + normpdf(x,-5,1)
 # Assuming that normpdf is defined and in scope ;-)

OTHER TIPS

Yes, in iPython notebook (and maybe Enthought Canopy?), you can set inline using the "magic function"

% pylab inline

You must restart the kernel for it to take effect (at least for iPython notebook versions anterior to 2.0)

You can use eval, which is a dangerous function (see e.g. http://nedbatchelder.com/blog/201206/eval_really_is_dangerous.html):

from numpy import *
x = np.arange(0, pi, 0.1)
f = eval('sin') # fill in any function
y = f(x)

or like assuming that the variable in your finction is always referred to as x

from numpy import *
x = np.arange(0, pi, 0.1)
y = eval('sin(x)')

that isn´t the same, because lambda does other thing, i just want replace values of my variable F in GRAD but using 'inline', this code is of matlab:

F= 3*X**2+2*X*Y+4*(Y-2)**2  
GRAD= inline([diff(F,X), diff(F,Y)])
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top