Mercurial > hg > octave-avbm
changeset 9159:c07cbffb82e3
update indexing docs
author | Jaroslav Hajek <highegg@gmail.com> |
---|---|
date | Wed, 29 Apr 2009 08:16:48 +0200 |
parents | 48ee8c73ff38 |
children | 11e0f0e8ff00 |
files | doc/ChangeLog doc/interpreter/expr.txi |
diffstat | 2 files changed, 39 insertions(+), 6 deletions(-) [+] |
line wrap: on
line diff
--- a/doc/ChangeLog +++ b/doc/ChangeLog @@ -1,6 +1,7 @@ 2009-04-29 Jaroslav Hajek <highegg@gmail.com> * interpreter/numbers.txi: Update info on ranges. + * interpreter/expr.txi: Update some info on indexing. 2009-04-26 Rik <rdrider0-list@yahoo.com> * interpreter/arith.txi: Update section 17.3 (Trigonometry) of arith.txi
--- a/doc/interpreter/expr.txi +++ b/doc/interpreter/expr.txi @@ -93,6 +93,14 @@ @noindent and select the first row of the matrix. +In general, an array with @samp{n} dimensions can be indexed using @samp{m} +indices. If @code{n == m}, each index corresponds to its respective dimension. +The set of index tuples determining the result is formed by the cartesian product +of the index vectors (or ranges or scalars). +If @code{n < m}, then the array is padded by trailing singleton dimensions. +If @code{n > m}, the last @code{n-m+1} dimensions are folded into a single +dimension with extent equal to product of extents of the original dimensions. + @c FIXED -- sections on variable prefer_zero_one_indexing were removed Indexing a scalar with a vector of ones can be used to create a @@ -102,7 +110,7 @@ @example @group a = 13; -a ([1, 1, 1, 1]) +a (ones (1, 4)) @end group @end example @@ -115,17 +123,41 @@ @example @group a = 13; -a ([1, 1], [1, 1, 1]) +a (ones (1, 2), ones (1, 3)) @end group @end example @noindent create a 2 by 3 matrix with all elements equal to 13. -This is an obscure notation and should be avoided. It is better to -use the function @code{ones} to generate a matrix of the appropriate -size whose elements are all one, and then to scale it to produce the -desired result. @xref{Special Utility Matrices}. +The last example could also be written as + +@example +@group +13 (ones (2, 3)) +@end group +@end example + +It should be, noted that @code{ones (1, n)} (a row vector of ones) results in a +range (with zero increment), and is therefore more efficient when used in index +expression than other forms of @dfn{ones}. In particular, when @samp{r} is a row +vector, the expressions + +@example + r(ones (1, n), :) +@end example + +@example + r(ones (n, 1), :) +@end example + +will produce indentical results, but the first one will be significantly +faster, at least for @samp{r} and @samp{n} large enough. The reason is that +in the first case the index is kept in a compressed form, which allows Octave +to choose a more efficient algorithm to handle the expression. + +In general, for an user unaware of these subtleties, it is best to use +the function @dfn{repmat} for spreading arrays into bigger ones. It is also possible to create a matrix with different values. The following example creates a 10 dimensional row vector @math{a} containing