@c DO NOT EDIT!  Generated automatically by munge-texi.

@c Copyright (C) 1996, 1997 John W. Eaton
@c This is part of the Octave manual.
@c For copying conditions, see the file gpl.texi.

@node Matrix Manipulation
@chapter Matrix Manipulation

There are a number of functions available for checking to see if the
elements of a matrix meet some condition, and for rearranging the
elements of a matrix.  For example, Octave can easily tell you if all
the elements of a matrix are finite, or are less than some specified
value.  Octave can also rotate the elements, extract the upper- or
lower-triangular parts, or sort the columns of a matrix.

@menu
* Finding Elements and Checking Conditions::  
* Rearranging Matrices::        
* Special Utility Matrices::    
* Famous Matrices::             
@end menu

@node Finding Elements and Checking Conditions
@section Finding Elements and Checking Conditions

The functions @code{any} and @code{all} are useful for determining
whether any or all of the elements of a matrix satisfy some condition.
The @code{find} function is also useful in determining which elements of
a matrix meet a specified condition.

@anchor{doc-any}
@deftypefn {Built-in Function} {} any (@var{x}, @var{dim})
For a vector argument, return 1 if any element of the vector is
nonzero.

For a matrix argument, return a row vector of ones and
zeros with each element indicating whether any of the elements of the
corresponding column of the matrix are nonzero.  For example,

@example
@group
any (eye (2, 4))
     @result{} [ 1, 1, 0, 0 ]
@end group
@end example

If the optional argument @var{dim} is supplied, work along dimension
@var{dim}.  For example,

@example
@group
any (eye (2, 4), 2)
     @result{} [ 1; 1 ]
@end group
@end example
@end deftypefn


@anchor{doc-all}
@deftypefn {Built-in Function} {} all (@var{x}, @var{dim})
The function @code{all} behaves like the function @code{any}, except
that it returns true only if all the elements of a vector, or all the
elements along dimension @var{dim} of a matrix, are nonzero.
@end deftypefn


Since the comparison operators (@pxref{Comparison Ops}) return matrices
of ones and zeros, it is easy to test a matrix for many things, not just
whether the elements are nonzero.  For example, 

@example
@group
all (all (rand (5) < 0.9))
     @result{} 0
@end group
@end example

@noindent
tests a random 5 by 5 matrix to see if all of its elements are less
than 0.9.

Note that in conditional contexts (like the test clause of @code{if} and
@code{while} statements) Octave treats the test as if you had typed
@code{all (all (condition))}.

