/usr/share/doc/octave/octave.html/Function-Application.html is in octave-doc 4.2.2-1ubuntu1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 | <!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>Function Application (GNU Octave)</title>
<meta name="description" content="Function Application (GNU Octave)">
<meta name="keywords" content="Function Application (GNU Octave)">
<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="Accumulation.html#Accumulation" rel="next" title="Accumulation">
<link href="Broadcasting.html#Broadcasting" rel="prev" title="Broadcasting">
<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="Function-Application"></a>
<div class="header">
<p>
Next: <a href="Accumulation.html#Accumulation" accesskey="n" rel="next">Accumulation</a>, Previous: <a href="Broadcasting.html#Broadcasting" accesskey="p" rel="prev">Broadcasting</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="Function-Application-1"></a>
<h3 class="section">19.3 Function Application</h3>
<a name="index-map"></a>
<a name="index-apply"></a>
<a name="index-function-application"></a>
<p>As a general rule, functions should already be written with matrix
arguments in mind and should consider whole matrix operations in a
vectorized manner. Sometimes, writing functions in this way appears
difficult or impossible for various reasons. For those situations,
Octave provides facilities for applying a function to each element of an
array, cell, or struct.
</p>
<a name="XREFarrayfun"></a><dl>
<dt><a name="index-arrayfun"></a>: <em></em> <strong>arrayfun</strong> <em>(<var>func</var>, <var>A</var>)</em></dt>
<dt><a name="index-arrayfun-1"></a>: <em><var>x</var> =</em> <strong>arrayfun</strong> <em>(<var>func</var>, <var>A</var>)</em></dt>
<dt><a name="index-arrayfun-2"></a>: <em><var>x</var> =</em> <strong>arrayfun</strong> <em>(<var>func</var>, <var>A</var>, <var>b</var>, …)</em></dt>
<dt><a name="index-arrayfun-3"></a>: <em>[<var>x</var>, <var>y</var>, …] =</em> <strong>arrayfun</strong> <em>(<var>func</var>, <var>A</var>, …)</em></dt>
<dt><a name="index-arrayfun-4"></a>: <em></em> <strong>arrayfun</strong> <em>(…, "UniformOutput", <var>val</var>)</em></dt>
<dt><a name="index-arrayfun-5"></a>: <em></em> <strong>arrayfun</strong> <em>(…, "ErrorHandler", <var>errfunc</var>)</em></dt>
<dd>
<p>Execute a function on each element of an array.
</p>
<p>This is useful for functions that do not accept array arguments. If the
function does accept array arguments it is better to call the function
directly.
</p>
<p>The first input argument <var>func</var> can be a string, a function
handle, an inline function, or an anonymous function. The input
argument <var>A</var> can be a logic array, a numeric array, a string
array, a structure array, or a cell array. By a call of the function
<code>arrayfun</code> all elements of <var>A</var> are passed on to the named
function <var>func</var> individually.
</p>
<p>The named function can also take more than two input arguments, with
the input arguments given as third input argument <var>b</var>, fourth
input argument <var>c</var>, … If given more than one array input
argument then all input arguments must have the same sizes, for
example:
</p>
<div class="example">
<pre class="example">arrayfun (@atan2, [1, 0], [0, 1])
⇒ [ 1.57080 0.00000 ]
</pre></div>
<p>If the parameter <var>val</var> after a further string input argument
<code>"UniformOutput"</code> is set <code>true</code> (the default), then the named
function <var>func</var> must return a single element which then will be
concatenated into the return value and is of type matrix. Otherwise,
if that parameter is set to <code>false</code>, then the outputs are
concatenated in a cell array. For example:
</p>
<div class="example">
<pre class="example">arrayfun (@(x,y) x:y, "abc", "def", "UniformOutput", false)
⇒
{
[1,1] = abcd
[1,2] = bcde
[1,3] = cdef
}
</pre></div>
<p>If more than one output arguments are given then the named function
must return the number of return values that also are expected, for
example:
</p>
<div class="example">
<pre class="example">[A, B, C] = arrayfun (@find, [10; 0], "UniformOutput", false)
⇒
A =
{
[1,1] = 1
[2,1] = [](0x0)
}
B =
{
[1,1] = 1
[2,1] = [](0x0)
}
C =
{
[1,1] = 10
[2,1] = [](0x0)
}
</pre></div>
<p>If the parameter <var>errfunc</var> after a further string input argument
<code>"ErrorHandler"</code> is another string, a function handle, an inline
function, or an anonymous function, then <var>errfunc</var> defines a
function to call in the case that <var>func</var> generates an error.
The definition of the function must be of the form
</p>
<div class="example">
<pre class="example">function […] = errfunc (<var>s</var>, …)
</pre></div>
<p>where there is an additional input argument to <var>errfunc</var>
relative to <var>func</var>, given by <var>s</var>. This is a structure with
the elements <code>"identifier"</code>, <code>"message"</code>, and
<code>"index"</code> giving, respectively, the error identifier, the error
message, and the index of the array elements that caused the error. The
size of the output argument of <var>errfunc</var> must have the same size as the
output argument of <var>func</var>, otherwise a real error is thrown. For
example:
</p>
<div class="example">
<pre class="example">function y = ferr (s, x), y = "MyString"; endfunction
arrayfun (@str2num, [1234],
"UniformOutput", false, "ErrorHandler", @ferr)
⇒
{
[1,1] = MyString
}
</pre></div>
<p><strong>See also:</strong> <a href="#XREFspfun">spfun</a>, <a href="#XREFcellfun">cellfun</a>, <a href="#XREFstructfun">structfun</a>.
</p></dd></dl>
<a name="XREFspfun"></a><dl>
<dt><a name="index-spfun"></a>: <em><var>y</var> =</em> <strong>spfun</strong> <em>(<var>f</var>, <var>S</var>)</em></dt>
<dd><p>Compute <code>f(<var>S</var>)</code> for the nonzero values of <var>S</var>.
</p>
<p>This results in a sparse matrix with the same structure as <var>S</var>. The
function <var>f</var> can be passed as a string, a function handle, or an
inline function.
</p>
<p><strong>See also:</strong> <a href="#XREFarrayfun">arrayfun</a>, <a href="#XREFcellfun">cellfun</a>, <a href="#XREFstructfun">structfun</a>.
</p></dd></dl>
<a name="XREFcellfun"></a><dl>
<dt><a name="index-cellfun"></a>: <em></em> <strong>cellfun</strong> <em>(<var>name</var>, <var>C</var>)</em></dt>
<dt><a name="index-cellfun-1"></a>: <em></em> <strong>cellfun</strong> <em>("size", <var>C</var>, <var>k</var>)</em></dt>
<dt><a name="index-cellfun-2"></a>: <em></em> <strong>cellfun</strong> <em>("isclass", <var>C</var>, <var>class</var>)</em></dt>
<dt><a name="index-cellfun-3"></a>: <em></em> <strong>cellfun</strong> <em>(<var>func</var>, <var>C</var>)</em></dt>
<dt><a name="index-cellfun-4"></a>: <em></em> <strong>cellfun</strong> <em>(<var>func</var>, <var>C</var>, <var>D</var>)</em></dt>
<dt><a name="index-cellfun-5"></a>: <em>[<var>a</var>, …] =</em> <strong>cellfun</strong> <em>(…)</em></dt>
<dt><a name="index-cellfun-6"></a>: <em></em> <strong>cellfun</strong> <em>(…, "ErrorHandler", <var>errfunc</var>)</em></dt>
<dt><a name="index-cellfun-7"></a>: <em></em> <strong>cellfun</strong> <em>(…, "UniformOutput", <var>val</var>)</em></dt>
<dd>
<p>Evaluate the function named <var>name</var> on the elements of the cell array
<var>C</var>.
</p>
<p>Elements in <var>C</var> are passed on to the named function individually. The
function <var>name</var> can be one of the functions
</p>
<dl compact="compact">
<dt><code>isempty</code></dt>
<dd><p>Return 1 for empty elements.
</p>
</dd>
<dt><code>islogical</code></dt>
<dd><p>Return 1 for logical elements.
</p>
</dd>
<dt><code>isnumeric</code></dt>
<dd><p>Return 1 for numeric elements.
</p>
</dd>
<dt><code>isreal</code></dt>
<dd><p>Return 1 for real elements.
</p>
</dd>
<dt><code>length</code></dt>
<dd><p>Return a vector of the lengths of cell elements.
</p>
</dd>
<dt><code>ndims</code></dt>
<dd><p>Return the number of dimensions of each element.
</p>
</dd>
<dt><code>numel</code></dt>
<dt><code>prodofsize</code></dt>
<dd><p>Return the number of elements contained within each cell element. The
number is the product of the dimensions of the object at each cell element.
</p>
</dd>
<dt><code>size</code></dt>
<dd><p>Return the size along the <var>k</var>-th dimension.
</p>
</dd>
<dt><code>isclass</code></dt>
<dd><p>Return 1 for elements of <var>class</var>.
</p></dd>
</dl>
<p>Additionally, <code>cellfun</code> accepts an arbitrary function <var>func</var>
in the form of an inline function, function handle, or the name of a
function (in a character string). The function can take one or more
arguments, with the inputs arguments given by <var>C</var>, <var>D</var>, etc.
Equally the function can return one or more output arguments. For example:
</p>
<div class="example">
<pre class="example">cellfun ("atan2", {1, 0}, {0, 1})
⇒ [ 1.57080 0.00000 ]
</pre></div>
<p>The number of output arguments of <code>cellfun</code> matches the number of
output arguments of the function. The outputs of the function will be
collected into the output arguments of <code>cellfun</code> like this:
</p>
<div class="example">
<pre class="example">function [a, b] = twoouts (x)
a = x;
b = x*x;
endfunction
[aa, bb] = cellfun (@twoouts, {1, 2, 3})
⇒
aa =
1 2 3
bb =
1 4 9
</pre></div>
<p>Note that per default the output argument(s) are arrays of the same size as
the input arguments. Input arguments that are singleton (1x1) cells will be
automatically expanded to the size of the other arguments.
</p>
<p>If the parameter <code>"UniformOutput"</code> is set to true (the default),
then the function must return scalars which will be concatenated into the
return array(s). If <code>"UniformOutput"</code> is false, the outputs are
concatenated into a cell array (or cell arrays). For example:
</p>
<div class="example">
<pre class="example">cellfun ("tolower", {"Foo", "Bar", "FooBar"},
"UniformOutput", false)
⇒ {"foo", "bar", "foobar"}
</pre></div>
<p>Given the parameter <code>"ErrorHandler"</code>, then <var>errfunc</var> defines a
function to call in case <var>func</var> generates an error. The form of the
function is
</p>
<div class="example">
<pre class="example">function […] = errfunc (<var>s</var>, …)
</pre></div>
<p>where there is an additional input argument to <var>errfunc</var> relative to
<var>func</var>, given by <var>s</var>. This is a structure with the elements
<code>"identifier"</code>, <code>"message"</code>, and <code>"index"</code> giving
respectively the error identifier, the error message, and the index into the
input arguments of the element that caused the error. For example:
</p>
<div class="example">
<pre class="example">function y = foo (s, x), y = NaN; endfunction
cellfun ("factorial", {-1,2}, "ErrorHandler", @foo)
⇒ [NaN 2]
</pre></div>
<p>Use <code>cellfun</code> intelligently. The <code>cellfun</code> function is a
useful tool for avoiding loops. It is often used with anonymous
function handles; however, calling an anonymous function involves an
overhead quite comparable to the overhead of an m-file function.
Passing a handle to a built-in function is faster, because the
interpreter is not involved in the internal loop. For example:
</p>
<div class="example">
<pre class="example">a = {…}
v = cellfun (@(x) det (x), a); # compute determinants
v = cellfun (@det, a); # faster
</pre></div>
<p><strong>See also:</strong> <a href="#XREFarrayfun">arrayfun</a>, <a href="#XREFstructfun">structfun</a>, <a href="#XREFspfun">spfun</a>.
</p></dd></dl>
<a name="XREFstructfun"></a><dl>
<dt><a name="index-structfun"></a>: <em></em> <strong>structfun</strong> <em>(<var>func</var>, <var>S</var>)</em></dt>
<dt><a name="index-structfun-1"></a>: <em>[<var>A</var>, …] =</em> <strong>structfun</strong> <em>(…)</em></dt>
<dt><a name="index-structfun-2"></a>: <em></em> <strong>structfun</strong> <em>(…, "ErrorHandler", <var>errfunc</var>)</em></dt>
<dt><a name="index-structfun-3"></a>: <em></em> <strong>structfun</strong> <em>(…, "UniformOutput", <var>val</var>)</em></dt>
<dd>
<p>Evaluate the function named <var>name</var> on the fields of the structure
<var>S</var>. The fields of <var>S</var> are passed to the function <var>func</var>
individually.
</p>
<p><code>structfun</code> accepts an arbitrary function <var>func</var> in the form of an
inline function, function handle, or the name of a function (in a character
string). In the case of a character string argument, the function must
accept a single argument named <var>x</var>, and it must return a string value.
If the function returns more than one argument, they are returned as
separate output variables.
</p>
<p>If the parameter <code>"UniformOutput"</code> is set to true (the default), then
the function must return a single element which will be concatenated into
the return value. If <code>"UniformOutput"</code> is false, the outputs are
placed into a structure with the same fieldnames as the input structure.
</p>
<div class="example">
<pre class="example">s.name1 = "John Smith";
s.name2 = "Jill Jones";
structfun (@(x) regexp (x, '(\w+)$', "matches"){1}, s,
"UniformOutput", false)
⇒
{
name1 = Smith
name2 = Jones
}
</pre></div>
<p>Given the parameter <code>"ErrorHandler"</code>, <var>errfunc</var> defines a function
to call in case <var>func</var> generates an error. The form of the function is
</p>
<div class="example">
<pre class="example">function […] = errfunc (<var>se</var>, …)
</pre></div>
<p>where there is an additional input argument to <var>errfunc</var> relative to
<var>func</var>, given by <var>se</var>. This is a structure with the
elements <code>"identifier"</code>, <code>"message"</code> and <code>"index"</code>,
giving respectively the error identifier, the error message, and the index
into the input arguments of the element that caused the error. For an
example on how to use an error handler, see <a href="#XREFcellfun">cellfun</a>.
</p>
<p><strong>See also:</strong> <a href="#XREFcellfun">cellfun</a>, <a href="#XREFarrayfun">arrayfun</a>, <a href="#XREFspfun">spfun</a>.
</p></dd></dl>
<p>Consistent with earlier advice, seek to use Octave built-in functions whenever
possible for the best performance. This advice applies especially to the four
functions above. For example, when adding two arrays together
element-by-element one could use a handle to the built-in addition function
<code>@plus</code> or define an anonymous function <code>@(x,y) x + y</code>. But, the
anonymous function is 60% slower than the first method.
See <a href="Operator-Overloading.html#Operator-Overloading">Operator Overloading</a>, for a list of basic functions which might be used
in place of anonymous ones.
</p>
<hr>
<div class="header">
<p>
Next: <a href="Accumulation.html#Accumulation" accesskey="n" rel="next">Accumulation</a>, Previous: <a href="Broadcasting.html#Broadcasting" accesskey="p" rel="prev">Broadcasting</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>
</body>
</html>
|