Error Undefined Function 'shortestpath' for input arguments of type 'double'
10 ビュー (過去 30 日間)
古いコメントを表示
Waseem Hussain
2017 年 10 月 24 日
コメント済み: Walter Roberson
2017 年 10 月 25 日
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()
その他の回答 (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!