Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
plot a 3D graph

Subject: plot a 3D graph

From: Toan Cao

Date: 19 Feb, 2013 21:40:20

Message: 1 of 4

Hi everyone,

I make a 3D graph of N nodes (vertices of 3D point cloud). I also have an adjacency matrix A (its size: NxN) for this graph where A(i,j)=1 if node i and node j connect together. If not , A(i,j)=0.
So, i want to display this graph in 3D where two nodes (i, j) are linked by a line if A(i, j)=1.
I know there is a function "gplot" for 2D graph, but i can not find the one for 3D graph.
Do you know how to display my graph in 3D? Hope to get your help!

Thanks in advance,
Toan

Subject: plot a 3D graph

From: ucla@stanford.edu

Date: 27 Feb, 2013 16:38:09

Message: 2 of 4

try this:

function gplot4(A, xyz)
% GPLOT3(A, xyz) is nearly the same as GPLOT(A, xy) except
% that the xyz variable requires a third dimension.
% This function takes an adjacency matrix and visualizes it
% in 3D.
[d e] = size(A);
if d ~= e
    error('A matrix must be square.');
end

[points f] = size(xyz);
if f ~= 3
    error('xyz matrix must be of width 3');
end

if points ~= d
    error('Width of A must equal height of xyz.');
end

xp = [];
xq = [];
yp = [];
yq = [];
zp = [];
zq = [];
% c = [];


for i = 1:(d-1)
    for j = 2:d
        if A(i,j) ~= 0
            xp = [xp xyz(i, 1)];
            xq = [xq xyz(j, 1)];
            yp = [yp xyz(i, 2)];
            yq = [yq xyz(j, 2)];
            zp = [zp xyz(i, 3)];
            zq = [zq xyz(j, 3)];
% c = [c full(A(i,j))];
        end
    end
end
line([xp; xq], [yp; yq], [zp; zq]);
%
%
% numc = 10;
% cmap = autumn(numc);
%
% c = (c - min(c))/(max(c)-min(c));
% c = floor((c*(numc-1))+1);
% %c = cmap(c,:);
% % line([xp; xq], [yp; yq], [zp; zq]);
%
% for n = 1:numel(xp)
% line([xp(n); xq(n)], [yp(n); yq(n)], [zp(n); zq(n)],'Color',cmap(c(n),:));
% end
%
end

Am Dienstag, 19. Februar 2013 22:40:20 UTC+1 schrieb Toan Cao:
> Hi everyone,
>
>
>
> I make a 3D graph of N nodes (vertices of 3D point cloud). I also have an adjacency matrix A (its size: NxN) for this graph where A(i,j)=1 if node i and node j connect together. If not , A(i,j)=0.
>
> So, i want to display this graph in 3D where two nodes (i, j) are linked by a line if A(i, j)=1.
>
> I know there is a function "gplot" for 2D graph, but i can not find the one for 3D graph.
>
> Do you know how to display my graph in 3D? Hope to get your help!
>
>
>
> Thanks in advance,
>
> Toan

Subject: plot a 3D graph

From: ucla@standfort.edu

Date: 27 Feb, 2013 16:41:02

Message: 3 of 4

OR THIS:

function [Xout,Yout,Zout]=gplot3(A,xy,lc)
% gplot‚Ì3ŽŸŒ³•\Ž¦
%
% 2005/04/11 Haruna MATSUSHITA

%GPLOT Plot graph, as in "graph theory".
% GPLOT(A,xy) plots the graph specified by A and xy. A graph, G, is
% a set of nodes numbered from 1 to n, and a set of connections, or
% edges, between them.
%
% In order to plot G, two matrices are needed. The adjacency matrix,
% A, has a(i,j) nonzero if and only if node i is connected to node
% j. The coordinates array, xy, is an n-by-2 matrix with the
% position for node i in the i-th row, xy(i,:) = [x(i) y(i)].
%
% GPLOT(A,xy,LineSpec) uses line type and color specified in the
% string LineSpec. See PLOT for possibilities.
%
% [X,Y] = GPLOT(A,xy) returns the NaN-punctuated vectors
% X and Y without actually generating a plot. These vectors
% can be used to generate the plot at a later time if desired.
%
% See also SPY, TREEPLOT.

% John Gilbert, 1991.
% Modified 1-21-91, LS; 2-28-92, 6-16-92 CBM.
% Copyright 1984-2002 The MathWorks, Inc.
% $Revision: 5.12 $ $Date: 2002/04/09 00:26:12 $

