<html lang="en"> <head> <title>Basic Vectorization - 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="next" href="Broadcasting.html#Broadcasting" title="Broadcasting"> <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="Basic-Vectorization"></a> <p> Next: <a rel="next" accesskey="n" href="Broadcasting.html#Broadcasting">Broadcasting</a>, Up: <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.1 Basic Vectorization</h3> <p>To a very good first approximation, the goal in vectorization is to write code that avoids loops and uses whole-array operations. As a trivial example, consider <pre class="example"> for i = 1:n for j = 1:m c(i,j) = a(i,j) + b(i,j); endfor endfor </pre> <p class="noindent">compared to the much simpler <pre class="example"> c = a + b; </pre> <p class="noindent">This isn't merely easier to write; it is also internally much easier to optimize. Octave delegates this operation to an underlying implementation which, among other optimizations, may use special vector hardware instructions or could conceivably even perform the additions in parallel. In general, if the code is vectorized, the underlying implementation has more freedom about the assumptions it can make in order to achieve faster execution. <p>This is especially important for loops with "cheap" bodies. Often it suffices to vectorize just the innermost loop to get acceptable performance. A general rule of thumb is that the "order" of the vectorized body should be greater or equal to the "order" of the enclosing loop. <p>As a less trivial example, instead of <pre class="example"> for i = 1:n-1 a(i) = b(i+1) - b(i); endfor </pre> <p class="noindent">write <pre class="example"> a = b(2:n) - b(1:n-1); </pre> <p>This shows an important general concept about using arrays for indexing instead of looping over an index variable. See <a href="Index-Expressions.html#Index-Expressions">Index Expressions</a>. Also use boolean indexing generously. If a condition needs to be tested, this condition can also be written as a boolean index. For instance, instead of <pre class="example"> for i = 1:n if a(i) > 5 a(i) -= 20 endif endfor </pre> <p class="noindent">write <pre class="example"> a(a>5) -= 20; </pre> <p class="noindent">which exploits the fact that <code>a > 5</code> produces a boolean index. <p>Use elementwise vector operators whenever possible to avoid looping (operators like <code>.*</code> and <code>.^</code>). See <a href="Arithmetic-Ops.html#Arithmetic-Ops">Arithmetic Ops</a>. For simple inline functions, the <code>vectorize</code> function can do this automatically. <!-- vectorize src/ov-fcn-inline.cc --> <p><a name="doc_002dvectorize"></a> <div class="defun"> — Built-in Function: <b>vectorize</b> (<var>fun</var>)<var><a name="index-vectorize-2125"></a></var><br> <blockquote><p>Create a vectorized version of the inline function <var>fun</var> by replacing all occurrences of <code>*</code>, <code>/</code>, etc., with <code>.*</code>, <code>./</code>, etc. <p>This may be useful, for example, when using inline functions with numerical integration or optimization where a vector-valued function is expected. <pre class="example"> fcn = vectorize (inline ("x^2 - 1")) ⇒ fcn = f(x) = x.^2 - 1 quadv (fcn, 0, 3) ⇒ 6 </pre> <!-- Texinfo @sp should work but in practice produces ugly results for HTML. --> <!-- A simple blank line produces the correct behavior. --> <!-- @sp 1 --> <p class="noindent"><strong>See also:</strong> <a href="doc_002dinline.html#doc_002dinline">inline</a>, <a href="doc_002dformula.html#doc_002dformula">formula</a>, <a href="doc_002dargnames.html#doc_002dargnames">argnames</a>. </p></blockquote></div> <p>Also exploit broadcasting in these elementwise operators both to avoid looping and unnecessary intermediate memory allocations. See <a href="Broadcasting.html#Broadcasting">Broadcasting</a>. <p>Use built-in and library functions if possible. Built-in and compiled functions are very fast. Even with an m-file library function, chances are good that it is already optimized, or will be optimized more in a future release. <p>For instance, even better than <pre class="example"> a = b(2:n) - b(1:n-1); </pre> <p class="noindent">is <pre class="example"> a = diff (b); </pre> <p>Most Octave functions are written with vector and array arguments in mind. If you find yourself writing a loop with a very simple operation, chances are that such a function already exists. The following functions occur frequently in vectorized code: <ul> <li>Index manipulation <ul> <li>find <li>sub2ind <li>ind2sub <li>sort <li>unique <li>lookup <li>ifelse / merge </ul> <li>Repetition <ul> <li>repmat <li>repelems </ul> <li>Vectorized arithmetic <ul> <li>sum <li>prod <li>cumsum <li>cumprod <li>sumsq <li>diff <li>dot <li>cummax <li>cummin </ul> <li>Shape of higher dimensional arrays <ul> <li>reshape <li>resize <li>permute <li>squeeze <li>deal </ul> </ul> </body></html>