Plotting a 3D matrix graph

8 views (last 30 days)
Bhavz
Bhavz on 24 Aug 2014
Commented: Bhavz on 26 Aug 2014
link(1,:)=[1 2]; link(2,:)=[1 3]; link(3,:)=[1 4]; link(4,:)=[2 6]; link(5,:)=[3 6]; link(6,:)=[3 7]; link(7,:)=[4 8]; link(8,:)=[4 9]; link(9,:)=[6 10]; link(10,:)=[7 10]; link(11,:)=[8 11]; link(12,:)=[8 12]; link(13,:)=[9 12]; link(14,:)=[9 13]; link(15,:)=[1 5]; link(16,:)=[5 13];
How would i plot the above links/edges vs a value fval that i am obtaining through my coding program in a 3D matrix graph or some other graph which is feasible in my case .Please do help as I have been stuck with this for ages

Accepted Answer

Matz Johansson Bergström
Matz Johansson Bergström on 24 Aug 2014
Edited: Matz Johansson Bergström on 24 Aug 2014
First, I assume that the graph is undirected, edges are going both ways, but you might want to change that maybe.
A = zeros(13, 13); %adjacency matrix, assume undirected graph
link(1,:)=[1 2];
link(2,:)=[1 3];
link(3,:)=[1 4];
link(4,:)=[2 6];
link(5,:)=[3 6];
link(6,:)=[3 7];
link(7,:)=[4 8];
link(8,:)=[4 9];
link(9,:)=[6 10];
link(10,:)=[7 10];
link(11,:)=[8 11];
link(12,:)=[8 12];
link(13,:)=[9 12];
link(14,:)=[9 13];
link(15,:)=[1 5];
link(16,:)=[5 13];
nlinks = length(link);
inds = sub2ind(size(A), link(:,1), link(:,2)) %convert to index in the adjacency matrix
A(inds) = 1; %the points are connected
%Making A symmetric (undirected graph)
A = triu(A, 1);
A = A + A';
%Positioning the nodes in a circle
theta = linspace(0, 2*pi, nlinks);
x = cos(theta);
y = sin(theta);
gplot(A, [x' y'], 'o-')
hold on
%Put text at the nodes
scale = 1.07; %radius of the text positions
for i = 1:size(A,1)
if sum(A(i,:)) > 0 %if there are any links
text(scale*x(i), scale*y(i), num2str(i), 'fontsize', 16,...
'HorizontalAlignment', 'center')%num2str(nums(i)))
end
end
axis([-1.1,1.1,-1.1,1.1])
So, I first create a adjacency matrix from the links you provided. Then I give gplot the coordinates to the node positions using a circle, that just seemed to work in this case. If you wish to place the nodes in another position you can give other coordinates to x and y.
I also added text at the node positions so you can easily check that it is correct.
  2 Comments
Matz Johansson Bergström
Matz Johansson Bergström on 24 Aug 2014
Edit: Added text and make the matrix symmetrical.
Bhavz
Bhavz on 26 Aug 2014
global hd ce Ye delta_edp X_dp;
link(1,:)=[1 2];
link(2,:)=[1 3];
link(3,:)=[1 4];
link(4,:)=[2 6];
link(5,:)=[3 6];
link(6,:)=[3 7];
link(7,:)=[4 8];
link(8,:)=[4 9];
link(9,:)=[6 10];
link(10,:)=[7 10];
link(11,:)=[8 11];
link(12,:)=[8 12];
link(13,:)=[9 12];
link(14,:)=[9 13];
link(15,:)=[1 5];
link(16,:)=[5 13];
%primary path
Pd_pr(1,:)=[2, 6, 10];
Pd_pr(2,:)=[3,7,11];
Pd_pr(3,:)=[3,7,12];
Pd_pr(4,:)=[15, 16,0];
%sencondary path
Pd_s(1,:)=[1,4,9];
Pd_s(2,:)=[2,5,9];
Pd_s(3,:)=[3,8,13];
Pd_s(4,:)=[3,8,14];
path=[Pd_pr;Pd_s];
%link capacity
ce=[50 50 200 50 50 50 150 50 50 50 50 50 50 50 50 50];
%demand
demand=[1 10;1 11;1 12;1 13];
%associated flow values for demand
hd=[50 50 50 50];
%value of X_dp
X_dp=zeros(4,8);
for jj=1:4
for kk=1:8
demandStart=demand(jj,1);
demandEnd=demand(jj,2);
pathStart=path(kk,1);
linkStart=link(pathStart,1);
pathEnd=path(kk,3);
if pathEnd==0
pathEnd=path(kk,2);
end
linkEnd=link(pathEnd,2);
if pathEnd==0
pathEnd=path(kk,2);
end
if demandStart==linkStart && demandEnd==linkEnd
X_dp(jj,kk)=1;
end
end
end
X_dp1=reshape(X_dp,1,[]);
%value of delat_edp
delta_edp=zeros(16,4,8);
for ii=1 : 16
for jj=1: 4
for kk=1:8
for nn=1:3
if path(kk,nn)==ii && X_dp(jj,kk)==1
delta_edp(ii,jj,kk)=1;
end
end
end
end
end
%value of Ye
Ye=zeros(16,1);
for ii=1:16
for kk=1:4
for nn=1:3
if Pd_pr(kk,nn)==ii
Ye(ii)=1;
end
end
end
end
lb = zeros(1,32); ub=X_dp1;
nonlcon=@constraint; [x,fval,exitflag] = ga(@objectiveFun,... 32,[],[],[],[],lb,ub,nonlcon);
l_dp=reshape(x,4,8)
min_romax=fval
So if thats the code and i want to plot fval value for each link would i still do it the way you suggested

Sign in to comment.

More Answers (0)

Categories

Find more on Introduction to Installation and Licensing in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!