<!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>Examples (GNU Octave (version 5.1.0))</title> <meta name="description" content="Examples (GNU Octave (version 5.1.0))"> <meta name="keywords" content="Examples (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="Vectorization-and-Faster-Code-Execution.html#Vectorization-and-Faster-Code-Execution" rel="up" title="Vectorization and Faster Code Execution"> <link href="Nonlinear-Equations.html#Nonlinear-Equations" rel="next" title="Nonlinear Equations"> <link href="Miscellaneous-Techniques.html#Miscellaneous-Techniques" rel="prev" title="Miscellaneous Techniques"> <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="Examples"></a> <div class="header"> <p> Previous: <a href="Miscellaneous-Techniques.html#Miscellaneous-Techniques" accesskey="p" rel="prev">Miscellaneous Techniques</a>, Up: <a href="Vectorization-and-Faster-Code-Execution.html#Vectorization-and-Faster-Code-Execution" accesskey="u" rel="up">Vectorization and Faster Code Execution</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="Examples-1"></a> <h3 class="section">19.7 Examples</h3> <p>The following are examples of vectorization questions asked by actual users of Octave and their solutions. </p> <ul> <li> For a vector <code>A</code>, the following loop <div class="example"> <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></div> <p>can be turned into the following one-liner: </p> <div class="example"> <pre class="example">B = [diff(A)(:), 0.5*(A(1:end-1)+A(2:end))(:)] </pre></div> <p>Note the usage of colon indexing to flatten an intermediate result into a column vector. This is a common vectorization trick. </p> </li></ul> </body> </html>