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 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286
|
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<!-- Created by GNU Texinfo 5.2, http://www.gnu.org/software/texinfo/ -->
<head>
<title>GNU Octave: Advanced Indexing</title>
<meta name="description" content="GNU Octave: Advanced Indexing">
<meta name="keywords" content="GNU Octave: Advanced Indexing">
<meta name="resource-type" content="document">
<meta name="distribution" content="global">
<meta name="Generator" content="makeinfo">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<link href="index.html#Top" rel="start" title="Top">
<link href="Concept-Index.html#Concept-Index" rel="index" title="Concept Index">
<link href="index.html#SEC_Contents" rel="contents" title="Table of Contents">
<link href="Index-Expressions.html#Index-Expressions" rel="up" title="Index Expressions">
<link href="Calling-Functions.html#Calling-Functions" rel="next" title="Calling Functions">
<link href="Index-Expressions.html#Index-Expressions" rel="prev" title="Index Expressions">
<style type="text/css">
<!--
a.summary-letter {text-decoration: none}
blockquote.smallquotation {font-size: smaller}
div.display {margin-left: 3.2em}
div.example {margin-left: 3.2em}
div.indentedblock {margin-left: 3.2em}
div.lisp {margin-left: 3.2em}
div.smalldisplay {margin-left: 3.2em}
div.smallexample {margin-left: 3.2em}
div.smallindentedblock {margin-left: 3.2em; font-size: smaller}
div.smalllisp {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}
pre.smalldisplay {font-family: inherit; font-size: smaller}
pre.smallexample {font-size: smaller}
pre.smallformat {font-family: inherit; font-size: smaller}
pre.smalllisp {font-size: smaller}
span.nocodebreak {white-space:nowrap}
span.nolinebreak {white-space:nowrap}
span.roman {font-family:serif; font-weight:normal}
span.sansserif {font-family:sans-serif; font-weight:normal}
ul.no-bullet {list-style: none}
-->
</style>
</head>
<body lang="en" bgcolor="#FFFFFF" text="#000000" link="#0000FF" vlink="#800080" alink="#FF0000">
<a name="Advanced-Indexing"></a>
<div class="header">
<p>
Up: <a href="Index-Expressions.html#Index-Expressions" accesskey="u" rel="up">Index Expressions</a> [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Concept-Index.html#Concept-Index" title="Index" rel="index">Index</a>]</p>
</div>
<hr>
<a name="Advanced-Indexing-1"></a>
<h4 class="subsection">8.1.1 Advanced Indexing</h4>
<p>An array with ‘<samp>n</samp>’ dimensions can be indexed using ‘<samp>m</samp>’
indices. More generally, the set of index tuples determining the
result is formed by the Cartesian product of the index vectors (or
ranges or scalars).
</p>
<p>For the ordinary and most common case, <code>m == n</code><!-- /@w -->, and each
index corresponds to its respective dimension. If <code>m < n</code><!-- /@w -->
and every index is less than the size of the array in the
<em>i^{th}</em> dimension, <code>m(i) < n(i)</code>, then the index expression
is padded with trailing singleton dimensions (<code>[ones (m-n, 1)]</code>).
If <code>m < n</code><!-- /@w --> but one of the indices <code>m(i)</code> is outside the
size of the current array, then the last <code><span class="nolinebreak">n-m+1</span></code><!-- /@w --> dimensions
are folded into a single dimension with an extent equal to the product
of extents of the original dimensions. This is easiest to understand
with an 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); # Case (m == n): ans = 6
a(2,1); # Case (m < n), idx within array:
# equivalent to a(2,1,1), ans = 2
a(2,4); # Case (m < n), idx outside array:
# Dimension 2 & 3 folded into new dimension of size 2x2 = 4
# Select 2nd row, 4th element of [2, 4, 6, 8], ans = 8
</pre></div>
<p>One advanced use of indexing is to create arrays filled with a single
value. This can be done by using an index of ones on a scalar value.
The result is an object with the dimensions of the index expression
and every element equal to the original scalar. For example, the
following statements
</p>
<div class="example">
<pre class="example">a = 13;
a(ones (1, 4))
</pre></div>
<p>produce a vector whose four elements are all equal to 13.
</p>
<p>Similarly, by indexing a scalar with two vectors of ones it is
possible to create a matrix. The following statements
</p>
<div class="example">
<pre class="example">a = 13;
a(ones (1, 2), ones (1, 3))
</pre></div>
<p>create a 2x3 matrix with all elements equal to 13.
</p>
<p>The last example could also be written as
</p>
<div class="example">
<pre class="example">13(ones (2, 3))
</pre></div>
<p>It is more efficient to use indexing rather than the code construction
<code>scalar * ones (N, M, …)</code> because it avoids the unnecessary
multiplication operation. Moreover, multiplication may not be
defined for the object to be replicated whereas indexing an array is
always defined. The following code shows how to create a 2x3 cell
array from a base unit which is not itself a scalar.
</p>
<div class="example">
<pre class="example">{"Hello"}(ones (2, 3))
</pre></div>
<p>It should be, noted that <code>ones (1, n)</code> (a row vector of ones)
results in a range (with zero increment). A range is stored
internally as a starting value, increment, end value, and total number
of values; hence, it is more efficient for storage than a vector or
matrix of ones whenever the number of elements is greater than 4. In
particular, when ‘<samp>r</samp>’ is a row vector, the expressions
</p>
<div class="example">
<pre class="example"> r(ones (1, n), :)
</pre></div>
<div class="example">
<pre class="example"> r(ones (n, 1), :)
</pre></div>
<p>will produce identical results, but the first one will be
significantly faster, at least for ‘<samp>r</samp>’ and ‘<samp>n</samp>’ large enough.
In the first case the index is held in compressed form as a range
which allows Octave to choose a more efficient algorithm to handle the
expression.
</p>
<p>A general recommendation, for a user unaware of these subtleties, is
to use the function <code>repmat</code> for replicating smaller arrays into
bigger ones.
</p>
<p>A second use of indexing is to speed up code. Indexing is a fast
operation and judicious use of it can reduce the requirement for
looping over individual array elements which is a slow operation.
</p>
<p>Consider the following example which creates a 10-element row vector
<em>a</em> containing the values
a(i) = sqrt (i).
</p>
<div class="example">
<pre class="example">for i = 1:10
a(i) = sqrt (i);
endfor
</pre></div>
<p>It is quite inefficient to create a vector using a loop like this. In
this case, it would have been much more efficient to use the
expression
</p>
<div class="example">
<pre class="example">a = sqrt (1:10);
</pre></div>
<p>which avoids the loop entirely.
</p>
<p>In cases where a loop cannot be avoided, or a number of values must be
combined to form a larger matrix, it is generally faster to set the
size of the matrix first (pre-allocate storage), and then insert
elements using indexing commands. For example, given a matrix
<code>a</code>,
</p>
<div class="example">
<pre class="example">[nr, nc] = size (a);
x = zeros (nr, n * nc);
for i = 1:n
x(:,(i-1)*nc+1:i*nc) = a;
endfor
</pre></div>
<p>is considerably faster than
</p>
<div class="example">
<pre class="example">x = a;
for i = 1:n-1
x = [x, a];
endfor
</pre></div>
<p>because Octave does not have to repeatedly resize the intermediate
result.
</p>
<a name="XREFsub2ind"></a><dl>
<dt><a name="index-sub2ind"></a>Function File: <em><var>ind</var> =</em> <strong>sub2ind</strong> <em>(<var>dims</var>, <var>i</var>, <var>j</var>)</em></dt>
<dt><a name="index-sub2ind-1"></a>Function File: <em><var>ind</var> =</em> <strong>sub2ind</strong> <em>(<var>dims</var>, <var>s1</var>, <var>s2</var>, …, <var>sN</var>)</em></dt>
<dd><p>Convert subscripts to a linear index.
</p>
<p>The following example shows how to convert the two-dimensional
index <code>(2,3)</code> of a 3-by-3 matrix to a linear index. The matrix
is linearly indexed moving from one column to next, filling up
all rows in each column.
</p>
<div class="example">
<pre class="example">linear_index = sub2ind ([3, 3], 2, 3)
⇒ 8
</pre></div>
<p><strong>See also:</strong> <a href="#XREFind2sub">ind2sub</a>.
</p></dd></dl>
<a name="XREFind2sub"></a><dl>
<dt><a name="index-ind2sub"></a>Function File: <em>[<var>s1</var>, <var>s2</var>, …, <var>sN</var>] =</em> <strong>ind2sub</strong> <em>(<var>dims</var>, <var>ind</var>)</em></dt>
<dd><p>Convert a linear index to subscripts.
</p>
<p>The following example shows how to convert the linear index <code>8</code>
in a 3-by-3 matrix into a subscript. The matrix is linearly indexed
moving from one column to next, filling up all rows in each column.
</p>
<div class="example">
<pre class="example">[r, c] = ind2sub ([3, 3], 8)
⇒ r = 2
⇒ c = 3
</pre></div>
<p><strong>See also:</strong> <a href="#XREFsub2ind">sub2ind</a>.
</p></dd></dl>
<a name="XREFisindex"></a><dl>
<dt><a name="index-isindex"></a>Built-in Function: <em></em> <strong>isindex</strong> <em>(<var>ind</var>)</em></dt>
<dt><a name="index-isindex-1"></a>Built-in Function: <em></em> <strong>isindex</strong> <em>(<var>ind</var>, <var>n</var>)</em></dt>
<dd><p>Return true if <var>ind</var> is a valid index. Valid indices are
either positive integers (although possibly of real data type), or logical
arrays. If present, <var>n</var> specifies the maximum extent of the dimension
to be indexed. When possible the internal result is cached so that
subsequent indexing using <var>ind</var> will not perform the check again.
</p></dd></dl>
<a name="XREFallow_005fnoninteger_005frange_005fas_005findex"></a><dl>
<dt><a name="index-allow_005fnoninteger_005frange_005fas_005findex"></a>Built-in Function: <em><var>val</var> =</em> <strong>allow_noninteger_range_as_index</strong> <em>()</em></dt>
<dt><a name="index-allow_005fnoninteger_005frange_005fas_005findex-1"></a>Built-in Function: <em><var>old_val</var> =</em> <strong>allow_noninteger_range_as_index</strong> <em>(<var>new_val</var>)</em></dt>
<dt><a name="index-allow_005fnoninteger_005frange_005fas_005findex-2"></a>Built-in Function: <em></em> <strong>allow_noninteger_range_as_index</strong> <em>(<var>new_val</var>, "local")</em></dt>
<dd><p>Query or set the internal variable that controls whether non-integer
ranges are allowed as indices. This might be useful for <small>MATLAB</small>
compatibility; however, it is still not entirely compatible because
<small>MATLAB</small> treats the range expression differently in different contexts.
</p>
<p>When called from inside a function with the <code>"local"</code> option, the
variable is changed locally for the function and any subroutines it calls.
The original variable value is restored when exiting the function.
</p></dd></dl>
<hr>
<div class="header">
<p>
Up: <a href="Index-Expressions.html#Index-Expressions" accesskey="u" rel="up">Index Expressions</a> [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Concept-Index.html#Concept-Index" title="Index" rel="index">Index</a>]</p>
</div>
</body>
</html>
|