필터 지우기
필터 지우기

Calling python in MATLAB

조회 수: 2 (최근 30일)
Anthony Sirico
Anthony Sirico 2020년 11월 9일
답변: Abhisek Pradhan 2020년 11월 12일
In the below code, I have a Depth First Search with the pseudocode and the python code. My first question is how can I call this program in MATLAB?
I can use the following:
system('python DFS.py')
But I want to call it like:
py.DFS();
And store the data in MATLAB, but when I execute the code it says Unable to resolve the name py.DFS.
My second question is, how can I change the code in python to where it takes a MATLAB adjacency list and runs that in the code, instead of putting the adjacency list directly in the python code?
#DFS(G, u)
#u.visited = true
#for each v within G.adj[u]
#if v.visited == false
#DFS(G,v)
#init() {
#For each u within G
#u.visited == false
#For each u within G
#DFS(G,u)
#}
# DFS algorithm in Python
# DFS algorithm
def dfs(graph, start, visited=None):
if visited is None:
visited = set()
visited.add(start)
print(start)
for next in graph[start] - visited:
dfs(graph, next, visited)
return visited
graph = {'1': set(['2', '4', '5']),
'2': set(['1', '4', '3']),
'3': set(['2', '4', '6']),
'4': set(['1', '2', '3', '5', '6', '7']),
'5': set(['1', '4', '7', '8']),
'6': set(['3', '4', '7', '10']),
'7': set(['4', '5', '6', '8', '10']),
'8': set(['5', '7', '9']),
'9': set(['8', '10']),
'10': set(['6', '7', '9'])}
dfs(graph, '1')
n = len(graph)
s = pow(n, n-2) #number of spanning tree graphs
print("This is the number of spanning trees: ", s)
python
algorithm
matlab
graph-theory
depth-first-se

답변 (1개)

Abhisek Pradhan
Abhisek Pradhan 2020년 11월 12일
You could try using the Python interface introduced in MATLAB R2014b. You would need Python on your machine.
A few resources which might help:
And for using MATLAB workspace variables in Python refer the an example mentioned link below.

카테고리

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

제품


릴리스

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by