| www.delorie.com/gnu/docs/octave/octave_65.html | search |
![]() Buy GNU books! | |
| [ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
An index expression allows you to reference or extract selected elements of a matrix or vector.
Indices may be scalars, vectors, ranges, or the special operator `:', which may be used to select entire rows or columns.
Vectors are indexed using a single expression. Matrices require two
indices unless the value of the built-in variable
do_fortran_indexing is nonzero, in which case matrices may
also be indexed by a single expression.
do_fortran_indexing is nonzero, Octave allows
you to select elements of a two-dimensional matrix using a single index
by treating the matrix as a single vector created from the columns of
the matrix. The default value is 0.
Given the matrix
a = [1, 2; 3, 4] |
all of the following expressions are equivalent
a (1, [1, 2]) a (1, 1:2) a (1, :) |
and select the first row of the matrix.
A special form of indexing may be used to select elements of a matrix or vector. If the indices are vectors made up of only ones and zeros, the result is a new matrix whose elements correspond to the elements of the index vector that are equal to one. For example,
a = [1, 2; 3, 4]; a ([1, 0], :) |
selects the first row of the matrix a.
This operation can be useful for selecting elements of a matrix based on some condition, since the comparison operators return matrices of ones and zeros.
This special zero-one form of indexing leads to a conflict with the standard indexing operation. For example, should the following statements
a = [1, 2; 3, 4]; a ([1, 1], :) |
return the original matrix, or the matrix formed by selecting the first
row twice? Although this conflict is not likely to arise very often in
practice, you may select the behavior you prefer by setting the built-in
variable prefer_zero_one_indexing.
prefer_zero_one_indexing is nonzero, Octave
will perform zero-one style indexing when there is a conflict with the
normal indexing rules. See section 8.1 Index Expressions. For example, given a
matrix
a = [1, 2, 3, 4] |
with prefer_zero_one_indexing is set to nonzero, the
expression
a ([1, 1, 1, 1]) |
results in the matrix [ 1, 2, 3, 4 ]. If the value of
prefer_zero_one_indexing set to 0, the result would be
the matrix [ 1, 1, 1, 1 ].
In the first case, Octave is selecting each element corresponding to a `1' in the index vector. In the second, Octave is selecting the first element multiple times.
The default value for prefer_zero_one_indexing is 0.
Finally, indexing a scalar with a vector of ones can be used to create a vector the same size as the index vector, with each element equal to the value of the original scalar. For example, the following statements
a = 13; a ([1, 1, 1, 1]) |
produce a vector whose four elements are all equal to 13.
Similarly, indexing a scalar with two vectors of ones can be used to create a matrix. For example the following statements
a = 13; a ([1, 1], [1, 1, 1]) |
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 ones to generate a matrix of the appropriate
size whose elements are all one, and then to scale it to produce the
desired result. See section 15.3 Special Utility Matrices.
prefer_column_vectors is nonzero, operations like
for i = 1:10 a (i) = i; endfor |
(for a previously undefined) produce column vectors. Otherwise, row
vectors are preferred. The default value is 1.
If a variable is already defined to be a vector (a matrix with a single
row or column), the original orientation is respected, regardless of the
value of prefer_column_vectors.
resize_on_range_error is nonzero, expressions
like
for i = 1:10 a (i) = sqrt (i); endfor |
(for a previously undefined) result in the variable a
being resized to be just large enough to hold the new value. New
elements that have not been given a value are set to zero. If the value
of resize_on_range_error is 0, an error message is printed and
control is returned to the top level. The default value is 1.
Note that it is quite inefficient to create a vector using a loop like the one shown in the example above. In this particular case, it would have been much more efficient to use the expression
a = sqrt (1:10); |
thus avoiding the loop entirely. In cases where a loop is still
required, or a number of values must be combined to form a larger
matrix, it is generally much faster to set the size of the matrix first,
and then insert elements using indexing commands. For example, given a
matrix a,
[nr, nc] = size (a); x = zeros (nr, n * nc); for i = 1:n x(:,(i-1)*n+1:i*n) = a; endfor |
is considerably faster than
x = a; for i = 1:n-1 x = [x, a]; endfor |
particularly for large matrices because Octave does not have to repeatedly resize the result.
| [ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
| webmaster donations bookstore | delorie software privacy |
| Copyright © 2003 by The Free Software Foundation | Updated Jun 2003 |