Convert Python Code to MATLAB Code?

조회 수: 8 (최근 30일)
L
L 2018년 11월 27일
댓글: dany katamba mpoyi 2022년 6월 11일
How can I convert Python code to MATLAB code? I want to convert the approximate entropy Python implementation on wikipedia to MATLAB:
Approximate Entropy:
import numpy as np
def ApEn(U, m, r):
def _maxdist(x_i, x_j):
return max([abs(ua - va) for ua, va in zip(x_i, x_j)])
def _phi(m):
x = [[U[j] for j in range(i, i + m - 1 + 1)] for i in range(N - m + 1)]
C = [len([1 for x_j in x if _maxdist(x_i, x_j) <= r]) / (N - m + 1.0) for x_i in x]
return (N - m + 1.0)**(-1) * sum(np.log(C))
N = len(U)
return abs(_phi(m+1) - _phi(m))
I would like to run the code without having to install Python, so I would like to convert the algorithm into MATLAB language. Thank you!
  댓글 수: 1
L
L 2018년 11월 27일
Sample Entropy Wikipedia includes the MATLAB version of the code. However, when I run it on my dataset, I get NaN for my result.

댓글을 달려면 로그인하십시오.

답변 (1개)

Aymane Amouri
Aymane Amouri 2021년 5월 5일
"
import numpy as np
from lmfit import Minimizer, Parameters, report_fit
# create data to be fitted
x = np.linspace (0, 15, 301)
data = (10. * np.sin (2 * x - 0.1) * np.exp (-x * x * 0.025) +
np.random.normal (size=len (x), scale=0.2))
# define objective function: returns the array to be minimized
def fcn2min(params, x, data):
""" model decaying sine wave, subtract data"""
amp = params['amp']
shift = params['shift']
omega = params['omega']
decay = params['decay']
model = amp * np.sin (x * omega + shift) * np.exp (-x * x * decay)
return model - data
# create a set of Parameters
params = Parameters ( )
params.add ('amp', value=1, min=0)
params.add ('decay', value=0.1)
params.add ('shift', value=0.0, min=-np.pi / 2., max=np.pi / 2)
params.add ('omega', value=3.0)
# do fit, here with leastsq model
minner = Minimizer (fcn2min, params, fcn_args=(x, data))
kws = {'options': {'maxiter': 10}}
result = minner.minimize ( )
# calculate final result
final = data + result.residual
# write error report
report_fit (result)
# try to plot results
try:
import pylab
pylab.plot (x, data, 'k+')
pylab.plot (x, final, 'r')
pylab.show ( )
except:
pass
pylab.plt.figure( )
"
j'ai besoin de transformer ce code python to matlab code

카테고리

Help CenterFile Exchange에서 Call Python from MATLAB에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by