Filter löschen
Filter löschen

Building a graph from a matrix

2 Ansichten (letzte 30 Tage)
L'O.G.
L'O.G. am 16 Mär. 2022
Kommentiert: L'O.G. am 17 Mär. 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?)

Akzeptierte Antwort

Matt J
Matt J am 16 Mär. 2022
Given an upper triangular matrix B
Isn't B your adjacency matrix? If so, then why not just,
G=graph(B);
  21 Kommentare
Matt J
Matt J am 16 Mär. 2022
Bearbeitet: Matt J am 16 Mär. 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. am 17 Mär. 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!

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Loops and Conditional Statements finden Sie in Help Center und File Exchange

Produkte


Version

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by