Error Undefined Function 'shortestpath' for input arguments of type 'double'
2 views (last 30 days)
Show older comments
Waseem Hussain
on 24 Oct 2017
Commented: Walter Roberson
on 25 Oct 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 Comments
Star Strider
on 24 Oct 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.
Accepted Answer
Walter Roberson
on 24 Oct 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()
More Answers (1)
Waseem Hussain
on 25 Oct 2017
2 Comments
Steven Lord
on 25 Oct 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
on 25 Oct 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.
See Also
Categories
Find more on Graph and Network Algorithms in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!