<html lang="en"> <head> <title>Ranges - Untitled</title> <meta http-equiv="Content-Type" content="text/html"> <meta name="description" content="Untitled"> <meta name="generator" content="makeinfo 4.13"> <link title="Top" rel="start" href="index.html#Top"> <link rel="up" href="Numeric-Data-Types.html#Numeric-Data-Types" title="Numeric Data Types"> <link rel="prev" href="Matrices.html#Matrices" title="Matrices"> <link rel="next" href="Single-Precision-Data-Types.html#Single-Precision-Data-Types" title="Single Precision Data Types"> <link href="http://www.gnu.org/software/texinfo/" rel="generator-home" title="Texinfo Homepage"> <meta http-equiv="Content-Style-Type" content="text/css"> <style type="text/css"><!-- pre.display { font-family:inherit } pre.format { font-family:inherit } pre.smalldisplay { font-family:inherit; font-size:smaller } pre.smallformat { font-family:inherit; font-size:smaller } pre.smallexample { font-size:smaller } pre.smalllisp { font-size:smaller } span.sc { font-variant:small-caps } span.roman { font-family:serif; font-weight:normal; } span.sansserif { font-family:sans-serif; font-weight:normal; } --></style> </head> <body> <div class="node"> <a name="Ranges"></a> <p> Next: <a rel="next" accesskey="n" href="Single-Precision-Data-Types.html#Single-Precision-Data-Types">Single Precision Data Types</a>, Previous: <a rel="previous" accesskey="p" href="Matrices.html#Matrices">Matrices</a>, Up: <a rel="up" accesskey="u" href="Numeric-Data-Types.html#Numeric-Data-Types">Numeric Data Types</a> <hr> </div> <h3 class="section">4.2 Ranges</h3> <p><a name="index-range-expressions-231"></a><a name="index-expression_002c-range-232"></a> <a name="index-colon-233"></a> A <dfn>range</dfn> 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 ‘<samp><span class="samp">:</span></samp>’ 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 <pre class="example"> 1 : 5 </pre> <p class="noindent">defines the set of values ‘<samp><span class="samp">[ 1, 2, 3, 4, 5 ]</span></samp>’, and the range <pre class="example"> 1 : 3 : 5 </pre> <p class="noindent">defines the set of values ‘<samp><span class="samp">[ 1, 4 ]</span></samp>’. <p>Although a range constant specifies a row vector, Octave does <em>not</em> convert range constants to vectors unless it is necessary to do so. This allows you to write a constant like ‘<samp><span class="samp">1 : 10000</span></samp>’ without using 80,000 bytes of storage on a typical 32-bit workstation. <p>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 <code>linspace</code> function instead (see <a href="Special-Utility-Matrices.html#Special-Utility-Matrices">Special Utility Matrices</a>). <p>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 <pre class="example"> a = 2*(1:1e7) - 1; </pre> <p>will produce the same result as ‘<samp><span class="samp">1:2:2e7-1</span></samp>’, but without ever forming a vector with ten million elements. <p>Using zero as an increment in the colon notation, as ‘<samp><span class="samp">1:0:1</span></samp>’ 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 <dfn>ones</dfn>. Note that because a range must be a row vector, ‘<samp><span class="samp">ones (1, 10)</span></samp>’ produces a range, while ‘<samp><span class="samp">ones (10, 1)</span></samp>’ does not. <p>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. </body></html>