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 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212
|
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<!-- Created by GNU Texinfo 6.7, http://www.gnu.org/software/texinfo/ -->
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Index Expressions (GNU Octave (version 6.2.0))</title>
<meta name="description" content="Index Expressions (GNU Octave (version 6.2.0))">
<meta name="keywords" content="Index Expressions (GNU Octave (version 6.2.0))">
<meta name="resource-type" content="document">
<meta name="distribution" content="global">
<meta name="Generator" content="makeinfo">
<link href="index.html" rel="start" title="Top">
<link href="Concept-Index.html" rel="index" title="Concept Index">
<link href="index.html#SEC_Contents" rel="contents" title="Table of Contents">
<link href="Expressions.html" rel="up" title="Expressions">
<link href="Advanced-Indexing.html" rel="next" title="Advanced Indexing">
<link href="Expressions.html" rel="prev" title="Expressions">
<style type="text/css">
<!--
a.summary-letter {text-decoration: none}
blockquote.indentedblock {margin-right: 0em}
div.display {margin-left: 3.2em}
div.example {margin-left: 3.2em}
div.lisp {margin-left: 3.2em}
kbd {font-style: oblique}
pre.display {font-family: inherit}
pre.format {font-family: inherit}
pre.menu-comment {font-family: serif}
pre.menu-preformatted {font-family: serif}
span.nolinebreak {white-space: nowrap}
span.roman {font-family: initial; font-weight: normal}
span.sansserif {font-family: sans-serif; font-weight: normal}
ul.no-bullet {list-style: none}
-->
</style>
<link rel="stylesheet" type="text/css" href="octave.css">
</head>
<body lang="en">
<span id="Index-Expressions"></span><div class="header">
<p>
Next: <a href="Calling-Functions.html" accesskey="n" rel="next">Calling Functions</a>, Up: <a href="Expressions.html" accesskey="u" rel="up">Expressions</a> [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Concept-Index.html" title="Index" rel="index">Index</a>]</p>
</div>
<hr>
<span id="Index-Expressions-1"></span><h3 class="section">8.1 Index Expressions</h3>
<span id="index-_0028"></span>
<span id="index-_0029"></span>
<span id="index-_003a"></span>
<p>An <em>index expression</em> allows you to reference or extract selected
elements of a vector, a matrix (2-D), or a higher-dimensional array.
</p>
<p>Indices may be scalars, vectors, ranges, or the special operator ‘<samp>:</samp>’,
which selects entire rows, columns, or higher-dimensional slices.
</p>
<p>An index expression consists of a set of parentheses enclosing <em>M</em>
expressions separated by commas. Each individual index value, or component,
is used for the respective dimension of the object that it is applied to. In
other words, the first index component is used for the first dimension (rows)
of the object, the second index component is used for the second dimension
(columns) of the object, and so on. The number of index components <em>M</em>
defines the dimensionality of the index expression. An index with two
components would be referred to as a 2-D index because it has two dimensions.
</p>
<p>In the simplest case, 1) all components are scalars, and 2) the dimensionality
of the index expression <em>M</em> is equal to the dimensionality of the object
it is applied to. For example:
</p>
<div class="example">
<pre class="example">A = reshape (1:8, 2, 2, 2) # Create 3-D array
A =
ans(:,:,1) =
1 3
2 4
ans(:,:,2) =
5 7
6 8
A(2, 1, 2) # second row, first column of second slice
# in third dimension: ans = 6
</pre></div>
<p>The size of the returned object in a specific dimension is equal to the number
of elements in the corresponding component of the index expression. When all
components are scalars, the result is a single output value. However, if any
component is a vector or range then the returned values are the Cartesian
product of the indices in the respective dimensions. For example:
</p>
<div class="example">
<pre class="example">A([1, 2], 1, 2) ≡ [A(1,1,2); A(2,1,2)]
⇒
ans =
5
6
</pre></div>
<p>The total number of returned values is the product of the number of elements
returned for each index component. In the example above, the total is 2*1*1 =
2 elements.
</p>
<p>Notice that the size of the returned object in a given dimension is equal to
the number of elements in the index expression for that dimension. In the code
above, the first index component (<code>[1, 2]</code>) was specified as a row vector,
but its shape is unimportant. The important fact is that the component
specified two values, and hence the result must have a size of two in the first
dimension; and because the first dimension corresponds to rows, the overall
result is a column vector.
</p>
<div class="example">
<pre class="example">A(1, [2, 1, 1], 1) # result is a row vector: ans = [3, 1, 1]
A(ones (2, 2), 1, 1) # result is a column vector: ans = [1; 1; 1; 1]
</pre></div>
<p>The first line demonstrates again that the size of the output in a given
dimension is equal to the number of elements in the respective indexing
component. In this case, the output has three elements in the second dimension
(which corresponds to columns), so the result is a row vector. The example
also shows how repeating entries in the index expression can be used to
replicate elements in the output. The last example further proves that the
shape of the indexing component is irrelevant, it is only the number of
elements (2x2 = 4) which is important.
</p>
<p>The above rules apply whenever the dimensionality of the index expression
is greater than one (<em>M > 1</em>). However, for one-dimensional index
expressions special rules apply and the shape of the output <strong>is</strong>
determined by the shape of the indexing component. For example:
</p>
<div class="example">
<pre class="example">A([1, 2]) # result is a row vector: ans = [1, 2]
A([1; 2]) # result is a column vector: ans = [1; 2]
</pre></div>
<p>Note that it is permissible to use a 1-D index with a multi-dimensional
object (also called linear indexing). In this case, the elements of the
multi-dimensional array are taken in column-first order like Fortran. That is,
the columns of the array are imagined to be stacked on top of each other to
form a column vector and then the single linear index is applied to this
vector.
</p>
<div class="example">
<pre class="example">A(5) # linear indexing into three-dimensional array: ans = 5
A(3:5) # result has shape of index component: ans = [3, 4, 5]
</pre></div>
<span id="index-_003a_002c-indexing-expressions"></span>
<p>A colon (‘<samp>:</samp>’) may be used as an index component to select all of the
elements in a specified dimension. Given the matrix,
</p>
<div class="example">
<pre class="example">A = [1, 2; 3, 4]
</pre></div>
<p>all of the following expressions are equivalent and select the first row of the
matrix.
</p>
<div class="example">
<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></div>
<p>When a colon is used in the special case of 1-D indexing the result is always a
column vector. Creating column vectors with a colon index is a very frequently
encountered code idiom and is faster and generally clearer than calling
<code>reshape</code> for this case.
</p>
<div class="example">
<pre class="example">A(:) # result is column vector: ans = [1; 2; 3; 4]
A(:)' # result is row vector: ans = [1, 2, 3, 4]
</pre></div>
<span id="index-end_002c-indexing"></span>
<span id="index-end_003aend_003a-and-_003aend"></span>
<p>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:
</p>
<div class="example">
<pre class="example">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></div>
<table class="menu" border="0" cellspacing="0">
<tr><td align="left" valign="top">• <a href="Advanced-Indexing.html" accesskey="1">Advanced Indexing</a></td><td> </td><td align="left" valign="top">
</td></tr>
</table>
<hr>
<div class="header">
<p>
Next: <a href="Calling-Functions.html" accesskey="n" rel="next">Calling Functions</a>, Up: <a href="Expressions.html" accesskey="u" rel="up">Expressions</a> [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Concept-Index.html" title="Index" rel="index">Index</a>]</p>
</div>
</body>
</html>
|