@anchor{doc-xor}
@deftypefn {Mapping Function} {} xor (@var{x}, @var{y})
Return the `exclusive or' of the entries of @var{x} and @var{y}.
For boolean expressions @var{x} and @var{y},
@code{xor (@var{x}, @var{y})} is true if and only if @var{x} or @var{y}
is true, but not if both @var{x} and @var{y} are true.
@end deftypefn


@anchor{doc-is_duplicate_entry}
@deftypefn {Function File} {} is_duplicate_entry (@var{x})
Return non-zero if any entries in @var{x} are duplicates of one
another.
@end deftypefn


@anchor{doc-diff}
@deftypefn {Function File} {} diff (@var{x}, @var{k}, @var{dim})
If @var{x} is a vector of length @var{n}, @code{diff (@var{x})} is the
vector of first differences
@iftex
@tex
 $x_2 - x_1, \ldots{}, x_n - x_{n-1}$.
@end tex
@end iftex
@ifinfo
@var{x}(2) - @var{x}(1), @dots{}, @var{x}(n) - @var{x}(n-1).
@end ifinfo

If @var{x} is a matrix, @code{diff (@var{x})} is the matrix of column
differences along the first non-singleton dimension.

The second argument is optional.  If supplied, @code{diff (@var{x},
@var{k})}, where @var{k} is a nonnegative integer, returns the
@var{k}-th differences. It is possible that @var{k} is larger than
then first non-singleton dimension of the matrix. In this case,
@code{diff} continues to take the differences along the next
non-singleton dimension.

The dimension along which to take the difference can be explicitly
stated with the optional variable @var{dim}. In this case the 
@var{k}-th order differences are calculated along this dimension.
In the case where @var{k} exceeds @code{size (@var{x}, @var{dim})}
then an empty matrix is returned.
@end deftypefn


@anchor{doc-isinf}
@deftypefn {Mapping Function} {} isinf (@var{x})
Return 1 for elements of @var{x} that are infinite and zero
otherwise. For example,

@example
@group
isinf ([13, Inf, NA, NaN])
     @result{} [ 0, 1, 0, 0 ]
@end group
@end example
@end deftypefn


@anchor{doc-isnan}
@deftypefn {Mapping Function} {} isnan (@var{x})
Return 1 for elements of @var{x} that are NaN values and zero
otherwise. For example,

@example
@group
isnan ([13, Inf, NA, NaN])
     @result{} [ 0, 0, 0, 1 ]
@end group
@end example
@end deftypefn


@anchor{doc-finite}
@deftypefn {Mapping Function} {} finite (@var{x})
Return 1 for elements of @var{x} that are finite values and zero
otherwise. For example,

@example
@group
finite ([13, Inf, NA, NaN])
     @result{} [ 1, 0, 0, 0 ]
@end group
@end example
@end deftypefn


@anchor{doc-find}
@deftypefn {Loadable Function} {} find (@var{x})
Return a vector of indices of nonzero elements of a matrix.  To obtain a
single index for each matrix element, Octave pretends that the columns
of a matrix form one long vector (like Fortran arrays are stored).  For
example,

@example
@group
find (eye (2))
     @result{} [ 1; 4 ]
@end group
@end example

If two outputs are requested, @code{find} returns the row and column
indices of nonzero elements of a matrix.  For example,

@example
@group
[i, j] = find (2 * eye (2))
     @result{} i = [ 1; 2 ]
     @result{} j = [ 1; 2 ]
@end group
@end example

If three outputs are requested, @code{find} also returns a vector
containing the nonzero values.  For example,

@example
@group
[i, j, v] = find (3 * eye (2))
     @result{} i = [ 1; 2 ]
     @result{} j = [ 1; 2 ]
     @result{} v = [ 3; 3 ]
@end group
@end example
@end deftypefn

        
@anchor{doc-common_size}
@deftypefn {Function File} {[@var{err}, @var{y1}, ...] =} common_size (@var{x1}, ...)
Determine if all input arguments are either scalar or of common
size.  If so, @var{err} is zero, and @var{yi} is a matrix of the
common size with all entries equal to @var{xi} if this is a scalar or
@var{xi} otherwise.  If the inputs cannot be brought to a common size,
errorcode is 1, and @var{yi} is @var{xi}.  For example,

@example
@group
[errorcode, a, b] = common_size ([1 2; 3 4], 5)
@result{} errorcode = 0
@result{} a = [ 1, 2; 3, 4 ]
@result{} b = [ 5, 5; 5, 5 ]
@end group
@end example

@noindent
This is useful for implementing functions where arguments can either
be scalars or of common size.
@end deftypefn


@node Rearranging Matrices
@section Rearranging Matrices

@anchor{doc-fliplr}
@deftypefn {Function File} {} fliplr (@var{x})
Return a copy of @var{x} with the order of the columns reversed.  For
example,

@example
@group
fliplr ([1, 2; 3, 4])
@result{}  2  1
         4  3
@end group
@end example

Note that @code{fliplr} only workw with 2-D arrays.  To flip N-d arrays
use @code{flipdim} instead.
@end deftypefn

@seealso{flipud, flipdim, rot90 and rotdim}


@anchor{doc-flipud}
@deftypefn {Function File} {} flipud (@var{x})
Return a copy of @var{x} with the order of the rows reversed.  For
example,

@example
@group
flipud ([1, 2; 3, 4])
@result{}  3  4
         1  2
@end group
@end example

Due to the difficulty of defining which axis about which to flip the 
matrix @code{flipud} only work with 2-d arrays.  To flip N-d arrays
use @code{flipdim} instead.
@end deftypefn

@seealso{fliplr, flipdim, rot90 and rotdim}


@anchor{doc-flipdim}
@deftypefn {Function File} {} flipdim (@var{x}, @var{dim})
Return a copy of @var{x} flipped about the dimension @var{dim}.
For example

@example
@group
flipdim ([1, 2; 3, 4], 2)
@result{}  2  1
         4  3
@end group
@end example
@end deftypefn

@seealso{fliplr, flipud, rot90 and rotdim}


@anchor{doc-rot90}
@deftypefn {Function File} {} rot90 (@var{x}, @var{n})
Return a copy of @var{x} with the elements rotated counterclockwise in
90-degree increments.  The second argument is optional, and specifies
how many 90-degree rotations are to be applied (the default value is 1).
Negative values of @var{n} rotate the matrix in a clockwise direction.
For example,

@example
@group
rot90 ([1, 2; 3, 4], -1)
@result{}  3  1
         4  2
@end group
@end example

@noindent
rotates the given matrix clockwise by 90 degrees.  The following are all
equivalent statements:

@example
@group
rot90 ([1, 2; 3, 4], -1)
@equiv{}
rot90 ([1, 2; 3, 4], 3)
@equiv{}
rot90 ([1, 2; 3, 4], 7)
@end group
@end example

Due to the difficulty of defining an axis about which to rotate the 
matrix @code{rot90} only work with 2-D arrays.  To rotate N-d arrays
use @code{rotdim} instead.
@end deftypefn

@seealso{rotdim, flipud, fliplr and flipdim}


@anchor{doc-rotdim}
@deftypefn {Function File} {} rotdim (@var{x}, @var{n}, @var{plane})
Return a copy of @var{x} with the elements rotated counterclockwise in
90-degree increments.  The second argument is optional, and specifies
how many 90-degree rotations are to be applied (the default value is 1).
The third argument is also optional and defines the plane of the
rotation. As such @var{plane} is a two element vector containing two
different valid dimensions of the matrix. If @var{plane} is not given
Then the first two non-singleton dimensions are used.

Negative values of @var{n} rotate the matrix in a clockwise direction.
For example,

@example
@group
rotdim ([1, 2; 3, 4], -1, [1, 2])
@result{}  3  1
         4  2
@end group
@end example

@noindent
rotates the given matrix clockwise by 90 degrees.  The following are all
equivalent statements:

@example
@group
rot90 ([1, 2; 3, 4], -1, [1, 2])
@equiv{}
rot90 ([1, 2; 3, 4], 3, [1, 2])
@equiv{}
rot90 ([1, 2; 3, 4], 7, [1, 2])
@end group
@end example
@end deftypefn

@seealso{rot90, flipud, fliplr and flipdim}


@anchor{doc-cat}
@deftypefn {Built-in Function} {} cat (@var{dim}, @var{array1}, @var{array2}, @dots{}, @var{arrayN})
Return the concatenation of N-d array objects, @var{array1},
@var{array2}, @dots{}, @var{arrayN} along dimension @var{dim}.

@example
@group
A = ones (2, 2);
B = zeros (2, 2);
cat (2, A, B)
@result{} ans =

     1 1 0 0
     1 1 0 0
@end group
@end example

Alternatively, we can concatenate @var{A} and @var{B} along the
second dimension the following way:

@example
@group
[A, B].
@end group
@end example

@var{dim} can be larger than the dimensions of the N-d array objects
and the result will thus have @var{dim} dimensions as the
following example shows:
@example
@group
cat (4, ones(2, 2), zeros (2, 2))
@result{} ans =

   ans(:,:,1,1) =

     1 1
     1 1

   ans(:,:,1,2) =
     0 0
     0 0
@end group
@end example

@end deftypefn
@seealso{horzcat and vertcat}


@anchor{doc-horzcat}
@deftypefn {Built-in Function} {} horzcat (@var{array1}, @var{array2}, @dots{}, @var{arrayN})
Return the horizontal concatenation of N-d array objects, @var{array1},
@var{array2}, @dots{}, @var{arrayN} along dimension 2.
@end deftypefn
@seealso{cat and vertcat}


@anchor{doc-vertcat}
@deftypefn {Built-in Function} {} vertcat (@var{array1}, @var{array2}, @dots{}, @var{arrayN})
Return the vertical concatenation of N-d array objects, @var{array1},
@var{array2}, @dots{}, @var{arrayN} along dimension 1.
@end deftypefn
@seealso{cat and horzcat}


@anchor{doc-permute}
@deftypefn {Built-in Function} {} permute (@var{a}, @var{perm})
Return the generalized transpose for an N-d array object @var{a}.
The permutation vector @var{perm} must contain the elements
@code{1:ndims(a)} (in any order, but each element must appear just once).

@end deftypefn
@seealso{ipermute}


@anchor{doc-ipermute}
@deftypefn {Built-in Function} {} ipermute (@var{a}, @var{iperm})
The inverse of the @code{permute} function.  The expression

@example
ipermute (permute (a, perm), perm)
@end example
returns the original array @var{a}.

@end deftypefn
@seealso{permute}


@anchor{doc-reshape}
@deftypefn {Function File} {} reshape (@var{a}, @var{m}, @var{n}, @dots{})
@deftypefnx {Function File} {} reshape (@var{a}, @var{siz})
Return a matrix with the given dimensions whose elements are taken
from the matrix @var{a}.  The elements of the matrix are access in
column-major order (like Fortran arrays are stored).

For example,

@example
@group
reshape ([1, 2, 3, 4], 2, 2)
     @result{}  1  3
         2  4
@end group
@end example

@noindent
Note that the total number of elements in the original
matrix must match the total number of elements in the new matrix.

A single dimension of the return matrix can be unknown and is flagged
by an empty argument.
@end deftypefn


@anchor{doc-circshift}
@deftypefn {Function File} {@var{y}} = circshift (@var{x}, @var{n})
Circularly shifts the values of the array @var{x}. @var{n} must be
a vector of integers no longer than the number of dimensions in 
@var{x}. The values of @var{n} can be either positive or negative,
which determines the direction in which the values or @var{x} are
shifted. If an element of @var{n} is zero, then the corresponding
dimension of @var{x} will not be shifted. For example

@example
@group
x = [1, 2, 3; 4, 5, 6, 7, 8, 9];
circshift (x, 1)
@result{}  7, 8, 9
    1, 2, 3
    4, 5, 6
circshift (x, -2)
@result{}  7, 8, 9
    1, 2, 3
    4, 5, 6
circshift (x, [0,1])
@result{}  3, 1, 2
    6, 4, 5
    9, 7, 8
@end group
@end example
@end deftypefn

@seealso {permute, ipermute, shiftdim}


@anchor{doc-shiftdim}
@deftypefn {Function File} {@var{y}} = shiftdim (@var{x}, @var{n})
@deftypefnx {Function File} {[@var{y}, @var{ns}]} = shiftdim (@var{x})
Shifts the dimension of @var{x} by @var{n}, where @var{n} must be
an integer scalar. When @var{n} is negative, the dimensions of
@var{x} are shifted to the left, with the leading dimensions
circulated to the end. If @var{n} is positive, then the dimensions
of @var{x} are shifted to the right, with the @var{n} singleton
dimensions added.

Called with a single argument, @code{shiftdim}, removes the leading
singleton dimensions, returning the number of dimensions removed
in the second output argument @var{ns}.

For example 

@example
@group
x = ones (1, 2, 3);
size (shiftdim (x, -1))
@result{} [2, 3, 1]
size (shiftdim (x, 1))
@result{} [1, 1, 2, 3]
[b, ns] = shiftdim (x);
@result{} b =  [1, 1, 1; 1, 1, 1]
@result{} ns = 1
@end group
@end example
@end deftypefn

@seealso {reshape, permute, ipermute, circshift, squeeze}


@anchor{doc-shift}
@deftypefn {Function File} {} shift (@var{x}, @var{b})
@deftypefnx {Function File} {} shift (@var{x}, @var{b}, @var{dim})
If @var{x} is a vector, perform a circular shift of length @var{b} of
the elements of @var{x}.

If @var{x} is a matrix, do the same for each column of @var{x}.
If the optional @var{dim} argument is given, operate along this 
dimension
@end deftypefn


@anchor{doc-sort}
@deftypefn {Loadable Function} {[@var{s}, @var{i}] =} sort (@var{x})
@deftypefnx {Loadable Function} {[@var{s}, @var{i}] =} sort (@var{x}, @var{dim})
@deftypefnx {Loadable Function} {[@var{s}, @var{i}] =} sort (@var{x}, @var{mode})
@deftypefnx {Loadable Function} {[@var{s}, @var{i}] =} sort (@var{x}, @var{dim}, @var{mode})
Return a copy of @var{x} with the elements elements arranged in
increasing order.  For matrices, @code{sort} orders the elements in each
column.

For example,

@example
@group
sort ([1, 2; 2, 3; 3, 1])
     @result{}  1  1
         2  2
         3  3
@end group
@end example

The @code{sort} function may also be used to produce a matrix
containing the original row indices of the elements in the sorted
matrix.  For example,

@example
@group
[s, i] = sort ([1, 2; 2, 3; 3, 1])
     @result{} s = 1  1
            2  2
            3  3
     @result{} i = 1  3
            2  1
            3  2
@end group
@end example

If the optional argument @var{dim} is given, then the matrix is sorted
along the dimension defined by @var{dim}. The optional argument @code{mode}
defines the order in which the values will be sorted. Valid values of
@code{mode} are `ascend' or `descend'.

