Error Undefined Function 'shortestpath' for input arguments of type 'double'
5 次查看(过去 30 天)
显示 更早的评论
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 个评论
Star Strider
2017-10-24
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.
采纳的回答
Walter Roberson
2017-10-24
shortestpath() is defined only for graph and digraph objects. You need to use graph() or digraph() to create a graph before you can shortestpath()
5 个评论
更多回答(1 个)
Waseem Hussain
2017-10-25
2 个评论
Steven Lord
2017-10-25
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
2017-10-25
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.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Graph and Network Algorithms 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!