Sophie

Sophie

distrib > Mageia > 7 > x86_64 > by-pkgid > 641ebb3060c35990cc021d8f7aaf9aca > files > 83

octave-doc-5.1.0-7.1.mga7.noarch.rpm

<!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 (version 5.1.0))</title>

<meta name="description" content="Advanced Indexing (GNU Octave (version 5.1.0))">
<meta name="keywords" content="Advanced Indexing (GNU Octave (version 5.1.0))">
<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> &nbsp; [<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>When it is necessary to extract subsets of entries out of an array whose
indices cannot be written as a Cartesian product of components, linear
indexing together with the function <code>sub2ind</code> can be used.  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(sub2ind (size (A), [1, 2, 1], [1, 1, 2], [1, 2, 1]))
   &rArr; ans = [A(1, 1, 1), A(2, 1, 2), A(1, 2, 1)]
</pre></div>

<p>An array with &lsquo;<samp>nd</samp>&rsquo; dimensions can be indexed by an index expression which
has from 1 to &lsquo;<samp>nd</samp>&rsquo; components.  For the ordinary and most common case, the
number of components &lsquo;<samp>M</samp>&rsquo; matches the number of dimensions &lsquo;<samp>nd</samp>&rsquo;.  In
this case the ordinary indexing rules apply and each component corresponds to
the respective dimension of the array.
</p>
<p>However, if the number of indexing components exceeds the number of dimensions
(<code>M&nbsp;&gt;&nbsp;nd</code><!-- /@w -->) then the excess components must all be singletons
(<code>1</code>).  Moreover, if <code>M&nbsp;&lt;&nbsp;nd</code><!-- /@w -->, the behavior is equivalent to
reshaping the input object so as to merge the trailing <code>nd&nbsp;<span class="nolinebreak">-</span>&nbsp;M</code><!-- /@w -->
dimensions into the last index dimension <code>M</code>.  Thus, the result will have
the dimensionality of the index expression, and not the original object.  This
is the case whenever dimensionality of the index is greater than one
(<code>M&nbsp;&gt;&nbsp;1</code><!-- /@w -->), so that the special rules for linear indexing are not
applied.  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

## 2-D indexing causes third dimension to be merged into second dimension.
## Equivalent array for indexing, Atmp, is now 2x4.
Atmp = reshape (A, 2, 4)
Atmp =

   1   3   5   7
   2   4   6   8


A(2,1)   # Reshape to 2x4 matrix, second entry of first column: ans = 2
A(2,4)   # Reshape to 2x4 matrix, second entry of fourth column: ans = 8
A(:,:)   # Reshape to 2x4 matrix, select all rows &amp; columns, ans = Atmp
</pre></div>

<p>Note here the elegant use of the double colon to replace the call to the
<code>reshape</code> function.
</p>
<p>Another advanced use of linear 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 row 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.  This could also have been
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 (M, N, &hellip;)</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">{&quot;Hello&quot;}(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 object (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 &lsquo;<samp>r</samp>&rsquo; 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 &lsquo;<samp>r</samp>&rsquo; and &lsquo;<samp>n</samp>&rsquo; 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 users unfamiliar with these techniques is to use
the function <code>repmat</code> for replicating smaller arrays into bigger ones,
which uses such tricks.
</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>, &hellip;, <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)] ==&gt; [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)
    &rArr; 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>, &hellip;, <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>, &hellip;, <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] ==&gt; [(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)
    &rArr; r =  2   2
    &rArr; 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)
    &rArr; r =  2   2
    &rArr; c =  1   3
    &rArr; s =  1   1
## same as dims = [9]
r = ind2sub (dims, ind)
    &rArr; 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 &quot;\0&quot;, it will always be a valid index.
</p></dd></dl>


<hr>
<div class="header">
<p>
Up: <a href="Index-Expressions.html#Index-Expressions" accesskey="u" rel="up">Index Expressions</a> &nbsp; [<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>