[i,j] = find(A);
[ignore, p] = sort(max(i,j));
i = i(p);
j = j(p);

% Create a long, NaN-separated list of line segments,
% rather than individual segments.

X = [ xy(i,1) xy(j,1) repmat(NaN,size(i))]';
Y = [ xy(i,2) xy(j,2) repmat(NaN,size(i))]';
Z = [ xy(i,3) xy(j,3) repmat(NaN,size(i))]';
X = X(:);
Y = Y(:);
Z = Z(:);

if nargout==0,
    if nargin<3,
        plot3(X, Y, Z)
    else
        plot3(X, Y, Z,lc,'MarkerFaceColor','none','MarkerEdgeColor','b','MarkerSize',5);
    end
else
    Xout = X;
    Yout = Y;
    Zout = Z;
end





Am Dienstag, 19. Februar 2013 22:40:20 UTC+1 schrieb Toan Cao:
> Hi everyone,
>
>
>
> I make a 3D graph of N nodes (vertices of 3D point cloud). I also have an adjacency matrix A (its size: NxN) for this graph where A(i,j)=1 if node i and node j connect together. If not , A(i,j)=0.
>
> So, i want to display this graph in 3D where two nodes (i, j) are linked by a line if A(i, j)=1.
>
> I know there is a function "gplot" for 2D graph, but i can not find the one for 3D graph.
>
> Do you know how to display my graph in 3D? Hope to get your help!
>
>
>
> Thanks in advance,
>
> Toan

Subject: plot a 3D graph

From: Toan Cao

Date: 27 Feb, 2013 17:45:09

Message: 4 of 4

"ucla@stanford.edu" <krull.jan79@googlemail.com> wrote in message <dec00be6-159d-46d5-93a6-02f1671e1c5b@googlegroups.com>...
> try this:
>
> function gplot4(A, xyz)
> % GPLOT3(A, xyz) is nearly the same as GPLOT(A, xy) except
> % that the xyz variable requires a third dimension.
> % This function takes an adjacency matrix and visualizes it
> % in 3D.
> [d e] = size(A);
> if d ~= e
> error('A matrix must be square.');
> end
>
> [points f] = size(xyz);
> if f ~= 3
> error('xyz matrix must be of width 3');
> end
>
> if points ~= d
> error('Width of A must equal height of xyz.');
> end
>
> xp = [];
> xq = [];
> yp = [];
> yq = [];
> zp = [];
> zq = [];
> % c = [];
>
>
> for i = 1:(d-1)
> for j = 2:d
> if A(i,j) ~= 0
> xp = [xp xyz(i, 1)];
> xq = [xq xyz(j, 1)];
> yp = [yp xyz(i, 2)];
> yq = [yq xyz(j, 2)];
> zp = [zp xyz(i, 3)];
> zq = [zq xyz(j, 3)];
> % c = [c full(A(i,j))];
> end
> end
> end
> line([xp; xq], [yp; yq], [zp; zq]);
> %
> %
> % numc = 10;
> % cmap = autumn(numc);
> %
> % c = (c - min(c))/(max(c)-min(c));
> % c = floor((c*(numc-1))+1);
> % %c = cmap(c,:);
> % % line([xp; xq], [yp; yq], [zp; zq]);
> %
> % for n = 1:numel(xp)
> % line([xp(n); xq(n)], [yp(n); yq(n)], [zp(n); zq(n)],'Color',cmap(c(n),:));
> % end
> %
> end
>
> Am Dienstag, 19. Februar 2013 22:40:20 UTC+1 schrieb Toan Cao:
> > Hi everyone,
> >
> >
> >
> > I make a 3D graph of N nodes (vertices of 3D point cloud). I also have an adjacency matrix A (its size: NxN) for this graph where A(i,j)=1 if node i and node j connect together. If not , A(i,j)=0.
> >
> > So, i want to display this graph in 3D where two nodes (i, j) are linked by a line if A(i, j)=1.
> >
> > I know there is a function "gplot" for 2D graph, but i can not find the one for 3D graph.
> >
> > Do you know how to display my graph in 3D? Hope to get your help!
> >
> >
> >
> > Thanks in advance,
> >
> > Toan
Thank you so much for your method
I will try to apply it in my case.

Toan

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us