how can we draw graph using adjacency matrix in matlab

Hi, every on am new to matlab i want to draw a graph using adjacency matrix of image when am tried an inbuilt function called graph i am not getting the expected output please help me to draw graph.
my input image
clc;
clear all;
close all;
X=imread('one.bmp');
imfinfo('one.bmp')
figure,imshow(X)
b = imresize(X,[100,100]);
si = size(b,1);
sj = size(b,2);
%figure;imshow(b);
% Binarization
% th = graythresh(b);
% I = im2bw(b,th);
%thinning
kl=bwmorph(~b,'thin',inf);
figure,imshow(kl)
R(:,:)=kl(:,:);
%grid size
t1=25;
D=100;
I=1;
U1=t1;
J=1;
U2=t1;
E=1;
t2=D/t1;
%Z=1;
for iir=1:t2
for jjr=1:t2
B(I:U1,J:U2)=R(I:U1,J:U2);
% vc=sum(B(I:U1,J:U2));
% Fd=sum(vc);
[x,y]=find(B==1);
CX=mean(x);
CY=mean(y);
CXXX(E)=CX;
CYYY(E)=CY;
CXX(iir,jjr)=CX;
CYY(iir,jjr)=CY;
T(I:U1,J:U2)=B(I:U1,J:U2);
J=J+t1;
U2=U2+t1;
E=E+1;
clear B x y
end
I=I+t1;
U1=U1+t1;
J=1;
U2=t1;
end
%plot and grid
figure,imshow(R)
hold on
M10 = size(R,1);
N10 = size(R,2);
a=t1;
b=t1;
for k = 1:a:M10
x = [1 N10];
y = [k k];
plot(x,y,'Color','white');
set(findobj('Tag','MyGrid'),'Visible','on')
end
for k = 1:b:N10
x = [k k];
y = [1 M10];
plot(x,y,'Color','white');
set(findobj('Tag','MyGrid'),'Visible','on')
end
plot(CYY,CXX,'g*')
%line(CYY,CXX)
%CC=bwconncomp(CXX,4)
hold off
%node neighbourhoood analyssis
N1=t2;
for I2=1:N1
for J2=1:N1
%last row
if(I2>=N1)
W1=CXX(I2,J2);
W2=CXX(I2-1,J2);
W3=CYY(I2,J2);
W4=CYY(I2-1,J2);
W6=[W1,W2];
W7=[W3,W4];
line(W7,W6);
if(J2>=N1)
Z=CXX(I2,J2);
else
if (CXX(I2,J2+1)>1)&& ((CYY(I2,J2+1)>1))
TXX=CXX(I2,J2);
TYY=CXX(I2,J2+1);
TTX=CYY(I2,J2);
TTY=CYY(I2,J2+1);
IY=[TXX,TYY];
IIY=[TTX,TTY];
line(IIY,IY);
end
end
else
if(J2>=N1);
W1=CXX(I2,J2);
W2=CXX(I2+1,J2);
W3=CYY(I2,J2);
W4=CYY(I2+1,J2);
W6=[W1,W2];
W7=[W3,W4];
line(W7,W6);
else
if (CXX(I2,J2+1)>1)&& ((CYY(I2,J2+1)>1))
TXX=CXX(I2,J2);
TYY=CXX(I2,J2+1);
TTX=CYY(I2,J2);
TTY=CYY(I2,J2+1);
IY=[TXX,TYY];
IIY=[TTX,TTY];
line(IIY,IY);
end
if (CXX(I2+1,J2)>1)&& ((CYY(I2+1,J2)>1))
W1=CXX(I2,J2);
W2=CXX(I2+1,J2);
W3=CYY(I2,J2);
W4=CYY(I2+1,J2);
W6=[W1,W2];
W7=[W3,W4];
line(W7,W6);
J2=J2+1
end
end
end
end
end
A=zeros(t2,t2);
ttt=1;
for rt=1:t2
for rt1=1:t2
if(CXX(rt,rt1)>1)
A(rt,rt1)=ttt
end
ttt=ttt+1;
end
end
g=1;
jk=1;
um=t2-1;
um1=t2;
for iir=1:t2
for jjr=1:t2
if(A(iir,jjr)>=0)
BB(jk)=0;
DD(g)=0;
FF(g)=0;
HH(g)=0;
end
if(A(iir,jjr)>=1)
if(iir==um1)&&(jjr==1)
GG(g)=A(iir,jjr);
HH(g)=A(iir-1,jjr);
BB(jk)=A(iir,jjr+1);
DD(g)=0;
else
if(iir==um1)&&(jjr>1)&&(jjr<=um)
FF(g)=A(iir,jjr-1);
BB(jk)=A(iir,jjr+1);
HH(g)=A(iir-1,jjr);
else
if(iir==um1)&&(jjr==um1)
HH(g)=A(iir-1,jjr);
FF(g)=A(iir,jjr-1);
DD(g)=0;
BB(jk)=0;
else
if(iir==1)&&(jjr==um1)
FF(g)=A(iir,jjr-1);
DD(g)=A(iir+1,jjr);
BB(jk)=0;
else
if(iir>=1)&&(iir<=um)&&(jjr==um1)
HH(g)=A(iir-1,jjr);
DD(g)=A(iir+1,jjr);
FF(g)=A(iir,jjr-1);
BB(jk)=0;
else
if(iir==1)&&(jjr==1)
BB(jk)=A(iir,jjr+1);
DD(g)=A(iir+1,jjr);
else
if(iir==1)&&(jjr>=1)&&(jjr<=um)
FF(g)=A(iir,jjr-1);
DD(g)=A(iir+1,jjr);
BB(jk)=A(iir,jjr+1);
else
if(iir>1)&&(iir<=um)&&(jjr==1)
HH(g)=A(iir-1,jjr);
DD(g)=A(iir+1,jjr);
BB(jk)=A(iir,jjr+1);
else
if(iir>1)&&(iir<=um)&&(jjr>1)&&(jjr<=um)
BB(jk)=A(iir,jjr+1);
DD(g)=A(iir+1,jjr);
HH(g)=A(iir-1,jjr);
FF(g)=A(iir,jjr-1);
end
end
end
end
end
end
end
end
end
end
g=g+1;
jk=jk+1;
end
end
adj=zeros(t2*t2);
H9=size(adj);
Y=1;
for ll=1:1
for ii=1:H9(1,1)
for jj=1:H9(1,1)
if (ii>=4)
if (jj==DD(1,Y))
adj(ii,jj)=1;
end
if (jj==FF(1,Y))
adj(ii,jj)=1;
end
if (jj==BB(1,Y))
adj(ii,jj)=1;
end
if (jj==HH(1,Y))
adj(ii,jj)=1;
end
else
if (jj==BB(1,Y))
adj(ii,jj)=1;
end
if (jj==DD(1,Y))
adj(ii,jj)=1;
end
if (jj==FF(1,Y))
adj(ii,jj)=1;
end
if (jj==HH(1,Y))
adj(ii,jj)=1;
end
end
end
Y=Y+1;
end
end
this my code i have stored my adjacency matrix in an variable adj
when am tried to draw a graph using this matrix am getting the following graph am using matlab 2016a when i tried draw a graph using the graph function am getting following result
my expected output be in this form.
please help me to draw a graph as my expected image.

 Réponse acceptée

