Is there more efficient way to find the neighbors of two nodes or more other than using a loop?
Afficher commentaires plus anciens
The built in function "neighbors" accepts only scalar inputs, so I wonder if there is more efficient way than performing the following
for ii = 1:length(inActive)
b = neighbors(G_dmg,inActive(ii));
end
I usually concatenate the neighbors of all nodes in one variable, but I cannot tell which nodes are neighbors to node X and which are neighbors to node Y.
b = [];
for ii = 1:length(inActive)
b = [b, neighbors(G_dmg,inActive(ii))];
end
Your ideas would be highly appreciated.
8 commentaires
Christine Tobler
le 17 Juin 2021
Could you say some more about what your next steps with variable b will be? You'd like to know for each element of b which node it was neighboring - so in which way would that be most convenient to use in the next step?
Also, here b could contain some nodes multiple times, for example if both inActive(1) and inActive(2) have the same node as a neighbor. Would this be a problem for your next steps? You could use unique(b) to remove any duplicates, but that would make it harder to also define which node any element of b was the neighbor of.
Waseem AL Aqqad
le 17 Juin 2021
Christine Tobler
le 17 Juin 2021
So something like this?
for ii=1:numnodes(G)
if isinf(G.Nodes.Load(ii))
G.Nodes.Load(ii) = mean(G.Nodes.Load(neighbors(G, ii)));
end
end
With the difference that you want to traverse the nodes not as 1, 2, 3, ..., numnodes(G), but instead start with the node that has most active neighbors (neighbors with non-Inf value)?
That is, if the first node has all active neighbors, its load will become some value that's not Inf. Otherwise, it will stay Inf, since the mean of a set of values that contains Inf is again Inf.
Do I understand this correctly?
Waseem AL Aqqad
le 17 Juin 2021
Modifié(e) : Waseem AL Aqqad
le 17 Juin 2021
Waseem AL Aqqad
le 17 Juin 2021
Modifié(e) : Waseem AL Aqqad
le 17 Juin 2021
Christine Tobler
le 17 Juin 2021
Modifié(e) : Christine Tobler
le 17 Juin 2021
All right, I think I understand it all. This seems like a realistic approach to me, yes, although I have no knowledge of load-based fault graphs.
To compute the number of neighbors with Inf loads, you can use the following code, which might be faster than a loop over all neighbors:
rng default;
G = graph(rand(5) > 0.3, 'upper', 'omitselfloop');
load = [-Inf; -Inf; 5; -Inf; 9];
p = plot(G);
highlight(p, load ~= -Inf)
A = adjacency(G);
nrActiveNeighbors = A * (load ~= -Inf)
nrNeighbors = degree(G)
ratioActive = nrActiveNeighbors ./ nrNeighbors
You could then sort by ratioActive and do the above loop using them.
Of course, you could also consider updating the nrActiveNeighbors every time you turn a node back to active, and to then find the maximum of ratioActive given this change. Hard to say which would be better there; after all the original ratioActive is telling us the originally active nodes.
Waseem AL Aqqad
le 17 Juin 2021
Modifié(e) : Waseem AL Aqqad
le 17 Juin 2021
Rubel Ahmed
le 23 Juin 2021
Modifié(e) : KSSV
le 23 Juin 2021
You can try Knnsearch() from Matlab
Réponses (0)
Catégories
En savoir plus sur Nearest Neighbors dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
