translating commands from python

3 visualizaciones (últimos 30 días)
HC98 el 31 de En. de 2020
Editada: Stephen23 el 31 de En. de 2020
So I'm trying to translate the following code from Python:
def chabrier03individual(m):
k = 0.158 * exp(-(-log(0.08))**2/(2 * 0.69**2))
return numpy.where(m <= 1,\
0.158*(1./m) * exp(-(log(m)-log(0.08))**2/(2 * 0.69**2)),\
And end up stuck with an incorrect if statement:
% Initial Conditions
close all
m = logspace(-2, 2, 400);
% m = 1E2:100:1E4
% (m<0.08, m**-0.3, numpy.where(m < 0.5, 0.08**-0.3 * (m/0.08)**-1.3, 0.08**-0.3 * (0.5/0.08)**-1.3 * (m/0.5)**-2.3))
%% def chabrier03individual(m):
k = 0.158*exp(-(-log(0.08))^2/(2*0.69^2))
if m<=1
u = 0.158*(1./m)*exp(-(log(m)-log(0.08))^2/(2 * 0.69^2));
v = k*m;
Where am I going wrong?
  2 comentarios
Geoff Hayes
Geoff Hayes el 31 de En. de 2020
HC98 - m appears to be an array with 400 elements, so what are you expecting to happen with
if m<=1
Is this logic that you want to apply to each element of m? Also, why is u used if m<=1 but v used for the else?
HC98 el 31 de En. de 2020
the expectation is that this function is applied to values of m less than 1.

Iniciar sesión para comentar.

Respuestas (1)

Stephen23 el 31 de En. de 2020
Editada: Stephen23 el 31 de En. de 2020
The equivalent to numpy's where is to use indexing:
x = ...;
y = ...;
idx = m<=1;
z = y;
z(idx) = x(idx)
Using if is a red herring (it would require a loop, which is a waste of MATLAB).


Más información sobre Call Python from MATLAB en Help Center y File Exchange.


Community Treasure Hunt

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

Start Hunting!

Translated by