Implement edges weights from nodes variables

3 次查看(过去 30 天)
Hello everyone,
I have a digraph G (see attached file) , with all nodes having a certain value of the variable “TempsD”. I would like to implement an algorithm that for every out-edges of node (i) their weights are equal to the variable value “TempsD” of node (i). This should be done for all edges of G.
How can I do that?
Thanks in advance

回答(1 个)

Satyam
Satyam 2025-6-17
Hi Fabio,
According to my understanding, we can implement an algorithm that for every out-edge of a source node, assigns the weight equal to the value of the variable 'TempsD' for that node.
This problem can be solved using an algorithm which iterates through each edge, retrieves the name of the source node from the first entry in the 'EndNodes' pair, and then looks up the corresponding 'TempsD' value from the Nodes table. This value is then assigned as the new weight for the edge. After processing all edges, the updated weights are stored in the 'Weight' column of the 'Edges' table.
You can refer to the following documentation of ‘digraph’ to learn more about different properties and their appropriate syntax: https://www.mathworks.com/help/matlab/ref/digraph.html
Here is a sample code snippet demonstrating the above approach:
% Loop through each edge
for k = 1:height(G.Edges)
% Get source node name from EndNodes
sourceNode = G.Edges.EndNodes{k,1}; % First column is source in digraph
% Find corresponding TempsD value from Nodes
tempsD = G.Nodes.TempsD(strcmp(G.Nodes.Name, sourceNode));
% Assign the value to the weight
newWeights(k) = tempsD;
end
I hope this algorithm satisfies the query.

类别

Help CenterFile Exchange 中查找有关 Graph and Network Algorithms 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by