Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

matlab draw a graph from the incidence matrix

Is there a way to draw a graph from the Incidence matrix. By graph I mean http://en.wikipedia.org/wiki/Graph_(mathematics) not a plot.

Up till now I found only how to convert incidence matrix to a adjacency matrix. In R this is possible with an igraph library. So is there an easy way to do it in matlab

like image 709
Salvador Dali Avatar asked Dec 21 '22 13:12

Salvador Dali


2 Answers

You may use gplot:

k = 1:30;
[B,XY] = bucky;
gplot(B(k,k),XY(k,:),'-*')
axis square

This function is commonly used in machine learning problems. While searching up I've seen an implementation for weighted graph plotting.

enter image description herehttp://www.mathworks.com/help/matlab/ref/gplot.html

EDIT:

dt = 2*pi/10;
t = dt:dt:2*pi;
x = cos(t); y = sin(t);
A = ones(10);
gplot(A,[x' y']);
A = ones(3,3);
gplot(A,[x' y']);
a = [0 1 1; 1 0 0; 1 1 0];
gplot(a,[x' y'] ,'-*');

All you have to do is make sure the XY plane has sufficient (x,y) pairs for each node in your graph.

Here is A's gplot:

enter image description here

like image 66
0x90 Avatar answered Jan 05 '23 16:01

0x90


GRAPHS WITH ARROWS

All the previous replies are dealing with graphs without taking into account if the graph is direct or not. I mean, it is possible that if there is an edge (i,j) the edge (j,i) is not there. To take into account this, through the following code:

% This figure will be used to plot the structure of the graph represented
% by the current A matrix.
figure

dt = 2*pi/N_robots;
t = dt:dt:2*pi;
x = cos(t); y = sin(t);

agents=[       2    2.5;
               0.5  2.0;
               0.5  1.0;
               2.0  0.5;
               3.5  1.0;
               3.5  2.0;];

agents = p0;       

agents = [x' y'];

% plot(agents(:,1),agents(:,2),'s','MarkerSize', 20, 'MarkerFaceColor', [1 0 1])
grid on
%xlim([0 4])
%ylim([0 3])
hold on
index=1;
% The following prints the non-directed graph corresponding to the A matrix
for i=1:N_robots
    for j=index:N_robots
        if A(i,j) == 1
            arrowline(agents([i j],1),agents([i j],2),'arrowsize',600);          
%             plot(agents([i j],1),agents([i j],2),'--','LineWidth',2.5);
        end
    end
end
set(gca,'FontSize',fontsize2)

title('Structure of the Graph','interpreter', 'latex','FontSize', 18)

You can have the following result: enter image description here

This is working for sure for 6 agents for now. I didn't have time to test for a generic number of agents, but in principle it should work. You could use a different agents vector to do that.

I hope this will help you.

like image 25
desmond13 Avatar answered Jan 05 '23 16:01

desmond13