How to convert python code to Matlab?

801 views (last 30 days)
Is there way to convert this python code to matlab code?
how can i convert python code to matlab???
this is the code that I want to convert:
import os
os.environ("KMP_DUPLICATE_LIB_OK") = "TRUE"; %%aggiungo una variabile ambiente
from sklearn.cluster import estimate_bandwidth
from sklearn import metrics
def estimate_bandwidth_meanshift(features, perc, quantile=0.5):
print('Start estimating bandwidth -------------------------------------')
bandwidth = estimate_bandwidth(features, quantile=quantile, n_samples = int(features.shape(0)*perc/100))
print('End estimating bandwidth ---------------------------------------')
return bandwidth
def getNMI(prediction, gt):
return metrics.normalized_mutual_info_score(gt, prediction)
def getARI(prediction, gt):
return metrics.adjusted_rand_score(gt, prediction)
Rik on 11 Nov 2022
I don't work with the Python link enough to understand this error without seeing the code you tried.

Sign in to comment.

Accepted Answer

Aditya Jha
Aditya Jha on 16 Nov 2022
There is no direct way to convert python code to MATLAB.
There are two approaches:
Refer to the following MATLAB answers post with similar issue:

More Answers (3)

rushabh rupani
rushabh rupani on 28 Jan 2023
Converting Python code to Matlab can be a complex process, as the two languages have different syntax and libraries.
You can use the Py2Mat library to accomplish your desired result.

Lekkalapudi Chandini
Lekkalapudi Chandini on 19 Apr 2023
% Plot the displacement versus time
plot(t, x(:, 1))
xlabel('Time (s)')
ylabel('Displacement (m)')
title('Displacement of Mechanical System')

Anoy Chowdhury
Anoy Chowdhury on 20 Jul 2023
Only some commands like
py.list({'This','is a','list'})
are recommendable to use from python. In the other hand, you can create a full funcion in python script / function and call it with:
However, trying to "convert" a python script line by line to MATLAB is pointless. To give you a context, is equivalent to try to convert to python some script which includes:
sys = armax(tt2,[na nb nc nk])
Since armax belongs to a toolbox (System Identification) it would be extremely complicated and unpractical.
If you want to use a python library as sklearn.cluster, stay in python, the only reason to import to MATLAB should be importing a python environment to interact with an MATLAB , for example an MATLAB AI agent:
classdef mountain_car_1 < rl.env.MATLABEnvironment
open_env = py.gym.make('MountainCar-v0'); %% IMPORT PYTHON OPEN AI ENVIRONMENT
function this = mountain_car_1()
ObservationInfo = rlNumericSpec([2 1]);
ObservationInfo.Name = 'MountainCar Descreet';
ObservationInfo.Description = 'Position, Velocity';
ActionInfo = rlFiniteSetSpec([0 1 2]);
ActionInfo.Name = 'Acceleration direction';
this = this@rl.env.MATLABEnvironment(ObservationInfo,ActionInfo);
function [Observation,Reward,IsDone,LoggedSignals] = step(this,Action)
result = cell(this.open_env.step(int16(Action)));
Observation = double(result{1})';
Reward = double(result{2});
IsDone = double(result{3});
LoggedSignals = [];
if (Observation(1)>=0.4)
Reward = 0;
IsDone = 1;
function InitialObservation = reset(this)
result = this.open_env.reset();
InitialObservation = double(result)';
In the previous example, if you had installed Python 3.10, and OpenAIGym, you can import the desired environment to MATLAB and interact both with the used and a potencial AI Bot.

Community Treasure Hunt

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

Start Hunting!