function [Xout,Yout,Zout]=gplot3(A,xyz,lc)
%GPLOT Plot graph, as in "graph theory".
%   GPLOT3(A,xyz) plots the 3D graph specified by A and xyz. A graph, G, is
%   a set of nodes numbered from 1 to n, and a set of connections, or
%   edges, between them. (The connectivity of the graph itself must be 
%   essentially 2-dimensional, but with 3D coordinates)
%
%   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, xyz, is an n-by-2 matrix with the
%   position for node i in the i-th row, xyz(i,:) = [x(i) y(i) z(i)].
%   
%   GPLOT3(A,xyz,LineSpec) uses line type and color specified in the
%   string LineSpec. See PLOT for possibilities.
%
%   [X,Y,Z] = GPLOT3(A,xyz) returns the NaN-punctuated vectors
%   X and Y and Z 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 (c) 1984-98 by The MathWorks, Inc.
%   $Revision: 5.8 $  $Date: 1997/11/21 23:44:26 $
%   modified by R. Murray-Smith 28th Feb 2000
[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 = [ xyz(i,1) xyz(j,1) repmat(NaN,size(i))]';
Y = [ xyz(i,2) xyz(j,2) repmat(NaN,size(i))]';
Z = [ xyz(i,3) xyz(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);
    end
else
    Xout = X;
    Yout = Y;
    Zout = Z;
end