view doc/interpreter/matrix.texi @ 2653:e7908588548a

[project @ 1997-02-01 16:53:52 by jwe]
author jwe
date Sat, 01 Feb 1997 16:57:10 +0000
parents 80a42c3fefc9
children 18192eea4973
line wrap: on
line source

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

@node Matrix Manipulation, Special Matrices, Plotting, Top
@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::        
@end menu

@node Finding Elements and Checking Conditions, Rearranging Matrices, Matrix Manipulation, Matrix Manipulation
@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.

@deftypefn {Built-in Function} {} any (@var{x})
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

To see if any of the elements of a matrix are nonzero, you can use a
statement like

@example
any (any (a))
@end example
@end deftypefn

@deftypefn {Built-in Function} {} all (@var{x})
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 in a column 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 it's 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))}.

@deftypefn {Function File} {[@var{errorcode}, @var{y_1}, ...] =} common_size (@var{x_1}, ...)
Determine if all input arguments are either scalar or of common
size.  If so, errorcode is zero, and @var{y_i} is a matrix of the
common size with all entries equal to @var{x_i} if this is a scalar or
@var{x_i} otherwise.  If the inputs cannot be brought to a common size,
errorcode is 1, and @var{y_i} is @var{x_i}.  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

@deftypefn {Function File} {} diff (@var{x}, @var{k})
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.

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.
@end deftypefn

@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, NaN])

     @result{} [ 0, 1, 0 ]
@end group
@end example
@end deftypefn

@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, NaN])

     @result{} [ 0, 0, 1 ]
@end group
@end example
@end deftypefn

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

@example
@group
finite ([13, Inf, NaN])

     @result{} [ 1, 0, 0 ]
@end group
@end example
@end deftypefn

@deftypefn {Loadable Function} {} find (@var{x})
The function @code{find} returns 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 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
        
@node Rearranging Matrices,  , Finding Elements and Checking Conditions, Matrix Manipulation
@section Rearranging Matrices

@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
@end deftypefn

@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
@end deftypefn

@deftypefn {Function File} {} rot90 (@var{x}, @var{n})
Returns 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
@end deftypefn

@deftypefn {Function File} {} reshape (@var{a}, @var{m}, @var{n})
Return a matrix with @var{m} rows and @var{n} columns whose elements are
taken from the matrix @var{a}.  To decide how to order the elements,
Octave pretends that the elements of a matrix are stored 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

If the variable @code{do_fortran_indexing} is nonzero, the
@code{reshape} function is equivalent to

@example
@group
retval = zeros (m, n);
retval (:) = a;
@end group
@end example

@noindent
but it is somewhat less cryptic to use @code{reshape} instead of the
colon operator.  Note that the total number of elements in the original
matrix must match the total number of elements in the new matrix.
@end deftypefn

@deftypefn {Function File} {} shift (@var{x}, @var{b})
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}.
@end deftypefn

@deftypefn {Loadable Function} {[s, i] =} sort (@var{x})
Returns 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
@end deftypefn

Since the @code{sort} function does not allow sort keys to be specified,
so 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

@deftypefn {Function File} {} tril (@var{a}, @var{k})
@deftypefnx {Function File} {} triu (@var{a}, @var{k})
Return a new matrix form 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

@deftypefn {Function File} {} vec (@var{x})
For a matrix @var{x}, returns the vector obtained by stacking the
columns of @var{x} one above the other.

See Magnus and Neudecker (1988), Matrix differential calculus with
applications in statistics and econometrics.
@end deftypefn

@deftypefn {Function File} {} vech (@var{x})
For a square matrix @var{x}, returns the vector obtained from @var{x}
by eliminating all supradiagonal elements and stacking the result
one column above the other.

See Magnus and Neudecker (1988), Matrix differential calculus with
applications in statistics and econometrics.
@end deftypefn