1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98
|
<html lang="en">
<head>
<title>Index Expressions - GNU Octave</title>
<meta http-equiv="Content-Type" content="text/html">
<meta name="description" content="GNU Octave">
<meta name="generator" content="makeinfo 4.13">
<link title="Top" rel="start" href="index.html#Top">
<link rel="up" href="Expressions.html#Expressions" title="Expressions">
<link rel="next" href="Calling-Functions.html#Calling-Functions" title="Calling Functions">
<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="Index-Expressions"></a>
<p>
Next: <a rel="next" accesskey="n" href="Calling-Functions.html#Calling-Functions">Calling Functions</a>,
Up: <a rel="up" accesskey="u" href="Expressions.html#Expressions">Expressions</a>
<hr>
</div>
<h3 class="section">8.1 Index Expressions</h3>
<p><a name="index-g_t_0028-513"></a><a name="index-g_t_0029-514"></a><a name="index-g_t_003a-515"></a>
An <dfn>index expression</dfn> allows you to reference or extract selected
elements of a matrix or vector.
<p>Indices may be scalars, vectors, ranges, or the special operator
‘<samp><span class="samp">:</span></samp>’, which may be used to select entire rows or columns.
<p>Vectors are indexed using a single index expression. Matrices (2-D)
and higher multi-dimensional arrays are indexed using either one index
or N indices where N is the dimension of the array.
When using a single index expression to index 2-D or higher data the
elements of the array are taken in column-first order (like Fortran).
<p>The output from indexing assumes the dimensions of the index
expression. For example:
<pre class="example"> a(2) # result is a scalar
a(1:2) # result is a row vector
a([1; 2]) # result is a column vector
</pre>
<p>As a special case, when a colon is used as a single index, the output
is a column vector containing all the elements of the vector or
matrix. For example:
<pre class="example"> a(:) # result is a column vector
a(:)' # result is a row vector
</pre>
<p>The above two code idioms are often used in place of <code>reshape</code>
when a simple vector, rather than an arbitrarily sized array, is
needed.
<p>Given the matrix
<pre class="example"> a = [1, 2; 3, 4]
</pre>
<p class="noindent">all of the following expressions are equivalent and select the first
row of the matrix.
<pre class="example"> a(1, [1, 2]) # row 1, columns 1 and 2
a(1, 1:2) # row 1, columns in range 1-2
a(1, :) # row 1, all columns
</pre>
<p><a name="index-g_t_0040code_007bend_007d_002c-indexing-516"></a><a name="index-g_t_003aend-517"></a>
In index expressions the keyword <code>end</code> automatically refers to
the last entry for a particular dimension. This magic index can also
be used in ranges and typically eliminates the needs to call
<code>size</code> or <code>length</code> to gather array bounds before indexing.
For example:
<pre class="example"> a = [1, 2, 3, 4];
a(1:end/2) # first half of a => [1, 2]
a(end + 1) = 5; # append element
a(end) = []; # delete element
a(1:2:end) # odd elements of a => [1, 3]
a(2:2:end) # even elements of a => [2, 4]
a(end:-1:1) # reversal of a => [4, 3, 2 , 1]
</pre>
<ul class="menu">
<li><a accesskey="1" href="Advanced-Indexing.html#Advanced-Indexing">Advanced Indexing</a>
</ul>
</body></html>
|