Align digraph EdgeCData with correct edges

5 ビュー (過去 30 日間)
Alex Wooten
Alex Wooten 2021 年 6 月 26 日
コメント済み: Alex Wooten 2021 年 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 Center および File ExchangeGraph 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