I'm facing problem with graph and digraphs in matlab
1 次查看(过去 30 天)
显示 更早的评论
Hello everyone, My question is how can we plot graphs using nodes and edges(streets in my case), while I am doing the same it keeps on showing the followingerror....can anyone show me how can I rectify the same....![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/206073/image.jpeg)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/206073/image.jpeg)
6 个评论
Rama Sidhardha Yeleti
2019-2-28
Hello Sir, Thanks for the help, This is what it is showing while I'm trying to plot the graph between nodes and edges of the road network....please help me to sort this out![Screenshot (10).png](https://www.mathworks.com/matlabcentral/answers/uploaded_files/206222/Screenshot%20(10).png)
![Screenshot (10).png](https://www.mathworks.com/matlabcentral/answers/uploaded_files/206222/Screenshot%20(10).png)
Rama Sidhardha Yeleti
2019-2-28
%This is the script that I used for plotting the graph between the nodes and edges and while doing that
%it keeps on saying the same error that I mentioned in the above screenshots.
r = shaperead('Export_Output.shp');
kk = {{r.coor_x_sam},{r.coor_y_sam}}
kk = kk.';
ss = shaperead('Export_Output_3.shp');
sd = {{ss.START_X},{ss.END_X}}
sd = sd.';
dm = graph(kk,sd);
plot(dm);
Stephan
2019-2-28
Ok,
can you provide the results of this code which are in your workspace? Please save them as .mat-file and attach them. I do not have access to Mapping Toolbox and if i had it would be needed to see the .shp-files. But more important is what you have as variables in workspace.
Rama Sidhardha Yeleti
2019-3-1
Sir, below attached is the varaibles in workspace that are generated while running that code please check once.
采纳的回答
Walter Roberson
2019-3-1
kk = [r.coor_x_sam; r.coor_y_sam].';
sd = [ss.START_X; ss.END_X] .';
However, these are two different sizes: 2021 entries compared to 1044 entries. Neither one of them contains integers, so neither of them contains vertices indexes. Neither one of them is an adjacency matrix or mass matrix. The units appear to be incompatible: it looks to me as if coor_x_sam is degrees whereas START_X appears to be UMC, and coor_x appears to be UMC. You also are not taking into account any Y coordinates.
I think what you have is map data, not graph data.
Now, it happens to be possible to match coor_x and coor_y against START_X and START_Y, indicating that it would be possible to convert into node numbers to construct a graph; it would then be possible to associate X Y coordinates with each node when you plotted, but that can be a bit of a nuisance.
20 个评论
Rama Sidhardha Yeleti
2019-3-1
Yes sir, this is map data and it's not graph data. I'm trying to find the shortest route between any two nodes. For that I'm trying to import that into matlab and plot that as a graph in the matlab.But while doing that the above mentioned errors are araising.
Also you said that both are of different sizes yes both are of different size because one is node and the other is edges(for every edge there will be two nodes that is why the size will be different). I'll be very thankful if you suggest me the same.
Rama Sidhardha Yeleti
2019-3-1
Ok sir,will be waiting for your write and please do that as soon as possible because that is very urgent.
Walter Roberson
2019-3-1
When you have "urgent" questions, you should hire a consulting company that can commit to offering you responses within your contracted timeline. I am a one-person organization, and I am assisting a number of other people at the same time, and I need to sleep and eat and relax. I do not offer urgent assistance on a volunteer basis, only on a commercial basis, and I charge a lot for it.
Walter Roberson
2019-3-1
load workspace
SX = [ss.START_X];
SY = [ss.START_Y];
SXY = [SX(:), SY(:)];
EX = [ss.END_X];
EY = [ss.END_Y];
EXY = [EX(:), EY(:)];
CX = [r.coor_x];
CY = [r.coor_y];
CXY = [CX(:), CY(:)];
[tf, startidx] = ismembertol(SXY, CXY, 'byrows',true);
assert(all(tf), 'Some start points do not match r data');
[tf, endidx] = ismembertol(EXY, CXY, 'byrows', true);
assert(all(tf), 'Some end points do not match r data');
numc = length(CX);
dm = graph(startidx, endidx, 1, numc);
plot(dm, 'XData', CX, 'YData', CY);
A lot of the roads do not go anywhere, just connecting two points that have no other connections.
Rama Sidhardha Yeleti
2019-3-6
Sir, I'm very much thankful for your write but I'm just a student presently pursuing my bachelor's degree so I'm unable to hire any consulting company.
Walter Roberson
2019-3-6
Then don't get yourself into situations where you need urgent programming assistance from volunteers . Volunteers work on their own time when they feel up to it and when it amuses them to do so.
Rama Sidhardha Yeleti
2019-3-6
%Sir, i have tried using the code for plotting the graph but it doesnot gets
%plotted.Once please check this.I have also attached the workspace for any
%reference.
d = shaperead('Export_Output_nodedata.shp');
km = {{d.coor_x},{d.coor_y}}
km = km.';
sm = shaperead('combined_shapefile.shp');
sl = {{sm.START_X},{sm.END_X}}
sl = sl.';
SX = [sm.START_X];
SY = [sm.START_Y];
SXY = [SX(:), SY(:)];
EX = [sm.END_X];
EY = [sm.END_Y];
EXY = [EX(:), EY(:)];
CX = [d.coor_x];
CY = [d.coor_y];
CXY = [CX(:), CY(:)];
[tf, startidx] = ismembertol(SXY, CXY, 'byrows',true);
assert(all(tf), 'Some start points do not match d data');
[tf, endidx] = ismembertol(EXY, CXY, 'byrows', true);
assert(all(tf), 'Some end points do not match d data');
numc = length(CX);
dm = graph(startidx, endidx, 1, numc);
plot(dm, 'XData', CX, 'YData', CY);
Rama Sidhardha Yeleti
2019-3-7
Sir, below attached is the zip file containing the shapefiles.Have a look at them.
Walter Roberson
2019-3-7
The first of those files does not have coor_x or coor_y properties, but you can probably use X and Y instead.
The second of those files does not exist. If you substitute the combined_multi.shp file that you attached, then START_x and START_Y and END_X and END_Y properties are not present. X and Y properties are present, but they cannot be substituted for this purpose because the file does not define lines (lines need start and end locations.)
Rama Sidhardha Yeleti
2019-3-8
yes sir, I got it and now can we use any algorithms(like dijkstra's algorithm) for finding the distance between any two nodes?
Walter Roberson
2019-3-8
With these files, you do not have any connection information between nodes.
With the data you had posted first, hardly any of the nodes were connected.
Once you have the x and y coordinates of endpoints of roads, then you can use Euclidean distances:
DXY = sqrt(sum((SXY - EXY).^2,2))
Then instead of
dm = graph(startidx, endidx, 1, numc);
use
dm = graph(startidx, endidx, DXY, numc);
After that you can use https://www.mathworks.com/help/bioinfo/ref/graphallshortestpaths.html to find the graph distance between any two nodes, keeping in mind that most of it will be empty because you have very connected nodes in your graph.
Finding the shortest path between the nodes is unfortunately more work.
Rama Sidhardha Yeleti
2019-3-11
编辑:Rama Sidhardha Yeleti
2019-3-11
Sir, what I'm asking is will there be any possibility of running the code for dijkstra's algorithm on this graph for finding the shortest path between any two nodes. It's not just dijkstra (can I be able to use kruskal or any other algorithm for finding the same)
If that can be done can you please tell me how will i be able to implement those algorithms on the data that I have and where can I find the codes for those algorithms to find the shortestpaths between any two nodes.
Sir, also you are telling that these nodes are not connected to edges(I couldnot understand this point)but by seeing the graph many of the nodes are connected to the edges(I have attached the graph below) also in the data that i provided(2nd time) has x and y coordinates as (START_X,START_Y and END_X,END_Y) once please go through them.
![Screenshot (12).png](https://www.mathworks.com/matlabcentral/answers/uploaded_files/207802/Screenshot%20(12).png)
Walter Roberson
2019-3-11
Rama Sidhardha Yeleti
2019-3-11
Sir, is it only applicable for the adjacency matrix which is square matrix or we can apply this for any(in my case) the adjacency matrix is not a square marix.
Walter Roberson
2019-3-11
Adjacency matrices can always be extended to be square by padding with zeros along the shorter side.
Rama Sidhardha Yeleti
2019-3-12
Sir, while applying the all-pairs-shortest-path-graph-solver it is not running and it is out with various errors can you give a try on that ?
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Directed Graphs 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!发生错误
由于页面发生更改,无法完成操作。请重新加载页面以查看其更新后的状态。
您也可以从以下列表中选择网站:
如何获得最佳网站性能
选择中国网站(中文或英文)以获得最佳网站性能。其他 MathWorks 国家/地区网站并未针对您所在位置的访问进行优化。
美洲
- América Latina (Español)
- Canada (English)
- United States (English)
欧洲
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
亚太
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)