Network Graph in Matlab ?

6 views (last 30 days)
Adam Silva
Adam Silva on 21 Jan 2014
Answered: Prateekshya on 10 Oct 2024
Hi everybody, I'm writing a program to output a graph look the same as illustrated below.
The matrix A,
A=[ 12 5
11 5
10 4
5 3
2 3
1 2 ];
column 1 represent the node and column 2 represent the number of connection from node to other nodes.
Matrix B,
B= [12 1
12 4
12 5
12 10
12 11
.
.
.
];
represents the connections to each nodes.
IS THERE A WAY TO GENERATE THIS NETWORK USING MATLAB?
Thanks in advanced.
Shape

Answers (1)

Prateekshya
Prateekshya on 10 Oct 2024
Hello Adam,
MATLAB provides several ways to create and visualize network graphs. You can use the graph and digraph functions for creating undirected and directed graphs, respectively. Here is a basic guide on how to generate and visualize a network graph in MATLAB.
  1. Create a Graph: Use the graph or digraph function to create a graph object.
  2. Add Nodes and Edges: Define the nodes and edges of the graph.
  3. Visualize the Graph: Use the plot function to visualize the graph.
Example code for undirected graph:
% Define the nodes and edges
nodes = {'A', 'B', 'C', 'D', 'E'};
edges = [1 2; 1 3; 2 3; 2 4; 3 5; 4 5];
% Create a graph object
G = graph(edges(:,1), edges(:,2), [], nodes);
% Plot the graph
figure;
h = plot(G, 'Layout', 'force');
% Customize the plot
title('Network Graph');
highlight(h, [1, 2], 'EdgeColor', 'r'); % Example of highlighting an edge
Example code for directed graph:
% Define the nodes and edges for a directed graph
nodes = {'A', 'B', 'C', 'D', 'E'};
edges = [1 2; 1 3; 2 3; 2 4; 3 5; 4 5];
% Create a directed graph object
DG = digraph(edges(:,1), edges(:,2), [], nodes);
% Plot the directed graph
figure;
h = plot(DG, 'Layout', 'layered');
% Customize the plot
title('Directed Network Graph');
highlight(h, [1, 2], 'EdgeColor', 'r'); % Example of highlighting an edge
To know more about the customization options, please follow the below links:
I hope this helps!

Community Treasure Hunt

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

Start Hunting!