- Cell array of character vectors. Example: {'A' 'B' 'C'}
- String array. Example: ["A" "B" "C"]
- Categorical array. categorical(["A" "B" "C"])
MATLAB engine for python - How to build graph with weights
4 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
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.
0 comentarios
Respuestas (1)
Vinayak Choyyan
el 12 de Abr. de 2023
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:
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']
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.
0 comentarios
Ver también
Categorías
Más información sobre Graph and Network Algorithms 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!