4.2 Ranges

A range is a convenient way to write a row vector with evenly spaced elements. A range expression is defined by the value of the first element in the range, an optional value for the increment between elements, and a maximum value which the elements of the range will not exceed. The base, increment, and limit are separated by colons (the ‘:’ character) and may contain any arithmetic expressions and function calls. If the increment is omitted, it is assumed to be 1. For example, the range

1 : 5

defines the set of values [ 1, 2, 3, 4, 5 ], and the range

1 : 3 : 5

defines the set of values [ 1, 4 ].

Although a range constant specifies a row vector, Octave does not normally convert range constants to vectors unless it is necessary to do so. This allows you to write a constant like 1 : 10000 without using 80,000 bytes of storage on a typical 32-bit workstation.

A common example of when it does become necessary to convert ranges into vectors occurs when they appear within a vector (i.e., inside square brackets). For instance, whereas

x = 0 : 0.1 : 1;

defines x to be a variable of type range and occupies 24 bytes of memory, the expression

y = [ 0 : 0.1 : 1];

defines y to be of type matrix and occupies 88 bytes of memory.

This space saving optimization may be disabled using the function disable_range.

val = disable_range ()
old_val = disable_range (new_val)
disable_range (new_val, "local")

Query or set the internal variable that controls whether ranges are stored in a special space-efficient format.

The default value is true. If this option is disabled Octave will store ranges as full matrices.

When called from inside a function with the "local" option, the variable is changed locally for the function and any subroutines it calls. The original variable value is restored when exiting the function.

See also: disable_diagonal_matrix, disable_permutation_matrix.

Note that the upper (or lower, if the increment is negative) bound on the range is not always included in the set of values, and that ranges defined by floating point values can produce surprising results because Octave uses floating point arithmetic to compute the values in the range. If it is important to include the endpoints of a range and the number of elements is known, you should use the linspace function instead (see Special Utility Matrices).

When adding a scalar to a range, subtracting a scalar from it (or subtracting a range from a scalar) and multiplying by scalar, Octave will attempt to avoid unpacking the range and keep the result as a range, too, if it can determine that it is safe to do so. For instance, doing

a = 2*(1:1e7) - 1;

will produce the same result as 1:2:2e7-1, but without ever forming a vector with ten million elements.

Using zero as an increment in the colon notation, as 1:0:1 is not allowed, because a division by zero would occur in determining the number of range elements. However, ranges with zero increment (i.e., all elements equal) are useful, especially in indexing, and Octave allows them to be constructed using the built-in function ones. Note that because a range must be a row vector, ones (1, 10) produces a range, while ones (10, 1) does not.

When Octave parses a range expression, it examines the elements of the expression to determine whether they are all constants. If they are, it replaces the range expression with a single range constant.

© 1996–2020 John W. Eaton
Permission is granted to make and distribute verbatim copies of this manual provided the copyright notice and this permission notice are preserved on all copies.
Permission is granted to copy and distribute modified versions of this manual under the conditions for verbatim copying, provided that the entire resulting derived work is distributed under the terms of a permission notice identical to this one.
Permission is granted to copy and distribute translations of this manual into another language, under the above conditions for modified versions.
https://octave.org/doc/v5.2.0/Ranges.html