Align digraph EdgeCData with correct edges

6 次查看(过去 30 天)
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?

采纳的回答

Asvin Kumar
Asvin Kumar 2021-6-30
编辑:Asvin Kumar 2021-6-30
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
CDataVec = 8×1
0.8500 0.1000 0.2000 0.5000 0.6500 0.3000 0.4500 1.0000
G.Edges.EndNodes
ans = 8×2
1 1 1 2 2 5 3 2 3 5 4 1 5 3 5 4
  1 个评论
Alex Wooten
Alex Wooten 2021-6-30
Great thank you for the in-depth explanation! I was hoping the answer would be something simple like that.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Graph and Network Algorithms 的更多信息

产品


版本

R2021a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by