Error Undefined Function 'shortestpath' for input arguments of type 'double'
2 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Waseem Hussain
am 24 Okt. 2017
Kommentiert: Walter Roberson
am 25 Okt. 2017
So I'm working on this code that needs to find the shortest distance between 2 points. I have labelled all the distances as 1 for now but when I run
path = shortestpath (DG,1,2)
it returns with the error - undefined function......
This is my code
W = [1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1]
DG = sparse ([1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 2 3 8 9 4 3 18 17 1 2 3 4 5 8 9 18 17 20 19 18 13 12 17 16 3 4 23 23 23 23 22 22 22 22], [2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 1 19 18 13 12 7 8 13 14 19 18 13 8 7 12 11 14 15 2 3 8 9 10 13 14 7 6 1 2 19 20 9 10 11 12], W)
path = shortestpath (DG,1,2)
Any help would be appreciated
2 Kommentare
Star Strider
am 24 Okt. 2017
You need to create a digraph object firsty.
However, when I do:
DG = digraph(DG);
path = shortestpath (DG,1,2)
I get this error message:
Error using digraph (line 214)
Adjacency matrix must be square.
I leave that for you to sort.
Akzeptierte Antwort
Walter Roberson
am 24 Okt. 2017
shortestpath() is defined only for graph and digraph objects. You need to use graph() or digraph() to create a graph before you can shortestpath()
Weitere Antworten (1)
Waseem Hussain
am 25 Okt. 2017
2 Kommentare
Steven Lord
am 25 Okt. 2017
From the documentation for sparse: "S = sparse(i,j,v) generates a sparse matrix S from the triplets i, j, and v such that S(i(k),j(k)) = v(k). The max(i)-by-max(j) output matrix has space allotted for length(v) nonzero elements."
If you want S to be larger than [max(i) max(j)] in size, you can use sparse to do that as well. Read through the remainder of the documentation for sparse for a description of that syntax.
Walter Roberson
am 25 Okt. 2017
The 23 is max() of the two sets of indices you give to sparse -- the maximum node number. You can add a couple of lines of code that would calculate it, but since you are already hard-coding all the node number information you might as well hard-code the 23 as well (that is, better would be if you had assigned the indices to variables in different statements.
Siehe auch
Kategorien
Mehr zu Graph and Network Algorithms finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!