The adjacency matrix only says which nodes are connected to each other. It does not say anything about how to place the nodes. You can specify layout properties in the plot function, but they may very well not correspond to what you want ('force','circle','layered','subspace'). You can also specify X and Y coordinates.
Plot graph
G = graph(adj);
p = plot(G,'layout','force')
Use coordinates specified in X and Y
p.XData = X;
p.YData = Y;

4 commentaires

AM getting the output image like this
when i plot the graph using your code
What I meant was that you have to specify the X and Y coordinates. I just called them X and Y and then you have to find them yourself in your code. I had trouble finding these coordinates in your code so I assumed you could. I am not familiar with your code, but this is something along the way:
M = adj;
M(~any(M,2),:) = []; % remove zero rows
M(:,~any(M,1)) = []; % remove zero columns
X = CXX'; % transpose
Y = CYY'; % transpose
X = X(:); % x vector
Y = Y(:); % y vector
X(isnan(X)) = []; % remove Nan
Y(isnan(Y)) = [];
G = graph(M); % graph
plot(G,'k','Xdata',X,'Ydata',Y,'MarkerSize',15,'NodeLabel',{}); % plot
DEEPAK P
DEEPAK P le 19 Avr 2017
Modifié(e) : DEEPAK P le 19 Avr 2017
thank u Tobias Johansson, how can i rotate this plotted figure?? is it possible to apply minimum spanning tree for this plotted graph??
It looks like your graph already has no cycles, so the minimum spanning tree of this graph is just the graph itself.
The minimum spanning tree of a graph is independent of coordinates associated with the nodes of the graph. Are you maybe looking for something like this? https://en.wikipedia.org/wiki/Euclidean_minimum_spanning_tree

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Networks dans Centre d'aide et File Exchange

Produits

Community Treasure Hunt

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

Start Hunting!

Translated by