Align digraph EdgeCData with correct edges
6 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Alex Wooten
el 26 de Jun. de 2021
Comentada: Alex Wooten
el 30 de Jun. de 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 comentarios
Respuesta aceptada
Asvin Kumar
el 30 de Jun. de 2021
Editada: Asvin Kumar
el 30 de Jun. de 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
Más respuestas (0)
Ver también
Categorías
Más información sobre Graph and Network Algorithms en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!