How can i do that list for dijkstra algorithm?
9 次查看(过去 30 天)
显示 更早的评论
Hello, I have to optmizate a route using dijkstra algorithm. I'll import data from an excel archive like the following image (but with more data):

ID is a location and Value the distance between locations. From 1 to 2 the distance is 3, from 5 to 6 the distance is 4 and from 4 to 7 the distance its 1
I have to transform this data into a matrix to use in the dijkstra algorithm like this:
W = [3 3 4 5 4 1 2 1 1 3 1 4 2 1 2 1 3];
A = [3 1 1 1 1 2 3 4 5 6 7 8 2 3 4 6 7];
B = [6 2 3 4 5 6 7 7 8 9 9 9 3 4 5 7 8];
Example for the image:
W: [3 4 3 4 3 2 2 3 4 1 3 2] - Distances (Using Values data)
A: [1 2 4 5 7 8 1 2 3 4 5 6] - Original Location (Using ID in from left to right to the first 6 values and then up to down to the following 6 values)
B: [2 3 5 6 8 9 4 5 6 7 8 9] - Destinated Location (Using ID in from left to right to the first 6 values and then up to down to the following 6 values)
So, using the first column we have: Distance 3, from 1 to 2. Second column: Distance 4, from 2 to 3...
How can I automate that?
0 个评论
回答(1 个)
Ameer Hamza
2020-10-31
编辑:Ameer Hamza
2020-10-31
You can use MATLAB's graph() object to do such a thing
W = [3 4 3 4 3 2 2 3 4 1 3 2];
A = [1 2 4 5 7 8 1 2 3 4 5 6];
B = [2 3 5 6 8 9 4 5 6 7 8 9];
G = graph(A, B, W);
Plot the graph()
plot(G)
Find the shortest path
shortestpath(G, 1, 9) % shortest path between 1 and 9
2 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Dijkstra algorithm 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!