- Calling Python from MATLAB documentation
- Calling custom Python modules (shows how to call multiple functions from a single Python file).
Calling python in MATLAB
1 visualización (últimos 30 días)
Mostrar comentarios más antiguos
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
0 comentarios
Respuestas (1)
Abhisek Pradhan
el 12 de Nov. de 2020
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.
0 comentarios
Ver también
Categorías
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!