Align digraph EdgeCData with correct edges
6 views (last 30 days)
Show older comments
I am working with an adjacency matrix which I am displaying in a digraph, and I have a corresponding matrix that contains additional information about my data, which I would like to use for the edge colors. The problem is, the EdgeCData property expects a vector, and in the conversion, the data is no longer aligned properly.
A = [1 1 0 0 0; 0 0 0 0 1; 0 1 0 0 1; 1 0 0 0 0; 0 0 1 1 0]; %adjacency matrix
CData = [.85 .1 0 0 0; 0 0 0 0 .2; 0 .5 0 0 .65; .3 0 0 0 0; 0 0 .45 1 0]; %color data
CDataVec = nonzeros(CData); %convert to vector for EdgeCData property (misaligns data)
colormap jet
caxis([0 1])
G = digraph(A);
DG = plot(G);
DG.EdgeCData = CDataVec;
c = colorbar;
My understanding is that adjacency matrices map onto digraphs such that the row ID is directed to the column ID (eg data in row 4, column 1 of adjacency matrix 'A' would have node 4 connecting to node 1 in the digraph). The edge color I would like in this case is 0.3 (since that is the value at row 4, column 1 in CData) but instead it is 1. How can I get all of the color data to line up with the proper edges?
0 Comments
Accepted Answer
Asvin Kumar
on 30 Jun 2021
Edited: Asvin Kumar
on 30 Jun 2021
You need to transpose the CData matrix before passing it into nonzeros.
nonzeros generates a list of non-zero elements by traversing the input 2D matrix in column-major form. However, the digraph generates a list of edges from the adjacency matrix in row-major form.
Try this code:
A = [1 1 0 0 0; 0 0 0 0 1; 0 1 0 0 1; 1 0 0 0 0; 0 0 1 1 0]; %adjacency matrix
CData = [.85 .1 0 0 0; 0 0 0 0 .2; 0 .5 0 0 .65; .3 0 0 0 0; 0 0 .45 1 0]; %color data
CDataVec = nonzeros(CData.'); %convert to vector for EdgeCData property
colormap jet
caxis([0 1])
G = digraph(A);
DG = plot(G);
DG.EdgeCData = CDataVec;
c = colorbar;
% Compare the order of the following two arrays:
CDataVec
G.Edges.EndNodes
More Answers (0)
See Also
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!