<?xml version="1.0" encoding="UTF-8"?>
<refentry xmlns="http://docbook.org/ns/docbook" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:svg="http://www.w3.org/2000/svg" xmlns:ns5="http://www.w3.org/1999/xhtml" xmlns:mml="http://www.w3.org/1998/Math/MathML" xmlns:db="http://docbook.org/ns/docbook" xmlns:scilab="http://www.scilab.org" xml:id="ndgrid" xml:lang="en">
    <refnamediv>
        <refname>ndgrid</refname>
        <refpurpose>build matrices or N-D arrays by replicating some template vectors
        </refpurpose>
    </refnamediv>
    <refsynopsisdiv>
        <title>Syntax</title>
        <synopsis>
               [X, Y] = ndgrid(x)
               [X, Y] = ndgrid(x,y)
            [X, Y, Z] = ndgrid(x,y,z)
         [X, Y, Z, T] = ndgrid(x,y,z,t)
    [X1, X2, ..., Xm] = ndgrid(x1,x2,...,xm)
        </synopsis>
    </refsynopsisdiv>
    <refsection role="arguments">
        <title>Arguments</title>
        <variablelist>
            <varlistentry>
                <term>x, y, z, ...</term>
                <listitem>
                    <para>vectors of any data types. They may have distinct data types. By default, <literal>y=x</literal>.</para>
                </listitem>
            </varlistentry>
            <varlistentry>
                <term>X, Y, Z, ...</term>
                <listitem>
                    <para>
                        matrices in case of 2 input arguments, or hypermatrices otherwise. They all have the same sizes: <code>size(x,"*")</code> rows, <code>size(y,"*")</code> columns, <code>size(z,"*")</code> layers, etc. They have the datatypes of respective input vectors: <literal>typeof(X)==typeof(x)</literal>, <literal>typeof(Y)==typeof(y)</literal>, etc.
                    </para>
                </listitem>
            </varlistentry>
        </variablelist>
    </refsection>
    <refsection role="description">
        <title>Description</title>
        <para>
            The first application of <function>ndgrid</function> is to build a grid of nodes meshing the 2D or 3D or N-D space according to 2, 3, or more sets <literal>x</literal>, <literal>y</literal>, etc.. of "template" coordinates sampled along each direction/dimension of the space that you want to mesh.
        </para>
        <para>
            Hence, the matrix or hypermatrix <literal>X</literal> is made by replicating the vector <literal>x</literal> as all its columns ; the matrix or hypermatrix <literal>Y</literal> is made by replicating the vector <literal>y</literal> as all its rows ; <literal>Z</literal> is made of replicating the vector <literal>z</literal> along all its local thicknesses (3rd dimension); etc.
        </para>
        <screen><![CDATA[--> [X, Y] = ndgrid([1 3 4], [0 2 4 6])
 X  =
   1.   1.   1.   1.
   3.   3.   3.   3.
   4.   4.   4.   4.

 Y  =
   0.   2.   4.   6.
   0.   2.   4.   6.
   0.   2.   4.   6.
]]></screen>
        <para>
            Then, the coordinates of the node(i,j) in the 2D space will be simply <literal>[x(i), y(j)]</literal> now given by <literal>[X(i,j), Y(i,j)]</literal>. As well, the coordinates of a <literal>node(i,j,k)</literal> of a 3D grid will be <literal>[x(i), y(j), z(k)]</literal> now given by <literal>[X(i,j,k), Y(i,j,k), Z(i,j,k)]</literal>.
        </para>
        <para>This replication scheme can be generalized to any number of dimensions, as well to any type of uniform data. Let's for instance consider 2 attributes:
            <orderedlist>
                <listitem>
                    The first is a number, to be chosen from the vector say <literal>n= [ 3 7 ]</literal>
                </listitem>
                <listitem>
                    The second is a letter, to be chosen from the vector say <literal>c= ["a" "e" "i" "o" "u" "y"]</literal>
                </listitem>
            </orderedlist>
            Then we want to build the set of all {n,c} possible pairs. It will just be the 2D grid:
        </para>
        <screen><![CDATA[--> [N, C] = ndgrid([3 7],["a" "e" "i" "o" "u" "y"])
 C  =
!a  e  i  o  u  y  !
!a  e  i  o  u  y  !

 N  =
   3.   3.   3.   3.   3.   3.
   7.   7.   7.   7.   7.   7.
]]></screen>
        <para>
            Then, the object(i,j) will have the properties <literal>{n(i) c(j)}</literal> that now can be addressed with <literal>{N(i,j) C(i,j)}</literal>. This kind of grid may be useful to initialize an array of structures.
        </para>
        <para>
            Following examples show how to use <varname>X, Y, Z</varname> in most frequent applications.
        </para>
    </refsection>
    <refsection role="examples">
        <title>Examples</title>
        <para>
            <emphasis role="bold">Example #1:</emphasis>
        </para>
        <programlisting role="example"><![CDATA[
// Create a simple 2d grid
x = linspace(-10,2,40);
y = linspace(-5,5,40);
[X,Y] = ndgrid(x,y);

// Compute ordinates Z(X,Y) on the {X, Y} grid and plot Z(X,Y)
Z = X - 3*X.*sin(X).*cos(Y-4) ;

clf()
plot3d(x,y,Z, flag=[color("green") 2 4], alpha=7, theta=60); show_window()
 ]]></programlisting>
        <scilab:image><![CDATA[
x = linspace(-10,2,40);
y = linspace(-5,5,40);

[X,Y] = ndgrid(x,y);
Z = X - 3*X.*sin(X).*cos(Y-4) ;

clf()
plot3d(x,y,Z, flag=[color("green") 2 4], alpha=7, theta=60); show_window()
]]></scilab:image>
        <para>
            <emphasis role="bold">Example #2:</emphasis>
        </para>
        <programlisting role="example"><![CDATA[
// Create a simple 3d grid
nx = 10; ny = 6; nz = 4;
x = linspace(0,2,nx);
y = linspace(0,1,ny);
z = linspace(0,0.5,nz);
[X,Y,Z] = ndgrid(x,y,z);

// Try to display this 3d grid
XF=[]; YF=[]; ZF=[];

for k=1:nz
   [xf,yf,zf] = nf3d(X(:,:,k),Y(:,:,k),Z(:,:,k));
   XF = [XF xf]; YF = [YF yf]; ZF = [ZF zf];
end

for j=1:ny
   [xf,yf,zf] = nf3d(matrix(X(:,j,:),[nx,nz]),...
                     matrix(Y(:,j,:),[nx,nz]),...
                     matrix(Z(:,j,:),[nx,nz]));
   XF = [XF xf]; YF = [YF yf]; ZF = [ZF zf];
end

clf()
plot3d(XF,YF,ZF, flag=[0 6 3], 66, 61,leg="X@Y@Z")
xtitle("A 3d grid !"); show_window()
 ]]></programlisting>
        <scilab:image><![CDATA[
nx = 10; ny = 6; nz = 4;
x = linspace(0,2,nx);
y = linspace(0,1,ny);
z = linspace(0,0.5,nz);

[X,Y,Z] = ndgrid(x,y,z);
XF=[]; YF=[]; ZF=[];

for k=1:nz
    [xf,yf,zf] = nf3d(X(:,:,k),Y(:,:,k),Z(:,:,k));
    XF = [XF xf]; YF = [YF yf]; ZF = [ZF zf];
end

for j=1:ny
    [xf,yf,zf] = nf3d(matrix(X(:,j,:),[nx,nz]),...
                      matrix(Y(:,j,:),[nx,nz]),...
                      matrix(Z(:,j,:),[nx,nz]));

    XF = [XF xf]; YF = [YF yf]; ZF = [ZF zf];
end

plot3d(XF,YF,ZF, flag=[0 6 3], 66, 61, leg="X@Y@Z")
xtitle("A 3d grid !");
         ]]></scilab:image>
        <para>
            <emphasis role="bold">Example #3: Creates a table of digrams:</emphasis>
        </para>
        <programlisting role="example"><![CDATA[
[c1, c2] = ndgrid(["a" "b" "c"], ["a" "b" "c" "d" "e" "f" "g" "h"])
c1+c2
 ]]></programlisting>
        <screen><![CDATA[--> [c1, c2] = ndgrid(["a" "b" "c"], ["a" "b" "c" "d" "e" "f" "g" "h"])
 c2  =
!a  b  c  d  e  f  g  h  !
!a  b  c  d  e  f  g  h  !
!a  b  c  d  e  f  g  h  !

 c1  =
!a  a  a  a  a  a  a  a  !
!b  b  b  b  b  b  b  b  !
!c  c  c  c  c  c  c  c  !

--> c1+c2
 ans  =
!aa  ab  ac  ad  ae  af  ag  ah  !
!ba  bb  bc  bd  be  bf  bg  bh  !
!ca  cb  cc  cd  ce  cf  cg  ch  !
]]></screen>
    </refsection>
    <refsection role="see also">
        <title>See also</title>
        <simplelist type="inline">
            <member>
                <link linkend="meshgrid">meshgrid</link>
            </member>
            <member>
                <link linkend="kron">kron</link>
            </member>
            <member>
                <link linkend="feval">feval</link>
            </member>
            <member>
                <link linkend="nf3d">nf3d</link>
            </member>
        </simplelist>
    </refsection>
    <refsection role="history">
        <title>History</title>
        <revhistory>
            <revision>
                <revnumber>6.0.0</revnumber>
                <revdescription>
                    Extension to all homogeneous datatypes ([], booleans, encoded integers,
                    polynomials, rationals, strings). Revision of the help page.
                </revdescription>
            </revision>
            <revision>
                <revnumber>6.0.1</revnumber>
                <revdescription>
                    <literal>ndgrid(x)</literal> is now accepted: Default <literal>y=x</literal>.
                </revdescription>
            </revision>
        </revhistory>
    </refsection>
</refentry>
