MATLAB engine for python - How to build graph with weights

조회 수: 2 (최근 30일)
Amit
Amit 2023년 4월 8일
답변: Vinayak Choyyan 2023년 4월 12일
Hey,
When trying to build the simplest graph on matlab engine with python -
s1 = ['a', 'b', 'c', 'd'];
t1 = ['b', 'c', 'd', 'e'];
w1 = [10, 20, 30, 40];
g = eng.graph(s1,t1,w1);
I'm getting this error-
MatlabExecutionError Traceback (most recent call last)
~\AppData\Local\Temp/ipykernel_15984/3466945527.py in
8 t1 = ['b', 'c', 'd', 'e'];
9 w1 = [10, 20, 30, 40];
---> 10 g = eng.graph(s1,t1,w1);
11
12
c:\Users\amitz\anaconda3\lib\site-packages\matlab\engine\matlabengine.py in __call__(self, *args, **kwargs)
68 return FutureResult(self._engine(), future, nargs, _stdout, _stderr, feval=True)
69 else:
---> 70 return FutureResult(self._engine(), future, nargs, _stdout,
71 _stderr, feval=True).result()
72
c:\Users\amitz\anaconda3\lib\site-packages\matlab\engine\futureresult.py in result(self, timeout)
65 raise TypeError(pythonengine.getMessage('TimeoutCannotBeNegative'))
66
---> 67 return self.__future.result(timeout)
68
69 def cancel(self):
c:\Users\amitz\anaconda3\lib\site-packages\matlab\engine\fevalfuture.py in result(self, timeout)
80 raise TimeoutError(pythonengine.getMessage('MatlabFunctionTimeout'))
...
File C:\Program Files\MATLAB\R2022b\toolbox\matlab\graphfun\+matlab\+internal\+graph\constructFromEdgeList.m, line 256, in constructFromEdgeList
File C:\Program Files\MATLAB\R2022b\toolbox\matlab\graphfun\@graph\graph.m, line 325, in graph.graph
Graph edge weights must be double or single, real, and not sparse
I tried looking everywhere but didn't find the cause.
Really appreciate your assistance.

답변 (1개)

Vinayak Choyyan
Vinayak Choyyan 2023년 4월 12일
Hi Amit,
As per my understanding, you are trying to use the ‘graph()’ function when you are facing an issue with assignment of weight to the graph edges.
‘graph(s,t)’ accepts 2 types of input, namely ‘Node index’ and ‘Node name’. Further, ‘Node name’ type input accepts one of the following types of input for multiple nodes:
  • Cell array of character vectors. Example: {'A' 'B' 'C'}
  • String array. Example: ["A" "B" "C"]
  • Categorical array. categorical(["A" "B" "C"])
Also, in MATLAB, as shown below, the following code results to ‘abcd’ rather than creating an array of characters as you were expecting.
s1 = ['a', 'b', 'c', 'd']
s1 = 'abcd'
Hence you had only created 2 nodes with names ‘abcd’ and ‘bcde’ and were passing the function 4 edge weights instead of 1 edge weight.
s1 = ['a', 'b', 'c', 'd'];
t1 = ['b', 'c', 'd', 'e'];
g = graph(s1,t1);
plot(g)
What you might want to try out is the following,
s2 = {'a', 'b', 'c', 'd'};
t2 = {'b', 'c', 'd', 'e'};
g2 = graph(s2,t2);
plot(g2)
Now you can also assign the 4 weights to the edges respectively.
w1 = [10, 20, 30, 40];
g3 = graph(s2,t2,w1);
plot(g3)
I hope this resolves the issue you were facing.

카테고리

Help CenterFile Exchange에서 Graph and Network Algorithms에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by