Azzera filtri
Azzera filtri

how to create undirected weighted graph with multiple sets of node labels and edge weights in MATLAB

6 visualizzazioni (ultimi 30 giorni)
Each edge of my graphs has multiple sets of weights. For example, 1) the distance between two nodes and 2) the correlation between these two nodes. Each node of my graphs also has multiple sets of features (attributes or labels). For example, 1) color, 2) acidity and so on.
I do not think the build-in function "graph" can do it. Do I then have to write my own code for it? Is there something in MATLAB that corresponds to networkx in python?

Risposta accettata

Li Xue
Li Xue il 11 Lug 2017
Hi Steven, many thanks for your prompt reply. This is exactly what I am looking for.

Più risposte (1)

Steven Lord
Steven Lord il 11 Lug 2017
You can add custom attributes to the nodes and edges in a graph or digraph object. The attribute Weight is considered "special" by some of the methods of graph or digraph (for example, shortestpath will use the Weight variable of the Edges table if it exists) but you could set that attribute to be the same as one of the other attributes before using a method that respects Weight.
% Make a random digraph using an adjacency matrix
rng default
A = rand(10) < 0.5;
D = digraph(A);
% Give the edges some weights
D.Edges.W1 = randi(10, numedges(D), 1);
D.Edges.W2 = randi(10, numedges(D), 1);
D.Edges.W3 = randi(10, numedges(D), 1);
% Because D.Edges has no Weight variable, shortestpath considers it to be unweighted
noweight = shortestpath(D, 1, 3)
% When we give the edges a Weight attribute, the shortest path changes
% The additional weight on the edges that made up the previous best path (noweight)
% make another route a shorter option
D.Edges.Weight = D.Edges.W3;
withweight = shortestpath(D, 1, 3)

Categorie

Scopri di più su Graph and Network Algorithms in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by