Align digraph EdgeCData with correct edges
6 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Alex Wooten
am 26 Jun. 2021
Kommentiert: Alex Wooten
am 30 Jun. 2021
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 Kommentare
Akzeptierte Antwort
Asvin Kumar
am 30 Jun. 2021
Bearbeitet: Asvin Kumar
am 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
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Graph and Network Algorithms finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!