Sophie

Sophie

distrib > Mageia > 4 > i586 > by-pkgid > b38d2da330d1936e5ab1307c039c4941 > files > 241

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

<html lang="en">
<head>
<title>Examples - 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="Vectorization-and-Faster-Code-Execution.html#Vectorization-and-Faster-Code-Execution" title="Vectorization and Faster Code Execution">
<link rel="prev" href="Miscellaneous-Techniques.html#Miscellaneous-Techniques" title="Miscellaneous Techniques">
<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="Examples"></a>
<p>
Previous:&nbsp;<a rel="previous" accesskey="p" href="Miscellaneous-Techniques.html#Miscellaneous-Techniques">Miscellaneous Techniques</a>,
Up:&nbsp;<a rel="up" accesskey="u" href="Vectorization-and-Faster-Code-Execution.html#Vectorization-and-Faster-Code-Execution">Vectorization and Faster Code Execution</a>
<hr>
</div>

<h3 class="section">19.6 Examples</h3>

<p>The following are examples of vectorization questions asked by actual
users of Octave and their solutions.

<!-- FIXME: We need a lot more examples here. -->
     <ul>
<li>For a vector <code>A</code>, the following loop

     <pre class="example">          n = length (A);
          B = zeros (n, 2);
          for i = 1:length(A)
            ## this will be two columns, the first is the difference and
            ## the second the mean of the two elements used for the diff.
            B(i,:) = [A(i+1)-A(i), (A(i+1) + A(i))/2)];
          endfor
</pre>
     <p class="noindent">can be turned into the following one-liner:

     <pre class="example">          B = [diff(A)(:), 0.5*(A(1:end-1)+A(2:end))(:)]
</pre>
     <p>Note the usage of colon indexing to flatten an intermediate result into
a column vector.  This is a common vectorization trick.

   </ul>

<!-- DO NOT EDIT!  Generated automatically by munge-texi.pl. -->
<!-- Copyright (C) 1996-2012 John W. Eaton -->
<!-- This file is part of Octave. -->
<!-- Octave is free software; you can redistribute it and/or modify it -->
<!-- under the terms of the GNU General Public License as published by the -->
<!-- Free Software Foundation; either version 3 of the License, or (at -->
<!-- your option) any later version. -->
<!-- Octave is distributed in the hope that it will be useful, but WITHOUT -->
<!-- ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or -->
<!-- FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License -->
<!-- for more details. -->
<!-- You should have received a copy of the GNU General Public License -->
<!-- along with Octave; see the file COPYING.  If not, see -->
<!-- <http://www.gnu.org/licenses/>. -->
   </body></html>