How to convert python code to Matlab?

615 vues (au cours des 30 derniers jours)
filippo gistri
filippo gistri le 9 Nov 2022
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)
  3 commentaires
filippo gistri
filippo gistri le 11 Nov 2022
I tried to run python on matlab, but it didn't work.
If i run, matlab return this error:
Error using python_utility><module>
Error in <frozen importlib>_call_with_frames_removed (line 241)
Error in <frozen importlib>exec_module (line 883)
Error in <frozen importlib>_load_unlocked (line 688)
Error in <frozen importlib>_find_and_load_unlocked (line 1006)
Error in <frozen importlib>_find_and_load (line 1027)
Error in <frozen importlib>_gcd_import (line 1050)
Error in __init__>import_module (line 126)
Rik
Rik le 11 Nov 2022
I don't work with the Python link enough to understand this error without seeing the code you tried.

Connectez-vous pour commenter.

Réponse acceptée

Aditya Jha
Aditya Jha le 16 Nov 2022
Hi!
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:

Plus de réponses (3)

rushabh rupani
rushabh rupani le 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 le 19 Avr 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 le 20 Juil 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:
pyrun('my_python_callback')
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
properties
open_env = py.gym.make('MountainCar-v0'); %% IMPORT PYTHON OPEN AI ENVIRONMENT
end
methods
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);
end
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;
end
end
function InitialObservation = reset(this)
result = this.open_env.reset();
InitialObservation = double(result)';
end
end
end
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.

Catégories

En savoir plus sur Call Python from MATLAB dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by