Mercurial > hg > octave-nkf
diff doc/interpreter/matrix.texi @ 2449:31d5588dbb61
[project @ 1996-10-30 22:58:44 by jwe]
author | jwe |
---|---|
date | Wed, 30 Oct 1996 23:00:41 +0000 |
parents | b1a56412c385 |
children | 7ee42ff6536a |
line wrap: on
line diff
--- a/doc/interpreter/matrix.texi +++ b/doc/interpreter/matrix.texi @@ -20,26 +20,25 @@ @node Finding Elements and Checking Conditions, Rearranging Matrices, Matrix Manipulation, Matrix Manipulation @section Finding Elements and Checking Conditions -@findex any -@findex all - 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. -Given a vector, the function @code{any} returns 1 if any element of the -vector is nonzero. +@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, @code{any} returns a row vector of ones and +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 -octave:13> any (eye (2, 4)) -ans = +@group +any (eye (2, 4)) - 1 1 0 0 + @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 @@ -48,18 +47,24 @@ @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 -octave:13> all (all (rand (5) < 0.9)) -ans = 0 +@group +all (all (rand (5) < 0.9)) + + @result{} 0 +@end group @end example @noindent @@ -70,100 +75,133 @@ @code{while} statements) Octave treats the test as if you had typed @code{all (all (condition))}. -@findex isinf -@findex finite -@findex isnan +@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]) -The functions @code{isinf}, @code{finite}, and @code{isnan} return 1 if -their arguments are infinite, finite, or not a number, respectively, and -return 0 otherwise. For matrix values, they all work on an element by -element basis. For example, evaluating the expression + @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 -isinf ([1, 2; Inf, 4]) -@end example +@group +isnan ([13, Inf, NaN]) -@noindent -produces the matrix + @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 -ans = +@group +finite ([13, Inf, NaN]) - 0 0 - 1 0 + @result{} [ 1, 0, 0 ] +@end group @end example +@end deftypefn -@findex find - +@deftypefn {Built-in 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 -octave:13> find (eye (2)) -ans = +@group +find (eye (2)) - 1 - 4 + @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 -octave:13> [i, j] = find (eye (2)) -i = +@group +[i, j] = find (2 * eye (2)) - 1 - 2 + @result{} i = [ 1; 2 ] -j = - - 1 - 2 + @result{} j = [ 1; 2 ] +@end group @end example -If three outputs are requested, @code{find} also returns the nonzero -values in a vector. +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 -@findex fliplr -@findex flipud +@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]) -The function @code{fliplr} reverses the order of the columns in a -matrix, and @code{flipud} reverses the order of the rows. For example, + @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 -octave:13> fliplr ([1, 2; 3, 4]) -ans = - - 2 1 - 4 3 - -octave:13> flipud ([1, 2; 3, 4]) -ans = +@group +flipud ([1, 2; 3, 4]) - 3 4 - 1 2 + @result{} 3 4 + 1 2 +@end group @end example - -@findex rot90 +@end deftypefn -The function @code{rot90 (@var{a}, @var{n})} rotates a matrix -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, +@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) -ans = - 3 1 - 4 2 + @result{} 3 1 + 4 2 +@end group @end example @noindent @@ -171,58 +209,62 @@ equivalent statements: @example +@group rot90 ([1, 2; 3, 4], -1) rot90 ([1, 2; 3, 4], 3) rot90 ([1, 2; 3, 4], 7) +@end group @end example - -@findex reshape +@end deftypefn -The function @code{reshape (@var{a}, @var{m}, @var{n})} returns 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). +@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 -octave:13> reshape ([1, 2, 3, 4], 2, 2) -ans = +@group +reshape ([1, 2, 3, 4], 2, 2) - 1 3 - 2 4 + @result{} 1 3 + 2 4 +@end group @end example -If the variable @code{do_fortran_indexing} is @code{"true"}, the +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. - -@findex sort +@end deftypefn -The function @samp{sort} can be used to arrange the elements of a vector -in increasing order. For matrices, @code{sort} orders the elements in -each column. +@deftypefn {Built-in 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 -octave:13> sort (rand (4)) -ans = +@group +sort ([1, 2; 2, 3; 3, 1]) - 0.065359 0.039391 0.376076 0.384298 - 0.111486 0.140872 0.418035 0.824459 - 0.269991 0.274446 0.421374 0.938918 - 0.580030 0.975784 0.562145 0.954964 + @result{} 1 1 + 2 2 + 3 3 +@end group @end example The @code{sort} function may also be used to produce a matrix @@ -230,64 +272,49 @@ matrix. For example, @example -s = +@group +[s, i] = sort ([1, 2; 2, 3; 3, 1]) - 0.051724 0.485904 0.253614 0.348008 - 0.391608 0.526686 0.536952 0.600317 - 0.733534 0.545522 0.691719 0.636974 - 0.986353 0.976130 0.868598 0.713884 - -i = + @result{} s = 1 1 + 2 2 + 3 3 - 2 4 2 3 - 4 1 3 4 - 1 2 4 1 - 3 3 1 2 + @result{} i = 1 3 + 2 1 + 3 2 +@end group @end example +@end deftypefn -@noindent -These values may be used to recover the original matrix from the sorted -version. For example, - -@example -@end example - -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 +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 third column. +that sorts the rows of a matrix based on the values in the second +column. @example -octave:13> a = rand (4) -a = - - 0.080606 0.453558 0.835597 0.437013 - 0.277233 0.625541 0.447317 0.952203 - 0.569785 0.528797 0.319433 0.747698 - 0.385467 0.124427 0.883673 0.226632 +@group +a = [1, 2; 2, 3; 3, 1]; +[s, i] = sort (a (:, 2)); +a (i, :) -octave:14> [s, i] = sort (a (:, 3)); -octave:15> a (i, :) -ans = - - 0.569785 0.528797 0.319433 0.747698 - 0.277233 0.625541 0.447317 0.952203 - 0.080606 0.453558 0.835597 0.437013 - 0.385467 0.124427 0.883673 0.226632 + @result{} 3 1 + 1 2 + 2 3 +@end group @end example -@findex triu -@findex tril - -The functions @code{triu (@var{a}, @var{k})} and @code{tril (@var{a}, -@var{k})} extract the upper or lower triangular part of the matrix -@var{a}, and set 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. +@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 @@ -304,32 +331,24 @@ @example @group -octave:13> tril (rand (4), -1) -ans = +tril (ones (3), -1) - 0.00000 0.00000 0.00000 0.00000 - 0.09012 0.00000 0.00000 0.00000 - 0.01215 0.34768 0.00000 0.00000 - 0.00302 0.69518 0.91940 0.00000 + @result{} 0 0 0 + 1 0 0 + 1 1 0 @end group @end example @noindent -forms a lower triangular matrix from a random 4 by 4 matrix, omitting -the main diagonal, and +and @example @group -octave:13> tril (rand (4), 1) -ans = +tril (ones (3), 1) - 0.06170 0.51396 0.00000 0.00000 - 0.96199 0.11986 0.35714 0.00000 - 0.16185 0.61442 0.79343 0.52029 - 0.68016 0.48835 0.63609 0.72113 + @result{} 1 1 0 + 1 1 1 + 1 1 1 @end group @end example - -@noindent -forms a lower triangular matrix from a random 4 by 4 matrix, including -the main diagonal and the first super-diagonal. +@end deftypefn