Borrar filtros
Borrar filtros

Building a graph from a matrix

2 visualizaciones (últimos 30 días)
L'O.G.
L'O.G. el 16 de Mzo. de 2022
Comentada: L'O.G. el 17 de Mzo. de 2022
Given an upper triangular matrix B that is ordered by the atoms in each molecule such that the atoms in molecule 1 come first, then molecule 2, etc., I want to check if any atoms in any two molecules meet some criteria. Then, I want to designate an edge between those two molecules, but being careful not to count more than one edge between the same two molecules. A molecule can form edges with many other molecules, though. How do I do this efficiently?
Something like the following?
N = 200; % number of molecules
G = graph;
G = addnode(G,N);
C = ((B < 1.75) & (B > 0));
% Here I'm not sure how to efficiently determine the molecules that meet those criteria
G = addedge(G,a,b); % where a and b are all molecules a and b (I guess I need a double loop?)

Respuesta aceptada

Matt J
Matt J el 16 de Mzo. de 2022
Given an upper triangular matrix B
Isn't B your adjacency matrix? If so, then why not just,
G=graph(B);
  21 comentarios
Matt J
Matt J el 16 de Mzo. de 2022
Editada: Matt J el 16 de Mzo. de 2022
OK, well, then in addition to C, you need an additional input which is a label vector, L, stating which molecule the atoms belong to. For the simplified example, this would be,
L=[1 1 2 2 3 3 4 4];
and adapting my approach from above leads to,
S=sparse(L,1:numel(L),1);
A=S*C*S'>0;
A(1:size(A,1)+1:end)=0; %molecule adjacency matrix.
plot(graph(A))
In an earlier comment, you predicted that there would be additional edges 1-3 and 1-4, but I think that must have been an error. There is no adjacency, according to your provided C matrix, between the atoms of molecules 1 and 3 or 1 and 4.
L'O.G.
L'O.G. el 17 de Mzo. de 2022
Thank you! Can you please explain the reason for A=S*C*S'>0; ? I will accept your answer in a few seconds -- thank you for your patience / walking me through this!

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Loops and Conditional Statements en Help Center y File Exchange.

Productos


Versión

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by