How can I translate this Python code to Matlab
显示 更早的评论
def findthepath(graph):
length = len(graph)
adj = list()
for i in range(length):
adj.append(sum(graph[i]))
startpoint = 0
odd = 0
for i in range(length - 1, -1, -1):
if (adj[i] % 2 == 1):
odd += 1
startpoint = i
if (odd > 2):
print("No Solution")
return
stack = list()
path = list()
start = startpoint
while (stack != [] or sum(graph[start]) != 0):
if (sum(graph[start]) == 0):
path.append(start + 1)
start = stack.pop(-1)
else:
for i in range(length):
if graph[start][i] == 1:
stack.append(start)
graph[start][i] = 0
graph[i][start] = 0
start = i
break
for element in path:
print(element, "-> ", end='')
print(start + 1)
'''
graph3 = [[0, 1, 0, 0, 1],
[1, 0, 1, 1, 1],
[0, 1, 0, 1, 0],
[0, 1, 1, 0, 1],
[1, 1, 0, 1, 0]]
findthepath(graph3)
'''
回答(1 个)
Ameer Hamza
2018-5-9
0 个投票
You don't need to translate it to MATLAB. You can directly execute python scripts from MATLAB: https://www.mathworks.com/help/matlab/getting-started-with-python.html. Especially refer to this section .
类别
在 帮助中心 和 File Exchange 中查找有关 Call Python from MATLAB 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!