For equal elements, the indices are such that the equal elements are listed
in the order that appeared in the original list.

The @code{sort} function may also be used to sort strings and cell arrays
of strings, it which case the dictionary order of the strings is used.

The algorithm used in @code{sort} is optimized for the sorting of partially
ordered lists.
@end deftypefn


Since the @code{sort} function does not allow sort keys to be specified,
it can't be used to order the rows of a matrix according to the values
of the elements in various columns@footnote{For example, to first sort
based on the values in column 1, and then, for any values that are
repeated in column 1, sort based on the values found in column 2, etc.}
in a single call.  Using the second output, however, it is possible to
sort all rows based on the values in a given column.  Here's an example
that sorts the rows of a matrix based on the values in the second
column.

@example
@group
a = [1, 2; 2, 3; 3, 1];
[s, i] = sort (a (:, 2));
a (i, :)
     @result{}  3  1
         1  2
         2  3
@end group
@end example

@anchor{doc-tril}
@deftypefn {Function File} {} tril (@var{a}, @var{k})
@deftypefnx {Function File} {} triu (@var{a}, @var{k})
Return a new matrix formed by extracting extract the lower (@code{tril})
or upper (@code{triu}) triangular part of the matrix @var{a}, and
setting all other elements to zero.  The second argument is optional,
and specifies how many diagonals above or below the main diagonal should
also be set to zero.

