Clear Filters
Clear Filters

Using Centrality to color code path lengths of any two nodes

3 views (last 30 days)
Hi,
I am using documentation from the Centrality function to try and replicate the results from the Minnesota example code but applied to a random plot of nodes and edges. I am trying to find the betweenness centrality of all nodes of the randomly generated plot and color code the plot based on the betweenness of the two nodes. What I am trying to replicate (or similar) is found here.
When trying to color code the nodes based on centrality, the error
% Unable to perform assignment because dot indexing is not supported for variables of this type.
%
% Error in PlayingWithNodes (line 24)
p.NodeCData = ucc; % Unable to perform assignment because dot indexing is not supported for variables of this type.
pops up. How can I accomplish my goal? I am still relatively new to Matlab (and coding in general...). My background is in Engineering so I never have used networks before, just good ole data processing, so a bunch of the jargon on the network documentation flies over my head, as well as all the coding jargon :P
Thank you so much! My stab at recreating the Minnesota example (very poorly) is below.
clear,clc
% Randomly generate values for node graph
n = 15;
e = 2*n;
s = randi(n, e, 1);
t = randi(n, e, 1);
% Graph random plot
G = graph(true(n), 'omitselfloops'); % No self loops
% Remove nodes that dont have an edge attatched
p = randperm(numedges(G), e);
G = graph(G.Edges(p, :));
isolated_nodes = find(degree(G) == 0);
G = rmnode(G,isolated_nodes);
% Plot, lol
plot(G)
% Find Centrality of each node and plot their closeness by color,
% unweighted
ucc = centrality(G,'closeness');
p.NodeCData = ucc; % Unable to perform assignment because dot indexing is not supported for variables of this type.
colormap jet
colorbar
title('Closeness Centrality Scores Unweighted')

Answers (1)

Rohit
Rohit on 15 Jun 2023
Hi Evan,
I understand that you want to use centrality function to apply it to a random plot of nodes and edges.
The error you are currently facing of unable to perform assignment is because you are trying to access NodeCData property which is not available for type ‘double’. In minnesota example, p is of type 'GraphPlot', so you can set ‘NodeCDataproperty.
I am giving you sample code of how you can use centrality function with random nodes and edges.
clear, clc
% Randomly generate values for node graph
n = 15;
e = 2*n;
s = randi(n, e, 1);
t = randi(n, e, 1);
% Create a random plot of nodes and edges
G = graph(s, t); % Create the graph
% Remove isolated nodes (nodes without edges)
isolated_nodes = find(degree(G) == 0);
G = rmnode(G, isolated_nodes);
% Plot the graph
figure
p = plot(G, 'Layout', 'force'); % Plot the graph
% Compute betweenness centrality
ucc = centrality(G, 'closeness');
% Update the node colors in the plot
p.NodeCData = ucc;
colormap jet
colorbar
title('Closeness Centrality Scores (Unweighted)')
You can know more about centrality function by following this documentation link: https://in.mathworks.com/help/matlab/ref/graph.centrality.html

Products


Release

R2022a

Community Treasure Hunt

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

Start Hunting!