Clear Filters
Clear Filters

Error Undefined Function 'shortestpath' for input arguments of type 'double'

10 views (last 30 days)
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
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.
Waseem Hussain
Waseem Hussain on 25 Oct 2017
Yeah I've been getting the problem from the start aswell!, I thought I'd use shortest path but like you said there needs to be some sort of graph.
No idea how to fix adjacency matrix must be square

Sign in to comment.

Accepted Answer

Walter Roberson
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()
  5 Comments
Waseem Hussain
Waseem Hussain on 25 Oct 2017
And last question is there a way to reposition nodes, I want it to reposition so that it resembles an extended net of a cube

Sign in to comment.

More Answers (1)

Waseem Hussain
Waseem Hussain on 25 Oct 2017
Could you explain to me where 23,23 came from on the last line aswell because I have to write up a report
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, 23, 23)
  2 Comments
Steven Lord
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
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.

Sign in to comment.

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!