The default value of @var{k} is zero, so that @code{triu} and
@code{tril} normally include the main diagonal as part of the result
matrix.

If the value of @var{k} is negative, additional elements above (for
@code{tril}) or below (for @code{triu}) the main diagonal are also
selected.

The absolute value of @var{k} must not be greater than the number of
sub- or super-diagonals.

For example,

@example
@group
tril (ones (3), -1)
@result{}  0  0  0
         1  0  0
         1  1  0
@end group
@end example

@noindent
and

@example
@group
tril (ones (3), 1)
@result{}  1  1  0
         1  1  1
         1  1  1
@end group
@end example
@end deftypefn

@seealso{triu and diag}


@anchor{doc-vec}
@deftypefn {Function File} {} vec (@var{x})
Return the vector obtained by stacking the columns of the matrix @var{x}
one above the other.
@end deftypefn


@anchor{doc-vech}
@deftypefn {Function File} {} vech (@var{x})
Return the vector obtained by eliminating all supradiagonal elements of
the square matrix @var{x} and stacking the result one column above the
other.
@end deftypefn


@anchor{doc-prepad}
@deftypefn {Function File} {} prepad (@var{x}, @var{l}, @var{c})
@deftypefnx {Function File} {} postpad (@var{x}, @var{l}, @var{c})
@deftypefnx {Function File} {} postpad (@var{x}, @var{l}, @var{c}, @var{dim})

