Plotting a 3D matrix graph
3 次查看(过去 30 天)
显示 更早的评论
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 个评论
采纳的回答
Matz Johansson Bergström
2014-8-24
编辑:Matz Johansson Bergström
2014-8-24
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 个评论
更多回答(0 个)
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!