Sophie

Sophie

distrib > Mageia > 4 > x86_64 > by-pkgid > b38d2da330d1936e5ab1307c039c4941 > files > 304

octave-doc-3.6.4-3.mga4.noarch.rpm

<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:&nbsp;<a rel="next" accesskey="n" href="Calling-Functions.html#Calling-Functions">Calling Functions</a>,
Up:&nbsp;<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
&lsquo;<samp><span class="samp">:</span></samp>&rsquo;, 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 =&gt; [1, 2]
     a(end + 1) = 5;   # append element
     a(end) = [];      # delete element
     a(1:2:end)        # odd elements of a =&gt; [1, 3]
     a(2:2:end)        # even elements of a =&gt; [2, 4]
     a(end:-1:1)       # reversal of a =&gt; [4, 3, 2 , 1]
</pre>
   <ul class="menu">
<li><a accesskey="1" href="Advanced-Indexing.html#Advanced-Indexing">Advanced Indexing</a>
</ul>

   </body></html>