Prepends (appends) the scalar value @var{c} to the vector @var{x}
until it is of length @var{l}.  If the third argument is not
supplied, a value of 0 is used.

If @code{length (@var{x}) > @var{l}}, elements from the beginning (end) of
@var{x} are removed until a vector of length @var{l} is obtained.

If @var{x} is a matrix, elements are prepended or removed from each row.

If the optional @var{dim} argument is given, then operate along this
dimension.
@end deftypefn


@node Special Utility Matrices
@section Special Utility Matrices

@anchor{doc-eye}
@deftypefn {Built-in Function} {} eye (@var{x})
@deftypefnx {Built-in Function} {} eye (@var{n}, @var{m})
@deftypefnx {Built-in Function} {} eye (@dots{}, @var{class})
Return an identity matrix.  If invoked with a single scalar argument,
@code{eye} returns a square matrix with the dimension specified.  If you
supply two scalar arguments, @code{eye} takes them to be the number of
rows and columns.  If given a vector with two elements, @code{eye} uses
the values of the elements as the number of rows and columns,
respectively.  For example,

@example
@group
eye (3)
     @result{}  1  0  0
         0  1  0
         0  0  1
@end group
@end example

The following expressions all produce the same result:

@example
@group
eye (2)
@equiv{}
eye (2, 2)
@equiv{}
eye (size ([1, 2; 3, 4])
@end group
@end example

The optional argument @var{class}, allows @code{eye} to return an array of
the specified type, like

@example
val = zeros (n,m, "uint8")
@end example

For compatibility with @sc{Matlab}, calling @code{eye} with no arguments
is equivalent to calling it with an argument of 1.
@end deftypefn


@anchor{doc-ones}
@deftypefn {Built-in Function} {} ones (@var{x})
@deftypefnx {Built-in Function} {} ones (@var{n}, @var{m})
@deftypefnx {Built-in Function} {} ones (@var{n}, @var{m}, @var{k}, @dots{})
@deftypefnx {Built-in Function} {} ones (@dots{}, @var{class})
Return a matrix or N-dimensional array whose elements are all 1.
The arguments are handled the same as the arguments for @code{eye}.

If you need to create a matrix whose values are all the same, you should
use an expression like

@example
val_matrix = val * ones (n, m)
@end example

The optional argument @var{class}, allows @code{ones} to return an array of
the specified type, like

@example
val = ones (n,m, "uint8")
@end example
@end deftypefn


@anchor{doc-zeros}
@deftypefn {Built-in Function} {} zeros (@var{x})
@deftypefnx {Built-in Function} {} zeros (@var{n}, @var{m})
@deftypefnx {Built-in Function} {} zeros (@var{n}, @var{m}, @var{k}, @dots{})
@deftypefnx {Built-in Function} {} zeros (@dots{}, @var{class})
Return a matrix or N-dimensional array whose elements are all 0.
The arguments are handled the same as the arguments for @code{eye}.

The optional argument @var{class}, allows @code{zeros} to return an array of
the specified type, like

@example
val = zeros (n,m, "uint8")
@end example
@end deftypefn


@anchor{doc-repmat}
@deftypefn {Function File} {} repmat (@var{A}, @var{m}, @var{n})
@deftypefnx {Function File} {} repmat (@var{A}, [@var{m} @var{n}])
Form a block matrix of size @var{m} by @var{n}, with a copy of matrix
@var{A} as each element.  If @var{n} is not specified, form an 
@var{m} by @var{m} block matrix.
@end deftypefn


@anchor{doc-rand}
@deftypefn {Loadable Function} {} rand (@var{x})
@deftypefnx {Loadable Function} {} rand (@var{n}, @var{m})
@deftypefnx {Loadable Function} {} rand (@code{"seed"}, @var{x})
Return a matrix with random elements uniformly distributed on the
interval (0, 1).  The arguments are handled the same as the arguments
for @code{eye}.  In
addition, you can set the seed for the random number generator using the
form

@example
rand ("seed", @var{x})
@end example

@noindent
where @var{x} is a scalar value.  If called as

@example
rand ("seed")
@end example

@noindent
@code{rand} returns the current value of the seed.
@end deftypefn


@anchor{doc-randn}
@deftypefn {Loadable Function} {} randn (@var{x})
@deftypefnx {Loadable Function} {} randn (@var{n}, @var{m})
@deftypefnx {Loadable Function} {} randn (@code{"seed"}, @var{x})
Return a matrix with normally distributed random elements.  The
arguments are handled the same as the arguments for @code{eye}.  In
addition, you can set the seed for the random number generator using the
form

@example
randn ("seed", @var{x})
@end example

@noindent
where @var{x} is a scalar value.  If called as

@example
randn ("seed")
@end example

@noindent
@code{randn} returns the current value of the seed.
@end deftypefn


The @code{rand} and @code{randn} functions use separate generators.
This ensures that

@example
@group
rand ("seed", 13);
randn ("seed", 13);
u = rand (100, 1);
n = randn (100, 1);
@end group
@end example

@noindent
and

@example
@group
rand ("seed", 13);
randn ("seed", 13);
u = zeros (100, 1);
n = zeros (100, 1);
for i = 1:100
  u(i) = rand ();
  n(i) = randn ();
end
@end group
@end example

@noindent
produce equivalent results.

Normally, @code{rand} and @code{randn} obtain their initial
seeds from the system clock, so that the sequence of random numbers is
not the same each time you run Octave.  If you really do need for to
reproduce a sequence of numbers exactly, you can set the seed to a
specific value.

If it is invoked without arguments, @code{rand} and @code{randn} return a
single element of a random sequence.

The @code{rand} and @code{randn} functions use Fortran code from
@sc{Ranlib}, a library of fortran routines for random number generation,
compiled by Barry W. Brown and James Lovato of the Department of
Biomathematics at The University of Texas, M.D. Anderson Cancer Center,
Houston, TX 77030.

@anchor{doc-randperm}
@deftypefn {Function File} {} randperm (@var{n})
Return a row vector containing a random permutation of the
integers from 1 to @var{n}.
@end deftypefn


@anchor{doc-diag}
@deftypefn {Built-in Function} {} diag (@var{v}, @var{k})
Return a diagonal matrix with vector @var{v} on diagonal @var{k}.  The
second argument is optional.  If it is positive, the vector is placed on
the @var{k}-th super-diagonal.  If it is negative, it is placed on the
@var{-k}-th sub-diagonal.  The default value of @var{k} is 0, and the
vector is placed on the main diagonal.  For example,

@example
@group
diag ([1, 2, 3], 1)
     @result{}  0  1  0  0
         0  0  2  0
         0  0  0  3
         0  0  0  0
@end group
@end example
@end deftypefn


@c XXX FIXME XXX -- is this really worth documenting?
@c
@c DOCSTRING(warn_imag_to_real)
@c 
@c XXX FIXME XXX -- this is here because it is used by @code{ones},
@c @code{zeros}, @code{rand}, etc.

The functions @code{linspace} and @code{logspace} make it very easy to
create vectors with evenly or logarithmically spaced elements.
@xref{Ranges}.

@anchor{doc-linspace}
@deftypefn {Built-in Function} {} linspace (@var{base}, @var{limit}, @var{n})
Return a row vector with @var{n} linearly spaced elements between
@var{base} and @var{limit}.  The number of elements, @var{n}, must be
greater than 1.  The @var{base} and @var{limit} are always included in
the range.  If @var{base} is greater than @var{limit}, the elements are
stored in decreasing order.  If the number of points is not specified, a
value of 100 is used.

The @code{linspace} function always returns a row vector.
@end deftypefn


@anchor{doc-logspace}
@deftypefn {Function File} {} logspace (@var{base}, @var{limit}, @var{n})
Similar to @code{linspace} except that the values are logarithmically
spaced from
@iftex
@tex
$10^{base}$ to $10^{limit}$.
@end tex
@end iftex
@ifinfo
10^base to 10^limit.
@end ifinfo

If @var{limit} is equal to
@iftex
@tex
$\pi$,
@end tex
@end iftex
@ifinfo
pi,
@end ifinfo
the points are between
@iftex
@tex
$10^{base}$ and $\pi$,
@end tex
@end iftex
@ifinfo
10^base and pi,
@end ifinfo
@emph{not}
@iftex
@tex
$10^{base}$ and $10^{\pi}$,
@end tex
@end iftex
@ifinfo
10^base and 10^pi,
@end ifinfo
in order to  be compatible with the corresponding @sc{Matlab} function.
@end deftypefn

@seealso{linspace}


@anchor{doc-warn_neg_dim_as_zero}
@defvr {Built-in Variable} warn_neg_dim_as_zero
If the value of @code{warn_neg_dim_as_zero} is nonzero, print a warning
for expressions like

@example
eye (-1)
@end example

@noindent
The default value is 0.
@end defvr


@anchor{doc-warn_imag_to_real}
@defvr {Built-in Variable} warn_imag_to_real
If the value of @code{warn_imag_to_real} is nonzero, a warning is
printed for implicit conversions of complex numbers to real numbers.
The default value is 0.
@end defvr


@node Famous Matrices
@section Famous Matrices

The following functions return famous matrix forms.

@anchor{doc-hankel}
@deftypefn {Function File} {} hankel (@var{c}, @var{r})
Return the Hankel matrix constructed given the first column @var{c}, and
(optionally) the last row @var{r}.  If the last element of @var{c} is
not the same as the first element of @var{r}, the last element of
@var{c} is used.  If the second argument is omitted, it is assumed to
be a vector of zeros with the same size as @var{c}.

A Hankel matrix formed from an m-vector @var{c}, and an n-vector
@var{r}, has the elements
@iftex
@tex
$$
H (i, j) = \cases{c_{i+j-1},&$i+j-1\le m$;\cr r_{i+j-m},&otherwise.\cr}
$$
@end tex
@end iftex
@ifinfo

@example
@group
H(i,j) = c(i+j-1),  i+j-1 <= m;
H(i,j) = r(i+j-m),  otherwise
@end group
@end example
@end ifinfo
@end deftypefn

@seealso{vander, sylvester_matrix, hilb, invhilb, and toeplitz}


@anchor{doc-hilb}
@deftypefn {Function File} {} hilb (@var{n})
Return the Hilbert matrix of order @var{n}.  The
@iftex
@tex
$i,\,j$
@end tex
@end iftex
@ifinfo
i, j
@end ifinfo
element of a Hilbert matrix is defined as
@iftex
@tex
$$
H (i, j) = {1 \over (i + j - 1)}
$$
@end tex
@end iftex
@ifinfo

@example
H (i, j) = 1 / (i + j - 1)
@end example
@end ifinfo
@end deftypefn

@seealso{hankel, vander, sylvester_matrix, invhilb, and toeplitz}


@anchor{doc-invhilb}
@deftypefn {Function File} {} invhilb (@var{n})
Return the inverse of a Hilbert matrix of order @var{n}.  This can be 
computed computed exactly using
@tex
$$\eqalign{
  A_{ij} &= -1^{i+j} (i+j-1)
             \left( \matrix{n+i-1 \cr n-j } \right)
             \left( \matrix{n+j-1 \cr n-i } \right)
             \left( \matrix{i+j-2 \cr i-2 } \right)^2 \cr
         &= { p(i)p(j) \over (i+j-1) }
}$$
where
$$
  p(k) = -1^k \left( \matrix{ k+n-1 \cr k-1 } \right)
              \left( \matrix{ n \cr k } \right)
$$
@end tex
@ifinfo
@example

            (i+j)         /n+i-1\  /n+j-1\   /i+j-2\ 2
 A(i,j) = -1      (i+j-1)(       )(       ) (       )
                          \ n-j /  \ n-i /   \ i-2 /

        = p(i) p(j) / (i+j-1)

@end example
where
@example
             k  /k+n-1\   /n\
    p(k) = -1  (       ) (   )
                \ k-1 /   \k/
@end example
@end ifinfo

The validity of this formula can easily be checked by expanding 
the binomial coefficients in both formulas as factorials.  It can 
be derived more directly via the theory of Cauchy matrices: 
see J. W. Demmel, Applied Numerical Linear Algebra, page 92.

Compare this with the numerical calculation of @code{inverse (hilb (n))},
which suffers from the ill-conditioning of the Hilbert matrix, and the
finite precision of your computer's floating point arithmetic.

@end deftypefn

@seealso{hankel, vander, sylvester_matrix, hilb, and toeplitz}


@anchor{doc-sylvester_matrix}
@deftypefn {Function File} {} sylvester_matrix (@var{k})
Return the Sylvester matrix of order
@iftex
@tex
$n = 2^k$.
@end tex
@end iftex
@ifinfo
n = 2^k.
@end ifinfo
@end deftypefn

@seealso{hankel, vander, hilb, invhilb, and toeplitz}


@anchor{doc-toeplitz}
@deftypefn {Function File} {} toeplitz (@var{c}, @var{r})
Return the Toeplitz matrix constructed given the first column @var{c},
and (optionally) the first row @var{r}.  If the first element of @var{c}
is not the same as the first element of @var{r}, the first element of
@var{c} is used.  If the second argument is omitted, the first row is
taken to be the same as the first column.

A square Toeplitz matrix has the form:
@iftex
@tex
$$
\left[\matrix{c_0    & r_1     & r_2      & \cdots & r_n\cr
              c_1    & c_0     & r_1      & \cdots & r_{n-1}\cr
              c_2    & c_1     & c_0      & \cdots & r_{n-2}\cr
              \vdots & \vdots  & \vdots   & \ddots & \vdots\cr
              c_n    & c_{n-1} & c_{n-2} & \ldots & c_0}\right]
$$
@end tex
@end iftex
@ifinfo

@example
@group
c(0)  r(1)   r(2)  ...  r(n)
c(1)  c(0)   r(1)  ... r(n-1)
c(2)  c(1)   c(0)  ... r(n-2)
 .     ,      ,   .      .
 .     ,      ,     .    .
 .     ,      ,       .  .
c(n) c(n-1) c(n-2) ...  c(0)
@end group
@end example
@end ifinfo
@end deftypefn

@seealso{hankel, vander, sylvester_matrix, hilb, and invhib}


@anchor{doc-vander}
@deftypefn {Function File} {} vander (@var{c})
Return the Vandermonde matrix whose next to last column is @var{c}.

A Vandermonde matrix has the form:
@iftex
@tex
$$
\left[\matrix{c_1^{n-1}  & \cdots & c_1^2  & c_1    & 1      \cr
              c_2^{n-1}  & \cdots & c_2^2  & c_2    & 1      \cr
              \vdots     & \ddots & \vdots & \vdots & \vdots \cr
              c_n^{n-1}  & \cdots & c_n^2  & c_n    & 1      }\right]
$$
@end tex
@end iftex
@ifinfo

@example
@group
c(1)^(n-1) ... c(1)^2  c(1)  1
c(2)^(n-1) ... c(2)^2  c(2)  1
    .     .      .      .    .
    .       .    .      .    .
    .         .  .      .    .
c(n)^(n-1) ... c(n)^2  c(n)  1
@end group
@end example
@end ifinfo
@end deftypefn

@seealso{hankel, sylvester_matrix, hilb, invhilb, and toeplitz}

