Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to graph adjacency matrix using MATLAB

I want to create a plot showing connections between nodes from an adjacency matrix like the one below.

enter image description here

gplot seems like the best tool for this. However, in order to use it, I need to pass the coordinate of each node. The problem is that I don't know where the coordinates should be, I was hoping the function would be capable of figuring out a good layout for me.

For example here's my output using the following arbitrary coordinates:

 A = [1 1 0 0 1 0;
      1 0 1 0 1 0;
      0 1 0 1 0 0;
      0 0 1 0 1 1;
      1 1 0 1 0 0;
      0 0 0 1 0 0];

 crd = [0 1;
        1 1;
        2 1;
        0 2;
        1 2;
        2 2];

 gplot (A, crd, "o-");

enter image description here

Which is hard to read, but if I play around with the coordinates a bit and change them to the following it becomes much more readable.

   crd = [0.5 0;
         0 1;
         0 2;
         1 2;
         1 1;
         1.5 2.5];

enter image description here

I don't expect perfectly optimized coordinates or anything, but how can I tell MATLAB to automatically figure out a set of coordinates for me that looks okay using some sort of algorithm so I can graph something that looks like the top picture.

Thanks in advance.

like image 744
Charles Clayton Avatar asked Dec 07 '14 05:12

Charles Clayton


People also ask

How do you create a graph using adjacency matrix in Matlab?

A = adjacency( G ) returns the sparse adjacency matrix for graph G . If (i,j) is an edge in G , then A(i,j) = 1 . Otherwise, A(i,j) = 0 . A = adjacency( G ,'weighted') returns a weighted adjacency matrix, where for each edge (i,j) , the value A(i,j) contains the weight of the edge.

How do you draw a graph from adjacency matrix?

Adjacency Matrix of a GraphTo fill the adjacency matrix, we look at the name of the vertex in row and column. If those vertices are connected by an edge or more, we count number of edges and put this number as matrix element. The matrix to represent a graph in this way is called Adjacency matrix .

How do you plot a matrix graph in Matlab?

Use the adjacency function to create a new adjacency matrix for the graph. Display the nodes in one hemisphere of the bucky ball by indexing into the adjacency matrix to create a new, smaller graph. figure A = adjacency(G); H = graph(A(1:30,1:30)); h = plot(H);

What is adjacency matrix in Matlab?

An adjacency matrix is a square matrix that provides information on reactants and products of reactions in a model.


1 Answers

As of R2015b, MATLAB now has a suite of graph and network algorithms. For this example, you can create an undirected graph object and then plot it using the overloaded plot function:

% Create symmetric adjacency matrix
A = [1 1 0 0 1 0;
     1 0 1 0 1 0;
     0 1 0 1 0 0;
     0 0 1 0 1 1;
     1 1 0 1 0 0;
     0 0 0 1 0 0];
% Create undirected graph object
G = graph(A);
% Plot
plot(G);

Layout created using MATLAB's graph/plot function

like image 178
KQS Avatar answered Sep 17 '22 15:09

KQS