/usr/share/doc/octave/octave.html/Advanced-Indexing.html is in octave-doc 4.2.2-1ubuntu1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 | <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<!-- Created by GNU Texinfo 6.5, http://www.gnu.org/software/texinfo/ -->
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Advanced Indexing (GNU Octave)</title>
<meta name="description" content="Advanced Indexing (GNU Octave)">
<meta name="keywords" content="Advanced Indexing (GNU Octave)">
<meta name="resource-type" content="document">
<meta name="distribution" content="global">
<meta name="Generator" content="makeinfo">
<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.indentedblock {margin-right: 0em}
blockquote.smallindentedblock {margin-right: 0em; font-size: smaller}
blockquote.smallquotation {font-size: smaller}
div.display {margin-left: 3.2em}
div.example {margin-left: 3.2em}
div.lisp {margin-left: 3.2em}
div.smalldisplay {margin-left: 3.2em}
div.smallexample {margin-left: 3.2em}
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.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">
<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>nd</samp>’ dimensions can be indexed by a vector <var>idx</var> which
has from 1 to ‘<samp>nd</samp>’ elements. If any element of <var>idx</var> is not a
scalar then the complete set of index tuples will be generated from the
Cartesian product of the index elements.
</p>
<p>For the ordinary and most common case, the number of indices
(<code>nidx = numel (<var>idx</var>)</code>) matches the number of dimensions ‘<samp>nd</samp>’.
In this case, each element of <var>idx</var> corresponds to its respective dimension,
i.e., <code><var>idx</var>(1)</code> refers to dimension 1, <code><var>idx</var>(2)</code> refers to
dimension 2, etc. If <code>nidx < nd</code><!-- /@w -->, and every index is less than the
size of the array in the <em>i^{th}</em> dimension
(<code><var>idx</var>(i) < size (<var>array</var>, i)</code>), then the index expression is
padded with <code>nd <span class="nolinebreak">-</span> nidx</code><!-- /@w --> trailing singleton dimensions. If
<code>nidx < nd</code><!-- /@w --> but one of the indices <code><var>idx</var>(i)</code> is outside the
size of the current array, then the last <code>nd <span class="nolinebreak">-</span> nidx + 1</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 (nidx == nd): ans = 6
A(2,1); # Case (nidx < nd), idx within array:
# equivalent to A(2,1,1), ans = 2
A(2,4); # Case (nidx < nd), 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>: <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>: <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 linear indices.
</p>
<p>The input <var>dims</var> is a dimension vector where each element is the size of
the array in the respective dimension (see <a href="Object-Sizes.html#XREFsize">size</a>). The remaining
inputs are scalars or vectors of subscripts to be converted.
</p>
<p>The output vector <var>ind</var> contains the converted linear indices.
</p>
<p>Background: Array elements can be specified either by a linear index which
starts at 1 and runs through the number of elements in the array, or they may
be specified with subscripts for the row, column, page, etc. The functions
<code>ind2sub</code> and <code>sub2ind</code> interconvert between the two forms.
</p>
<p>The linear index traverses dimension 1 (rows), then dimension 2 (columns), then
dimension 3 (pages), etc. until it has numbered all of the elements. Consider
the following 3-by-3 matrices:
</p>
<div class="example">
<pre class="example">[(1,1), (1,2), (1,3)] [1, 4, 7]
[(2,1), (2,2), (2,3)] ==> [2, 5, 8]
[(3,1), (3,2), (3,3)] [3, 6, 9]
</pre></div>
<p>The left matrix contains the subscript tuples for each matrix element. The
right matrix shows the linear indices for the same matrix.
</p>
<p>The following example shows how to convert the two-dimensional indices
<code>(2,1)</code> and <code>(2,3)</code> of a 3-by-3 matrix to linear indices with a
single call to <code>sub2ind</code>.
</p>
<div class="example">
<pre class="example">s1 = [2, 2];
s2 = [1, 3];
ind = sub2ind ([3, 3], s1, s2)
⇒ ind = 2 8
</pre></div>
<p><strong>See also:</strong> <a href="#XREFind2sub">ind2sub</a>, <a href="Object-Sizes.html#XREFsize">size</a>.
</p></dd></dl>
<a name="XREFind2sub"></a><dl>
<dt><a name="index-ind2sub"></a>: <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 linear indices to subscripts.
</p>
<p>The input <var>dims</var> is a dimension vector where each element is the size of
the array in the respective dimension (see <a href="Object-Sizes.html#XREFsize">size</a>). The second
input <var>ind</var> contains linear indies to be converted.
</p>
<p>The outputs <var>s1</var>, …, <var>sN</var> contain the converted subscripts.
</p>
<p>Background: Array elements can be specified either by a linear index which
starts at 1 and runs through the number of elements in the array, or they may
be specified with subscripts for the row, column, page, etc. The functions
<code>ind2sub</code> and <code>sub2ind</code> interconvert between the two forms.
</p>
<p>The linear index traverses dimension 1 (rows), then dimension 2 (columns), then
dimension 3 (pages), etc. until it has numbered all of the elements. Consider
the following 3-by-3 matrices:
</p>
<div class="example">
<pre class="example">[1, 4, 7] [(1,1), (1,2), (1,3)]
[2, 5, 8] ==> [(2,1), (2,2), (2,3)]
[3, 6, 9] [(3,1), (3,2), (3,3)]
</pre></div>
<p>The left matrix contains the linear indices for each matrix element. The right
matrix shows the subscript tuples for the same matrix.
</p>
<p>The following example shows how to convert the two-dimensional indices
<code>(2,1)</code> and <code>(2,3)</code> of a 3-by-3 matrix to linear indices with a
single call to <code>sub2ind</code>.
</p>
<p>The following example shows how to convert the linear indices <code>2</code> and
<code>8</code> in a 3-by-3 matrix into subscripts.
</p>
<div class="example">
<pre class="example">ind = [2, 8];
[r, c] = ind2sub ([3, 3], ind)
⇒ r = 2 2
⇒ c = 1 3
</pre></div>
<p>If the number of output subscripts exceeds the number of dimensions, the
exceeded dimensions are set to <code>1</code>. On the other hand, if fewer
subscripts than dimensions are provided, the exceeding dimensions are merged
into the final requested dimension. For clarity, consider the following
examples:
</p>
<div class="example">
<pre class="example">ind = [2, 8];
dims = [3, 3];
## same as dims = [3, 3, 1]
[r, c, s] = ind2sub (dims, ind)
⇒ r = 2 2
⇒ c = 1 3
⇒ s = 1 1
## same as dims = [9]
r = ind2sub (dims, ind)
⇒ r = 2 8
</pre></div>
<p><strong>See also:</strong> <a href="#XREFind2sub">ind2sub</a>, <a href="Object-Sizes.html#XREFsize">size</a>.
</p></dd></dl>
<a name="XREFisindex"></a><dl>
<dt><a name="index-isindex"></a>: <em></em> <strong>isindex</strong> <em>(<var>ind</var>)</em></dt>
<dt><a name="index-isindex-1"></a>: <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.
</p>
<p>Valid indices are either positive integers (although possibly of real data
type), or logical arrays.
</p>
<p>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>
<p>Implementation Note: Strings are first converted to double values before the
checks for valid indices are made. Unless a string contains the NULL
character "\0", it will always be a valid index.
</p></dd></dl>
<a name="XREFallow_005fnoninteger_005frange_005fas_005findex"></a><dl>
<dt><a name="index-allow_005fnoninteger_005frange_005fas_005findex"></a>: <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>: <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>: <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.
</p>
<p>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>
|