Plotting a 3D matrix graph
8 views (last 30 days)
Show older comments
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
0 Comments
Accepted Answer
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
More Answers (0)
See Also
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!