annotate scripts/general/arrayfun.m @ 9788:e3e5d43d9d4c

fix arrayfun with nargout=0
author Jaroslav Hajek <highegg@gmail.com>
date Mon, 09 Nov 2009 08:59:28 +0100
parents 4cf195710ae8
children a8ce6bdecce5
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
8920
eb63fbe60fab update copyright notices
John W. Eaton <jwe@octave.org>
parents: 8610
diff changeset
1 ## Copyright (C) 2006, 2007, 2008, 2009 Bill Denney
9674
4cf195710ae8 simplify arrayfun
Jaroslav Hajek <highegg@gmail.com>
parents: 9622
diff changeset
2 ## Copyright (C) 2009 Jaroslav Hajek
6422
9b982dd07654 [project @ 2007-03-20 21:26:12 by dbateman]
dbateman
parents:
diff changeset
3 ##
6439
a37c427ae9d1 [project @ 2007-03-23 15:04:01 by jwe]
jwe
parents: 6422
diff changeset
4 ## This file is part of Octave.
6422
9b982dd07654 [project @ 2007-03-20 21:26:12 by dbateman]
dbateman
parents:
diff changeset
5 ##
6439
a37c427ae9d1 [project @ 2007-03-23 15:04:01 by jwe]
jwe
parents: 6422
diff changeset
6 ## Octave is free software; you can redistribute it and/or modify it
a37c427ae9d1 [project @ 2007-03-23 15:04:01 by jwe]
jwe
parents: 6422
diff changeset
7 ## under the terms of the GNU General Public License as published by
7016
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 6714
diff changeset
8 ## the Free Software Foundation; either version 3 of the License, or (at
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 6714
diff changeset
9 ## your option) any later version.
6439
a37c427ae9d1 [project @ 2007-03-23 15:04:01 by jwe]
jwe
parents: 6422
diff changeset
10 ##
a37c427ae9d1 [project @ 2007-03-23 15:04:01 by jwe]
jwe
parents: 6422
diff changeset
11 ## Octave is distributed in the hope that it will be useful, but
a37c427ae9d1 [project @ 2007-03-23 15:04:01 by jwe]
jwe
parents: 6422
diff changeset
12 ## WITHOUT ANY WARRANTY; without even the implied warranty of
a37c427ae9d1 [project @ 2007-03-23 15:04:01 by jwe]
jwe
parents: 6422
diff changeset
13 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
a37c427ae9d1 [project @ 2007-03-23 15:04:01 by jwe]
jwe
parents: 6422
diff changeset
14 ## General Public License for more details.
6422
9b982dd07654 [project @ 2007-03-20 21:26:12 by dbateman]
dbateman
parents:
diff changeset
15 ##
9b982dd07654 [project @ 2007-03-20 21:26:12 by dbateman]
dbateman
parents:
diff changeset
16 ## You should have received a copy of the GNU General Public License
7016
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 6714
diff changeset
17 ## along with Octave; see the file COPYING. If not, see
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 6714
diff changeset
18 ## <http://www.gnu.org/licenses/>.
6422
9b982dd07654 [project @ 2007-03-20 21:26:12 by dbateman]
dbateman
parents:
diff changeset
19
9b982dd07654 [project @ 2007-03-20 21:26:12 by dbateman]
dbateman
parents:
diff changeset
20 ## -*- texinfo -*-
8357
bdb0a5aea9f2 Minor bug fixes, update help text and tests
Thomas Treichl <Thomas.Treichl@gmx.net>
parents: 7017
diff changeset
21 ## @deftypefn {Function File} {} arrayfun (@var{func}, @var{a})
bdb0a5aea9f2 Minor bug fixes, update help text and tests
Thomas Treichl <Thomas.Treichl@gmx.net>
parents: 7017
diff changeset
22 ## @deftypefnx {Function File} {@var{x} =} arrayfun (@var{func}, @var{a})
bdb0a5aea9f2 Minor bug fixes, update help text and tests
Thomas Treichl <Thomas.Treichl@gmx.net>
parents: 7017
diff changeset
23 ## @deftypefnx {Function File} {@var{x} =} arrayfun (@var{func}, @var{a}, @var{b}, @dots{})
bdb0a5aea9f2 Minor bug fixes, update help text and tests
Thomas Treichl <Thomas.Treichl@gmx.net>
parents: 7017
diff changeset
24 ## @deftypefnx {Function File} {[@var{x}, @var{y}, @dots{}] =} arrayfun (@var{func}, @var{a}, @dots{})
bdb0a5aea9f2 Minor bug fixes, update help text and tests
Thomas Treichl <Thomas.Treichl@gmx.net>
parents: 7017
diff changeset
25 ## @deftypefnx {Function File} {} arrayfun (@dots{}, "UniformOutput", @var{val})
bdb0a5aea9f2 Minor bug fixes, update help text and tests
Thomas Treichl <Thomas.Treichl@gmx.net>
parents: 7017
diff changeset
26 ## @deftypefnx {Function File} {} arrayfun (@dots{}, "ErrorHandler", @var{errfunc})
bdb0a5aea9f2 Minor bug fixes, update help text and tests
Thomas Treichl <Thomas.Treichl@gmx.net>
parents: 7017
diff changeset
27 ##
6439
a37c427ae9d1 [project @ 2007-03-23 15:04:01 by jwe]
jwe
parents: 6422
diff changeset
28 ## Execute a function on each element of an array. This is useful for
a37c427ae9d1 [project @ 2007-03-23 15:04:01 by jwe]
jwe
parents: 6422
diff changeset
29 ## functions that do not accept array arguments. If the function does
6422
9b982dd07654 [project @ 2007-03-20 21:26:12 by dbateman]
dbateman
parents:
diff changeset
30 ## accept array arguments it is better to call the function directly.
9b982dd07654 [project @ 2007-03-20 21:26:12 by dbateman]
dbateman
parents:
diff changeset
31 ##
8357
bdb0a5aea9f2 Minor bug fixes, update help text and tests
Thomas Treichl <Thomas.Treichl@gmx.net>
parents: 7017
diff changeset
32 ## The first input argument @var{func} can be a string, a function
bdb0a5aea9f2 Minor bug fixes, update help text and tests
Thomas Treichl <Thomas.Treichl@gmx.net>
parents: 7017
diff changeset
33 ## handle, an inline function or an anonymous function. The input
bdb0a5aea9f2 Minor bug fixes, update help text and tests
Thomas Treichl <Thomas.Treichl@gmx.net>
parents: 7017
diff changeset
34 ## argument @var{a} can be a logic array, a numeric array, a string
bdb0a5aea9f2 Minor bug fixes, update help text and tests
Thomas Treichl <Thomas.Treichl@gmx.net>
parents: 7017
diff changeset
35 ## array, a structure array or a cell array. By a call of the function
bdb0a5aea9f2 Minor bug fixes, update help text and tests
Thomas Treichl <Thomas.Treichl@gmx.net>
parents: 7017
diff changeset
36 ## @command{arrayfun} all elements of @var{a} are passed on to the named
bdb0a5aea9f2 Minor bug fixes, update help text and tests
Thomas Treichl <Thomas.Treichl@gmx.net>
parents: 7017
diff changeset
37 ## function @var{func} individually.
bdb0a5aea9f2 Minor bug fixes, update help text and tests
Thomas Treichl <Thomas.Treichl@gmx.net>
parents: 7017
diff changeset
38 ##
bdb0a5aea9f2 Minor bug fixes, update help text and tests
Thomas Treichl <Thomas.Treichl@gmx.net>
parents: 7017
diff changeset
39 ## The named function can also take more than two input arguments, with
bdb0a5aea9f2 Minor bug fixes, update help text and tests
Thomas Treichl <Thomas.Treichl@gmx.net>
parents: 7017
diff changeset
40 ## the input arguments given as third input argument @var{b}, fourth
bdb0a5aea9f2 Minor bug fixes, update help text and tests
Thomas Treichl <Thomas.Treichl@gmx.net>
parents: 7017
diff changeset
41 ## input argument @var{c}, @dots{} If given more than one array input
bdb0a5aea9f2 Minor bug fixes, update help text and tests
Thomas Treichl <Thomas.Treichl@gmx.net>
parents: 7017
diff changeset
42 ## argument then all input arguments must have the same sizes, for
bdb0a5aea9f2 Minor bug fixes, update help text and tests
Thomas Treichl <Thomas.Treichl@gmx.net>
parents: 7017
diff changeset
43 ## example
bdb0a5aea9f2 Minor bug fixes, update help text and tests
Thomas Treichl <Thomas.Treichl@gmx.net>
parents: 7017
diff changeset
44 ##
bdb0a5aea9f2 Minor bug fixes, update help text and tests
Thomas Treichl <Thomas.Treichl@gmx.net>
parents: 7017
diff changeset
45 ## @example
bdb0a5aea9f2 Minor bug fixes, update help text and tests
Thomas Treichl <Thomas.Treichl@gmx.net>
parents: 7017
diff changeset
46 ## @group
bdb0a5aea9f2 Minor bug fixes, update help text and tests
Thomas Treichl <Thomas.Treichl@gmx.net>
parents: 7017
diff changeset
47 ## arrayfun (@@atan2, [1, 0], [0, 1])
bdb0a5aea9f2 Minor bug fixes, update help text and tests
Thomas Treichl <Thomas.Treichl@gmx.net>
parents: 7017
diff changeset
48 ## @result{} ans = [1.5708 0.0000]
bdb0a5aea9f2 Minor bug fixes, update help text and tests
Thomas Treichl <Thomas.Treichl@gmx.net>
parents: 7017
diff changeset
49 ## @end group
bdb0a5aea9f2 Minor bug fixes, update help text and tests
Thomas Treichl <Thomas.Treichl@gmx.net>
parents: 7017
diff changeset
50 ## @end example
bdb0a5aea9f2 Minor bug fixes, update help text and tests
Thomas Treichl <Thomas.Treichl@gmx.net>
parents: 7017
diff changeset
51 ##
bdb0a5aea9f2 Minor bug fixes, update help text and tests
Thomas Treichl <Thomas.Treichl@gmx.net>
parents: 7017
diff changeset
52 ## If the parameter @var{val} after a further string input argument
bdb0a5aea9f2 Minor bug fixes, update help text and tests
Thomas Treichl <Thomas.Treichl@gmx.net>
parents: 7017
diff changeset
53 ## "UniformOutput" is set @code{true} (the default), then the named
bdb0a5aea9f2 Minor bug fixes, update help text and tests
Thomas Treichl <Thomas.Treichl@gmx.net>
parents: 7017
diff changeset
54 ## function @var{func} must return a single element which then will be
bdb0a5aea9f2 Minor bug fixes, update help text and tests
Thomas Treichl <Thomas.Treichl@gmx.net>
parents: 7017
diff changeset
55 ## concatenated into the return value and is of type matrix. Otherwise,
bdb0a5aea9f2 Minor bug fixes, update help text and tests
Thomas Treichl <Thomas.Treichl@gmx.net>
parents: 7017
diff changeset
56 ## if that parameter is set to @code{false}, then the outputs are
bdb0a5aea9f2 Minor bug fixes, update help text and tests
Thomas Treichl <Thomas.Treichl@gmx.net>
parents: 7017
diff changeset
57 ## concatenated in a cell array. For example
bdb0a5aea9f2 Minor bug fixes, update help text and tests
Thomas Treichl <Thomas.Treichl@gmx.net>
parents: 7017
diff changeset
58 ##
bdb0a5aea9f2 Minor bug fixes, update help text and tests
Thomas Treichl <Thomas.Treichl@gmx.net>
parents: 7017
diff changeset
59 ## @example
bdb0a5aea9f2 Minor bug fixes, update help text and tests
Thomas Treichl <Thomas.Treichl@gmx.net>
parents: 7017
diff changeset
60 ## @group
bdb0a5aea9f2 Minor bug fixes, update help text and tests
Thomas Treichl <Thomas.Treichl@gmx.net>
parents: 7017
diff changeset
61 ## arrayfun (@@(x,y) x:y, "abc", "def", "UniformOutput", false)
bdb0a5aea9f2 Minor bug fixes, update help text and tests
Thomas Treichl <Thomas.Treichl@gmx.net>
parents: 7017
diff changeset
62 ## @result{} ans =
bdb0a5aea9f2 Minor bug fixes, update help text and tests
Thomas Treichl <Thomas.Treichl@gmx.net>
parents: 7017
diff changeset
63 ## @{
bdb0a5aea9f2 Minor bug fixes, update help text and tests
Thomas Treichl <Thomas.Treichl@gmx.net>
parents: 7017
diff changeset
64 ## [1,1] = abcd
bdb0a5aea9f2 Minor bug fixes, update help text and tests
Thomas Treichl <Thomas.Treichl@gmx.net>
parents: 7017
diff changeset
65 ## [1,2] = bcde
bdb0a5aea9f2 Minor bug fixes, update help text and tests
Thomas Treichl <Thomas.Treichl@gmx.net>
parents: 7017
diff changeset
66 ## [1,3] = cdef
bdb0a5aea9f2 Minor bug fixes, update help text and tests
Thomas Treichl <Thomas.Treichl@gmx.net>
parents: 7017
diff changeset
67 ## @}
bdb0a5aea9f2 Minor bug fixes, update help text and tests
Thomas Treichl <Thomas.Treichl@gmx.net>
parents: 7017
diff changeset
68 ## @end group
bdb0a5aea9f2 Minor bug fixes, update help text and tests
Thomas Treichl <Thomas.Treichl@gmx.net>
parents: 7017
diff changeset
69 ## @end example
bdb0a5aea9f2 Minor bug fixes, update help text and tests
Thomas Treichl <Thomas.Treichl@gmx.net>
parents: 7017
diff changeset
70 ##
bdb0a5aea9f2 Minor bug fixes, update help text and tests
Thomas Treichl <Thomas.Treichl@gmx.net>
parents: 7017
diff changeset
71 ## If more than one output arguments are given then the named function
bdb0a5aea9f2 Minor bug fixes, update help text and tests
Thomas Treichl <Thomas.Treichl@gmx.net>
parents: 7017
diff changeset
72 ## must return the number of return values that also are expected, for
bdb0a5aea9f2 Minor bug fixes, update help text and tests
Thomas Treichl <Thomas.Treichl@gmx.net>
parents: 7017
diff changeset
73 ## example
bdb0a5aea9f2 Minor bug fixes, update help text and tests
Thomas Treichl <Thomas.Treichl@gmx.net>
parents: 7017
diff changeset
74 ##
bdb0a5aea9f2 Minor bug fixes, update help text and tests
Thomas Treichl <Thomas.Treichl@gmx.net>
parents: 7017
diff changeset
75 ## @example
bdb0a5aea9f2 Minor bug fixes, update help text and tests
Thomas Treichl <Thomas.Treichl@gmx.net>
parents: 7017
diff changeset
76 ## @group
bdb0a5aea9f2 Minor bug fixes, update help text and tests
Thomas Treichl <Thomas.Treichl@gmx.net>
parents: 7017
diff changeset
77 ## [A, B, C] = arrayfun (@@find, [10; 0], "UniformOutput", false)
bdb0a5aea9f2 Minor bug fixes, update help text and tests
Thomas Treichl <Thomas.Treichl@gmx.net>
parents: 7017
diff changeset
78 ## @result{}
bdb0a5aea9f2 Minor bug fixes, update help text and tests
Thomas Treichl <Thomas.Treichl@gmx.net>
parents: 7017
diff changeset
79 ## A =
bdb0a5aea9f2 Minor bug fixes, update help text and tests
Thomas Treichl <Thomas.Treichl@gmx.net>
parents: 7017
diff changeset
80 ## @{
bdb0a5aea9f2 Minor bug fixes, update help text and tests
Thomas Treichl <Thomas.Treichl@gmx.net>
parents: 7017
diff changeset
81 ## [1,1] = 1
bdb0a5aea9f2 Minor bug fixes, update help text and tests
Thomas Treichl <Thomas.Treichl@gmx.net>
parents: 7017
diff changeset
82 ## [2,1] = [](0x0)
bdb0a5aea9f2 Minor bug fixes, update help text and tests
Thomas Treichl <Thomas.Treichl@gmx.net>
parents: 7017
diff changeset
83 ## @}
bdb0a5aea9f2 Minor bug fixes, update help text and tests
Thomas Treichl <Thomas.Treichl@gmx.net>
parents: 7017
diff changeset
84 ## B =
bdb0a5aea9f2 Minor bug fixes, update help text and tests
Thomas Treichl <Thomas.Treichl@gmx.net>
parents: 7017
diff changeset
85 ## @{
bdb0a5aea9f2 Minor bug fixes, update help text and tests
Thomas Treichl <Thomas.Treichl@gmx.net>
parents: 7017
diff changeset
86 ## [1,1] = 1
bdb0a5aea9f2 Minor bug fixes, update help text and tests
Thomas Treichl <Thomas.Treichl@gmx.net>
parents: 7017
diff changeset
87 ## [2,1] = [](0x0)
bdb0a5aea9f2 Minor bug fixes, update help text and tests
Thomas Treichl <Thomas.Treichl@gmx.net>
parents: 7017
diff changeset
88 ## @}
bdb0a5aea9f2 Minor bug fixes, update help text and tests
Thomas Treichl <Thomas.Treichl@gmx.net>
parents: 7017
diff changeset
89 ## C =
bdb0a5aea9f2 Minor bug fixes, update help text and tests
Thomas Treichl <Thomas.Treichl@gmx.net>
parents: 7017
diff changeset
90 ## @{
bdb0a5aea9f2 Minor bug fixes, update help text and tests
Thomas Treichl <Thomas.Treichl@gmx.net>
parents: 7017
diff changeset
91 ## [1,1] = 10
bdb0a5aea9f2 Minor bug fixes, update help text and tests
Thomas Treichl <Thomas.Treichl@gmx.net>
parents: 7017
diff changeset
92 ## [2,1] = [](0x0)
bdb0a5aea9f2 Minor bug fixes, update help text and tests
Thomas Treichl <Thomas.Treichl@gmx.net>
parents: 7017
diff changeset
93 ## @}
bdb0a5aea9f2 Minor bug fixes, update help text and tests
Thomas Treichl <Thomas.Treichl@gmx.net>
parents: 7017
diff changeset
94 ## @end group
bdb0a5aea9f2 Minor bug fixes, update help text and tests
Thomas Treichl <Thomas.Treichl@gmx.net>
parents: 7017
diff changeset
95 ## @end example
bdb0a5aea9f2 Minor bug fixes, update help text and tests
Thomas Treichl <Thomas.Treichl@gmx.net>
parents: 7017
diff changeset
96 ##
bdb0a5aea9f2 Minor bug fixes, update help text and tests
Thomas Treichl <Thomas.Treichl@gmx.net>
parents: 7017
diff changeset
97 ## If the parameter @var{errfunc} after a further string input argument
bdb0a5aea9f2 Minor bug fixes, update help text and tests
Thomas Treichl <Thomas.Treichl@gmx.net>
parents: 7017
diff changeset
98 ## "ErrorHandler" is another string, a function handle, an inline
bdb0a5aea9f2 Minor bug fixes, update help text and tests
Thomas Treichl <Thomas.Treichl@gmx.net>
parents: 7017
diff changeset
99 ## function or an anonymous function, then @var{errfunc} defines a
bdb0a5aea9f2 Minor bug fixes, update help text and tests
Thomas Treichl <Thomas.Treichl@gmx.net>
parents: 7017
diff changeset
100 ## function to call in the case that @var{func} generates an error.
bdb0a5aea9f2 Minor bug fixes, update help text and tests
Thomas Treichl <Thomas.Treichl@gmx.net>
parents: 7017
diff changeset
101 ## The definition of the function must be of the form
bdb0a5aea9f2 Minor bug fixes, update help text and tests
Thomas Treichl <Thomas.Treichl@gmx.net>
parents: 7017
diff changeset
102 ##
bdb0a5aea9f2 Minor bug fixes, update help text and tests
Thomas Treichl <Thomas.Treichl@gmx.net>
parents: 7017
diff changeset
103 ## @example
bdb0a5aea9f2 Minor bug fixes, update help text and tests
Thomas Treichl <Thomas.Treichl@gmx.net>
parents: 7017
diff changeset
104 ## function [@dots{}] = errfunc (@var{s}, @dots{})
bdb0a5aea9f2 Minor bug fixes, update help text and tests
Thomas Treichl <Thomas.Treichl@gmx.net>
parents: 7017
diff changeset
105 ## @end example
bdb0a5aea9f2 Minor bug fixes, update help text and tests
Thomas Treichl <Thomas.Treichl@gmx.net>
parents: 7017
diff changeset
106 ##
bdb0a5aea9f2 Minor bug fixes, update help text and tests
Thomas Treichl <Thomas.Treichl@gmx.net>
parents: 7017
diff changeset
107 ## where there is an additional input argument to @var{errfunc}
bdb0a5aea9f2 Minor bug fixes, update help text and tests
Thomas Treichl <Thomas.Treichl@gmx.net>
parents: 7017
diff changeset
108 ## relative to @var{func}, given by @var{s}. This is a structure with
bdb0a5aea9f2 Minor bug fixes, update help text and tests
Thomas Treichl <Thomas.Treichl@gmx.net>
parents: 7017
diff changeset
109 ## the elements "identifier", "message" and "index", giving
bdb0a5aea9f2 Minor bug fixes, update help text and tests
Thomas Treichl <Thomas.Treichl@gmx.net>
parents: 7017
diff changeset
110 ## respectively the error identifier, the error message and the index of
bdb0a5aea9f2 Minor bug fixes, update help text and tests
Thomas Treichl <Thomas.Treichl@gmx.net>
parents: 7017
diff changeset
111 ## the array elements that caused the error. The size of the output
bdb0a5aea9f2 Minor bug fixes, update help text and tests
Thomas Treichl <Thomas.Treichl@gmx.net>
parents: 7017
diff changeset
112 ## argument of @var{errfunc} must have the same size as the output
bdb0a5aea9f2 Minor bug fixes, update help text and tests
Thomas Treichl <Thomas.Treichl@gmx.net>
parents: 7017
diff changeset
113 ## argument of @var{func}, otherwise a real error is thrown. For
bdb0a5aea9f2 Minor bug fixes, update help text and tests
Thomas Treichl <Thomas.Treichl@gmx.net>
parents: 7017
diff changeset
114 ## example
bdb0a5aea9f2 Minor bug fixes, update help text and tests
Thomas Treichl <Thomas.Treichl@gmx.net>
parents: 7017
diff changeset
115 ##
bdb0a5aea9f2 Minor bug fixes, update help text and tests
Thomas Treichl <Thomas.Treichl@gmx.net>
parents: 7017
diff changeset
116 ## @example
bdb0a5aea9f2 Minor bug fixes, update help text and tests
Thomas Treichl <Thomas.Treichl@gmx.net>
parents: 7017
diff changeset
117 ## @group
bdb0a5aea9f2 Minor bug fixes, update help text and tests
Thomas Treichl <Thomas.Treichl@gmx.net>
parents: 7017
diff changeset
118 ## function y = ferr (s, x), y = "MyString"; endfunction
bdb0a5aea9f2 Minor bug fixes, update help text and tests
Thomas Treichl <Thomas.Treichl@gmx.net>
parents: 7017
diff changeset
119 ## arrayfun (@@str2num, [1234], \
bdb0a5aea9f2 Minor bug fixes, update help text and tests
Thomas Treichl <Thomas.Treichl@gmx.net>
parents: 7017
diff changeset
120 ## "UniformOutput", false, "ErrorHandler", @@ferr)
bdb0a5aea9f2 Minor bug fixes, update help text and tests
Thomas Treichl <Thomas.Treichl@gmx.net>
parents: 7017
diff changeset
121 ## @result{} ans =
bdb0a5aea9f2 Minor bug fixes, update help text and tests
Thomas Treichl <Thomas.Treichl@gmx.net>
parents: 7017
diff changeset
122 ## @{
bdb0a5aea9f2 Minor bug fixes, update help text and tests
Thomas Treichl <Thomas.Treichl@gmx.net>
parents: 7017
diff changeset
123 ## [1,1] = MyString
bdb0a5aea9f2 Minor bug fixes, update help text and tests
Thomas Treichl <Thomas.Treichl@gmx.net>
parents: 7017
diff changeset
124 ## @}
bdb0a5aea9f2 Minor bug fixes, update help text and tests
Thomas Treichl <Thomas.Treichl@gmx.net>
parents: 7017
diff changeset
125 ## @end group
bdb0a5aea9f2 Minor bug fixes, update help text and tests
Thomas Treichl <Thomas.Treichl@gmx.net>
parents: 7017
diff changeset
126 ## @end example
bdb0a5aea9f2 Minor bug fixes, update help text and tests
Thomas Treichl <Thomas.Treichl@gmx.net>
parents: 7017
diff changeset
127 ##
bdb0a5aea9f2 Minor bug fixes, update help text and tests
Thomas Treichl <Thomas.Treichl@gmx.net>
parents: 7017
diff changeset
128 ## @seealso{cellfun, spfun, structfun}
6422
9b982dd07654 [project @ 2007-03-20 21:26:12 by dbateman]
dbateman
parents:
diff changeset
129 ## @end deftypefn
9b982dd07654 [project @ 2007-03-20 21:26:12 by dbateman]
dbateman
parents:
diff changeset
130
7017
a1dbe9d80eee [project @ 2007-10-12 21:27:11 by jwe]
jwe
parents: 7016
diff changeset
131 ## Author: Bill Denney <denney@seas.upenn.edu>
9674
4cf195710ae8 simplify arrayfun
Jaroslav Hajek <highegg@gmail.com>
parents: 9622
diff changeset
132 ## Rewritten: Jaroslav Hajek <highegg@gmail.com>
7017
a1dbe9d80eee [project @ 2007-10-12 21:27:11 by jwe]
jwe
parents: 7016
diff changeset
133
6439
a37c427ae9d1 [project @ 2007-03-23 15:04:01 by jwe]
jwe
parents: 6422
diff changeset
134 function varargout = arrayfun (func, varargin)
6422
9b982dd07654 [project @ 2007-03-20 21:26:12 by dbateman]
dbateman
parents:
diff changeset
135
9b982dd07654 [project @ 2007-03-20 21:26:12 by dbateman]
dbateman
parents:
diff changeset
136 if (nargin < 2)
6439
a37c427ae9d1 [project @ 2007-03-23 15:04:01 by jwe]
jwe
parents: 6422
diff changeset
137 print_usage ();
6422
9b982dd07654 [project @ 2007-03-20 21:26:12 by dbateman]
dbateman
parents:
diff changeset
138 endif
9b982dd07654 [project @ 2007-03-20 21:26:12 by dbateman]
dbateman
parents:
diff changeset
139
9674
4cf195710ae8 simplify arrayfun
Jaroslav Hajek <highegg@gmail.com>
parents: 9622
diff changeset
140 nargs = length (varargin);
4cf195710ae8 simplify arrayfun
Jaroslav Hajek <highegg@gmail.com>
parents: 9622
diff changeset
141
4cf195710ae8 simplify arrayfun
Jaroslav Hajek <highegg@gmail.com>
parents: 9622
diff changeset
142 recognized_opts = {"UniformOutput", "ErrorHandler"};
4cf195710ae8 simplify arrayfun
Jaroslav Hajek <highegg@gmail.com>
parents: 9622
diff changeset
143
4cf195710ae8 simplify arrayfun
Jaroslav Hajek <highegg@gmail.com>
parents: 9622
diff changeset
144 while (nargs >= 2)
4cf195710ae8 simplify arrayfun
Jaroslav Hajek <highegg@gmail.com>
parents: 9622
diff changeset
145 maybeopt = varargin{nargs-1};
4cf195710ae8 simplify arrayfun
Jaroslav Hajek <highegg@gmail.com>
parents: 9622
diff changeset
146 if (ischar (maybeopt) && any (strcmpi (maybeopt, recognized_opts)))
4cf195710ae8 simplify arrayfun
Jaroslav Hajek <highegg@gmail.com>
parents: 9622
diff changeset
147 nargs -= 2;
6422
9b982dd07654 [project @ 2007-03-20 21:26:12 by dbateman]
dbateman
parents:
diff changeset
148 else
9674
4cf195710ae8 simplify arrayfun
Jaroslav Hajek <highegg@gmail.com>
parents: 9622
diff changeset
149 break;
6422
9b982dd07654 [project @ 2007-03-20 21:26:12 by dbateman]
dbateman
parents:
diff changeset
150 endif
9b982dd07654 [project @ 2007-03-20 21:26:12 by dbateman]
dbateman
parents:
diff changeset
151 endwhile
9b982dd07654 [project @ 2007-03-20 21:26:12 by dbateman]
dbateman
parents:
diff changeset
152
9674
4cf195710ae8 simplify arrayfun
Jaroslav Hajek <highegg@gmail.com>
parents: 9622
diff changeset
153 args = varargin(1:nargs);
4cf195710ae8 simplify arrayfun
Jaroslav Hajek <highegg@gmail.com>
parents: 9622
diff changeset
154 opts = varargin(nargs+1:end);
4cf195710ae8 simplify arrayfun
Jaroslav Hajek <highegg@gmail.com>
parents: 9622
diff changeset
155
4cf195710ae8 simplify arrayfun
Jaroslav Hajek <highegg@gmail.com>
parents: 9622
diff changeset
156 args = cellfun (@num2cell, args, "UniformOutput", false,
4cf195710ae8 simplify arrayfun
Jaroslav Hajek <highegg@gmail.com>
parents: 9622
diff changeset
157 "ErrorHandler", @arg_class_error);
4cf195710ae8 simplify arrayfun
Jaroslav Hajek <highegg@gmail.com>
parents: 9622
diff changeset
158
9788
e3e5d43d9d4c fix arrayfun with nargout=0
Jaroslav Hajek <highegg@gmail.com>
parents: 9674
diff changeset
159 [varargout{1:max(1, nargout)}] = cellfun (func, args{:}, opts{:});
9674
4cf195710ae8 simplify arrayfun
Jaroslav Hajek <highegg@gmail.com>
parents: 9622
diff changeset
160
4cf195710ae8 simplify arrayfun
Jaroslav Hajek <highegg@gmail.com>
parents: 9622
diff changeset
161 endfunction
4cf195710ae8 simplify arrayfun
Jaroslav Hajek <highegg@gmail.com>
parents: 9622
diff changeset
162
4cf195710ae8 simplify arrayfun
Jaroslav Hajek <highegg@gmail.com>
parents: 9622
diff changeset
163 function arg_class_error (S, X)
4cf195710ae8 simplify arrayfun
Jaroslav Hajek <highegg@gmail.com>
parents: 9622
diff changeset
164 error ("arrayfun: invalid argument of class %s", class (X))
6422
9b982dd07654 [project @ 2007-03-20 21:26:12 by dbateman]
dbateman
parents:
diff changeset
165 endfunction
9b982dd07654 [project @ 2007-03-20 21:26:12 by dbateman]
dbateman
parents:
diff changeset
166
8357
bdb0a5aea9f2 Minor bug fixes, update help text and tests
Thomas Treichl <Thomas.Treichl@gmx.net>
parents: 7017
diff changeset
167 %% Test function to check the "Errorhandler" option
bdb0a5aea9f2 Minor bug fixes, update help text and tests
Thomas Treichl <Thomas.Treichl@gmx.net>
parents: 7017
diff changeset
168 %!function [z] = arrayfunerror (S, varargin)
bdb0a5aea9f2 Minor bug fixes, update help text and tests
Thomas Treichl <Thomas.Treichl@gmx.net>
parents: 7017
diff changeset
169 %! z = S;
bdb0a5aea9f2 Minor bug fixes, update help text and tests
Thomas Treichl <Thomas.Treichl@gmx.net>
parents: 7017
diff changeset
170 %! endfunction
bdb0a5aea9f2 Minor bug fixes, update help text and tests
Thomas Treichl <Thomas.Treichl@gmx.net>
parents: 7017
diff changeset
171 %% First input argument can be a string, an inline function, a
bdb0a5aea9f2 Minor bug fixes, update help text and tests
Thomas Treichl <Thomas.Treichl@gmx.net>
parents: 7017
diff changeset
172 %% function_handle or an anonymous function
6422
9b982dd07654 [project @ 2007-03-20 21:26:12 by dbateman]
dbateman
parents:
diff changeset
173 %!test
8357
bdb0a5aea9f2 Minor bug fixes, update help text and tests
Thomas Treichl <Thomas.Treichl@gmx.net>
parents: 7017
diff changeset
174 %! arrayfun (@isequal, [false, true], [true, true]); %% No output argument
bdb0a5aea9f2 Minor bug fixes, update help text and tests
Thomas Treichl <Thomas.Treichl@gmx.net>
parents: 7017
diff changeset
175 %!error
bdb0a5aea9f2 Minor bug fixes, update help text and tests
Thomas Treichl <Thomas.Treichl@gmx.net>
parents: 7017
diff changeset
176 %! arrayfun (@isequal); %% One or less input arguments
bdb0a5aea9f2 Minor bug fixes, update help text and tests
Thomas Treichl <Thomas.Treichl@gmx.net>
parents: 7017
diff changeset
177 %!test
bdb0a5aea9f2 Minor bug fixes, update help text and tests
Thomas Treichl <Thomas.Treichl@gmx.net>
parents: 7017
diff changeset
178 %! A = arrayfun ("isequal", [false, true], [true, true]);
bdb0a5aea9f2 Minor bug fixes, update help text and tests
Thomas Treichl <Thomas.Treichl@gmx.net>
parents: 7017
diff changeset
179 %! assert (A, [false, true]);
bdb0a5aea9f2 Minor bug fixes, update help text and tests
Thomas Treichl <Thomas.Treichl@gmx.net>
parents: 7017
diff changeset
180 %!test
bdb0a5aea9f2 Minor bug fixes, update help text and tests
Thomas Treichl <Thomas.Treichl@gmx.net>
parents: 7017
diff changeset
181 %! A = arrayfun (inline ("(x == y)", "x", "y"), [false, true], [true, true]);
bdb0a5aea9f2 Minor bug fixes, update help text and tests
Thomas Treichl <Thomas.Treichl@gmx.net>
parents: 7017
diff changeset
182 %! assert (A, [false, true]);
bdb0a5aea9f2 Minor bug fixes, update help text and tests
Thomas Treichl <Thomas.Treichl@gmx.net>
parents: 7017
diff changeset
183 %!test
bdb0a5aea9f2 Minor bug fixes, update help text and tests
Thomas Treichl <Thomas.Treichl@gmx.net>
parents: 7017
diff changeset
184 %! A = arrayfun (@isequal, [false, true], [true, true]);
bdb0a5aea9f2 Minor bug fixes, update help text and tests
Thomas Treichl <Thomas.Treichl@gmx.net>
parents: 7017
diff changeset
185 %! assert (A, [false, true]);
bdb0a5aea9f2 Minor bug fixes, update help text and tests
Thomas Treichl <Thomas.Treichl@gmx.net>
parents: 7017
diff changeset
186 %!test
bdb0a5aea9f2 Minor bug fixes, update help text and tests
Thomas Treichl <Thomas.Treichl@gmx.net>
parents: 7017
diff changeset
187 %! A = arrayfun (@(x,y) isequal(x,y), [false, true], [true, true]);
bdb0a5aea9f2 Minor bug fixes, update help text and tests
Thomas Treichl <Thomas.Treichl@gmx.net>
parents: 7017
diff changeset
188 %! assert (A, [false, true]);
bdb0a5aea9f2 Minor bug fixes, update help text and tests
Thomas Treichl <Thomas.Treichl@gmx.net>
parents: 7017
diff changeset
189
bdb0a5aea9f2 Minor bug fixes, update help text and tests
Thomas Treichl <Thomas.Treichl@gmx.net>
parents: 7017
diff changeset
190 %% Number of input and output arguments may be greater than one
bdb0a5aea9f2 Minor bug fixes, update help text and tests
Thomas Treichl <Thomas.Treichl@gmx.net>
parents: 7017
diff changeset
191 %#!test
bdb0a5aea9f2 Minor bug fixes, update help text and tests
Thomas Treichl <Thomas.Treichl@gmx.net>
parents: 7017
diff changeset
192 %! A = arrayfun (@(x) islogical (x), false);
bdb0a5aea9f2 Minor bug fixes, update help text and tests
Thomas Treichl <Thomas.Treichl@gmx.net>
parents: 7017
diff changeset
193 %! assert (A, true);
bdb0a5aea9f2 Minor bug fixes, update help text and tests
Thomas Treichl <Thomas.Treichl@gmx.net>
parents: 7017
diff changeset
194 %!test
bdb0a5aea9f2 Minor bug fixes, update help text and tests
Thomas Treichl <Thomas.Treichl@gmx.net>
parents: 7017
diff changeset
195 %! A = arrayfun (@(x,y,z) x + y + z, [1, 1, 1], [2, 2, 2], [3, 4, 5]);
bdb0a5aea9f2 Minor bug fixes, update help text and tests
Thomas Treichl <Thomas.Treichl@gmx.net>
parents: 7017
diff changeset
196 %! assert (A, [6, 7, 8], 1e-16);
bdb0a5aea9f2 Minor bug fixes, update help text and tests
Thomas Treichl <Thomas.Treichl@gmx.net>
parents: 7017
diff changeset
197 %!test %% Two input arguments of different types
bdb0a5aea9f2 Minor bug fixes, update help text and tests
Thomas Treichl <Thomas.Treichl@gmx.net>
parents: 7017
diff changeset
198 %! A = arrayfun (@(x,y) islogical (x) && ischar (y), false, "a");
bdb0a5aea9f2 Minor bug fixes, update help text and tests
Thomas Treichl <Thomas.Treichl@gmx.net>
parents: 7017
diff changeset
199 %! assert (A, true);
bdb0a5aea9f2 Minor bug fixes, update help text and tests
Thomas Treichl <Thomas.Treichl@gmx.net>
parents: 7017
diff changeset
200 %!test %% Pass another variable to the anonymous function
bdb0a5aea9f2 Minor bug fixes, update help text and tests
Thomas Treichl <Thomas.Treichl@gmx.net>
parents: 7017
diff changeset
201 %! y = true; A = arrayfun (@(x) islogical (x && y), false);
bdb0a5aea9f2 Minor bug fixes, update help text and tests
Thomas Treichl <Thomas.Treichl@gmx.net>
parents: 7017
diff changeset
202 %! assert (A, true);
bdb0a5aea9f2 Minor bug fixes, update help text and tests
Thomas Treichl <Thomas.Treichl@gmx.net>
parents: 7017
diff changeset
203 %!test %% Three ouptut arguments of different type
bdb0a5aea9f2 Minor bug fixes, update help text and tests
Thomas Treichl <Thomas.Treichl@gmx.net>
parents: 7017
diff changeset
204 %! [A, B, C] = arrayfun (@find, [10, 11; 0, 12], "UniformOutput", false);
bdb0a5aea9f2 Minor bug fixes, update help text and tests
Thomas Treichl <Thomas.Treichl@gmx.net>
parents: 7017
diff changeset
205 %! assert (isequal (A, {true, true; [], true}));
bdb0a5aea9f2 Minor bug fixes, update help text and tests
Thomas Treichl <Thomas.Treichl@gmx.net>
parents: 7017
diff changeset
206 %! assert (isequal (B, {true, true; [], true}));
bdb0a5aea9f2 Minor bug fixes, update help text and tests
Thomas Treichl <Thomas.Treichl@gmx.net>
parents: 7017
diff changeset
207 %! assert (isequal (C, {10, 11; [], 12}));
bdb0a5aea9f2 Minor bug fixes, update help text and tests
Thomas Treichl <Thomas.Treichl@gmx.net>
parents: 7017
diff changeset
208
bdb0a5aea9f2 Minor bug fixes, update help text and tests
Thomas Treichl <Thomas.Treichl@gmx.net>
parents: 7017
diff changeset
209 %% Input arguments can be of type logical
bdb0a5aea9f2 Minor bug fixes, update help text and tests
Thomas Treichl <Thomas.Treichl@gmx.net>
parents: 7017
diff changeset
210 %!test
bdb0a5aea9f2 Minor bug fixes, update help text and tests
Thomas Treichl <Thomas.Treichl@gmx.net>
parents: 7017
diff changeset
211 %! A = arrayfun (@(x,y) x == y, [false, true], [true, true]);
bdb0a5aea9f2 Minor bug fixes, update help text and tests
Thomas Treichl <Thomas.Treichl@gmx.net>
parents: 7017
diff changeset
212 %! assert (A, [false, true]);
bdb0a5aea9f2 Minor bug fixes, update help text and tests
Thomas Treichl <Thomas.Treichl@gmx.net>
parents: 7017
diff changeset
213 %!test
bdb0a5aea9f2 Minor bug fixes, update help text and tests
Thomas Treichl <Thomas.Treichl@gmx.net>
parents: 7017
diff changeset
214 %! A = arrayfun (@(x,y) x == y, [false; true], [true; true], "UniformOutput", true);
bdb0a5aea9f2 Minor bug fixes, update help text and tests
Thomas Treichl <Thomas.Treichl@gmx.net>
parents: 7017
diff changeset
215 %! assert (A, [false; true]);
bdb0a5aea9f2 Minor bug fixes, update help text and tests
Thomas Treichl <Thomas.Treichl@gmx.net>
parents: 7017
diff changeset
216 %!test
bdb0a5aea9f2 Minor bug fixes, update help text and tests
Thomas Treichl <Thomas.Treichl@gmx.net>
parents: 7017
diff changeset
217 %! A = arrayfun (@(x) x, [false, true, false, true], "UniformOutput", false);
bdb0a5aea9f2 Minor bug fixes, update help text and tests
Thomas Treichl <Thomas.Treichl@gmx.net>
parents: 7017
diff changeset
218 %! assert (A, {false, true, false, true});
bdb0a5aea9f2 Minor bug fixes, update help text and tests
Thomas Treichl <Thomas.Treichl@gmx.net>
parents: 7017
diff changeset
219 %!test %% Three ouptut arguments of same type
bdb0a5aea9f2 Minor bug fixes, update help text and tests
Thomas Treichl <Thomas.Treichl@gmx.net>
parents: 7017
diff changeset
220 %! [A, B, C] = arrayfun (@find, [true, false; false, true], "UniformOutput", false);
bdb0a5aea9f2 Minor bug fixes, update help text and tests
Thomas Treichl <Thomas.Treichl@gmx.net>
parents: 7017
diff changeset
221 %! assert (isequal (A, {true, []; [], true}));
bdb0a5aea9f2 Minor bug fixes, update help text and tests
Thomas Treichl <Thomas.Treichl@gmx.net>
parents: 7017
diff changeset
222 %! assert (isequal (B, {true, []; [], true}));
bdb0a5aea9f2 Minor bug fixes, update help text and tests
Thomas Treichl <Thomas.Treichl@gmx.net>
parents: 7017
diff changeset
223 %! assert (isequal (C, {true, []; [], true}));
bdb0a5aea9f2 Minor bug fixes, update help text and tests
Thomas Treichl <Thomas.Treichl@gmx.net>
parents: 7017
diff changeset
224 %!test
bdb0a5aea9f2 Minor bug fixes, update help text and tests
Thomas Treichl <Thomas.Treichl@gmx.net>
parents: 7017
diff changeset
225 %! A = arrayfun (@(x,y) array2str (x,y), true, true, "ErrorHandler", @arrayfunerror);
bdb0a5aea9f2 Minor bug fixes, update help text and tests
Thomas Treichl <Thomas.Treichl@gmx.net>
parents: 7017
diff changeset
226 %! assert (isfield (A, "identifier"), true);
bdb0a5aea9f2 Minor bug fixes, update help text and tests
Thomas Treichl <Thomas.Treichl@gmx.net>
parents: 7017
diff changeset
227 %! assert (isfield (A, "message"), true);
bdb0a5aea9f2 Minor bug fixes, update help text and tests
Thomas Treichl <Thomas.Treichl@gmx.net>
parents: 7017
diff changeset
228 %! assert (isfield (A, "index"), true);
bdb0a5aea9f2 Minor bug fixes, update help text and tests
Thomas Treichl <Thomas.Treichl@gmx.net>
parents: 7017
diff changeset
229 %! assert (isempty (A.message), false);
bdb0a5aea9f2 Minor bug fixes, update help text and tests
Thomas Treichl <Thomas.Treichl@gmx.net>
parents: 7017
diff changeset
230 %! assert (A.index, 1);
bdb0a5aea9f2 Minor bug fixes, update help text and tests
Thomas Treichl <Thomas.Treichl@gmx.net>
parents: 7017
diff changeset
231 %!test %% Overwriting setting of "UniformOutput" true
bdb0a5aea9f2 Minor bug fixes, update help text and tests
Thomas Treichl <Thomas.Treichl@gmx.net>
parents: 7017
diff changeset
232 %! A = arrayfun (@(x,y) array2str (x,y), true, true, \
bdb0a5aea9f2 Minor bug fixes, update help text and tests
Thomas Treichl <Thomas.Treichl@gmx.net>
parents: 7017
diff changeset
233 %! "UniformOutput", true, "ErrorHandler", @arrayfunerror);
bdb0a5aea9f2 Minor bug fixes, update help text and tests
Thomas Treichl <Thomas.Treichl@gmx.net>
parents: 7017
diff changeset
234 %! assert (isfield (A, "identifier"), true);
bdb0a5aea9f2 Minor bug fixes, update help text and tests
Thomas Treichl <Thomas.Treichl@gmx.net>
parents: 7017
diff changeset
235 %! assert (isfield (A, "message"), true);
bdb0a5aea9f2 Minor bug fixes, update help text and tests
Thomas Treichl <Thomas.Treichl@gmx.net>
parents: 7017
diff changeset
236 %! assert (isfield (A, "index"), true);
bdb0a5aea9f2 Minor bug fixes, update help text and tests
Thomas Treichl <Thomas.Treichl@gmx.net>
parents: 7017
diff changeset
237 %! assert (isempty (A.message), false);
bdb0a5aea9f2 Minor bug fixes, update help text and tests
Thomas Treichl <Thomas.Treichl@gmx.net>
parents: 7017
diff changeset
238 %! assert (A.index, 1);
bdb0a5aea9f2 Minor bug fixes, update help text and tests
Thomas Treichl <Thomas.Treichl@gmx.net>
parents: 7017
diff changeset
239
bdb0a5aea9f2 Minor bug fixes, update help text and tests
Thomas Treichl <Thomas.Treichl@gmx.net>
parents: 7017
diff changeset
240 %% Input arguments can be of type numeric
bdb0a5aea9f2 Minor bug fixes, update help text and tests
Thomas Treichl <Thomas.Treichl@gmx.net>
parents: 7017
diff changeset
241 %!test
9622
40cbcdf12b54 fix broken tests
Jaroslav Hajek <highegg@gmail.com>
parents: 8920
diff changeset
242 %! A = arrayfun (@(x,y) x>y, [1.1, 4.2], [3.1, 2+3*i]);
8357
bdb0a5aea9f2 Minor bug fixes, update help text and tests
Thomas Treichl <Thomas.Treichl@gmx.net>
parents: 7017
diff changeset
243 %! assert (A, [false, true]);
bdb0a5aea9f2 Minor bug fixes, update help text and tests
Thomas Treichl <Thomas.Treichl@gmx.net>
parents: 7017
diff changeset
244 %!test
bdb0a5aea9f2 Minor bug fixes, update help text and tests
Thomas Treichl <Thomas.Treichl@gmx.net>
parents: 7017
diff changeset
245 %! A = arrayfun (@(x,y) x>y, [1.1, 4.2; 2, 4], [3.1, 2; 2, 4+2*i], "UniformOutput", true);
bdb0a5aea9f2 Minor bug fixes, update help text and tests
Thomas Treichl <Thomas.Treichl@gmx.net>
parents: 7017
diff changeset
246 %! assert (A, [false, true; false, false]);
bdb0a5aea9f2 Minor bug fixes, update help text and tests
Thomas Treichl <Thomas.Treichl@gmx.net>
parents: 7017
diff changeset
247 %!test
bdb0a5aea9f2 Minor bug fixes, update help text and tests
Thomas Treichl <Thomas.Treichl@gmx.net>
parents: 7017
diff changeset
248 %! A = arrayfun (@(x,y) x:y, [1.1, 4], [3.1, 6], "UniformOutput", false);
bdb0a5aea9f2 Minor bug fixes, update help text and tests
Thomas Treichl <Thomas.Treichl@gmx.net>
parents: 7017
diff changeset
249 %! assert (isequal (A{1}, [1.1, 2.1, 3.1]));
bdb0a5aea9f2 Minor bug fixes, update help text and tests
Thomas Treichl <Thomas.Treichl@gmx.net>
parents: 7017
diff changeset
250 %! assert (isequal (A{2}, [4, 5, 6]));
bdb0a5aea9f2 Minor bug fixes, update help text and tests
Thomas Treichl <Thomas.Treichl@gmx.net>
parents: 7017
diff changeset
251 %!test %% Three ouptut arguments of different type
bdb0a5aea9f2 Minor bug fixes, update help text and tests
Thomas Treichl <Thomas.Treichl@gmx.net>
parents: 7017
diff changeset
252 %! [A, B, C] = arrayfun (@find, [10, 11; 0, 12], "UniformOutput", false);
bdb0a5aea9f2 Minor bug fixes, update help text and tests
Thomas Treichl <Thomas.Treichl@gmx.net>
parents: 7017
diff changeset
253 %! assert (isequal (A, {true, true; [], true}));
bdb0a5aea9f2 Minor bug fixes, update help text and tests
Thomas Treichl <Thomas.Treichl@gmx.net>
parents: 7017
diff changeset
254 %! assert (isequal (B, {true, true; [], true}));
bdb0a5aea9f2 Minor bug fixes, update help text and tests
Thomas Treichl <Thomas.Treichl@gmx.net>
parents: 7017
diff changeset
255 %! assert (isequal (C, {10, 11; [], 12}));
bdb0a5aea9f2 Minor bug fixes, update help text and tests
Thomas Treichl <Thomas.Treichl@gmx.net>
parents: 7017
diff changeset
256 %!test
bdb0a5aea9f2 Minor bug fixes, update help text and tests
Thomas Treichl <Thomas.Treichl@gmx.net>
parents: 7017
diff changeset
257 %! A = arrayfun (@(x,y) array2str(x,y), {1.1, 4}, {3.1, 6}, "ErrorHandler", @arrayfunerror);
bdb0a5aea9f2 Minor bug fixes, update help text and tests
Thomas Treichl <Thomas.Treichl@gmx.net>
parents: 7017
diff changeset
258 %! B = isfield (A(1), "message") && isfield (A(1), "index");
bdb0a5aea9f2 Minor bug fixes, update help text and tests
Thomas Treichl <Thomas.Treichl@gmx.net>
parents: 7017
diff changeset
259 %! assert ([(isfield (A(1), "identifier")), (isfield (A(2), "identifier"))], [true, true]);
bdb0a5aea9f2 Minor bug fixes, update help text and tests
Thomas Treichl <Thomas.Treichl@gmx.net>
parents: 7017
diff changeset
260 %! assert ([(isfield (A(1), "message")), (isfield (A(2), "message"))], [true, true]);
bdb0a5aea9f2 Minor bug fixes, update help text and tests
Thomas Treichl <Thomas.Treichl@gmx.net>
parents: 7017
diff changeset
261 %! assert ([(isfield (A(1), "index")), (isfield (A(2), "index"))], [true, true]);
bdb0a5aea9f2 Minor bug fixes, update help text and tests
Thomas Treichl <Thomas.Treichl@gmx.net>
parents: 7017
diff changeset
262 %! assert ([(isempty (A(1).message)), (isempty (A(2).message))], [false, false]);
bdb0a5aea9f2 Minor bug fixes, update help text and tests
Thomas Treichl <Thomas.Treichl@gmx.net>
parents: 7017
diff changeset
263 %! assert ([A(1).index, A(2).index], [1, 2]);
bdb0a5aea9f2 Minor bug fixes, update help text and tests
Thomas Treichl <Thomas.Treichl@gmx.net>
parents: 7017
diff changeset
264 %!test %% Overwriting setting of "UniformOutput" true
bdb0a5aea9f2 Minor bug fixes, update help text and tests
Thomas Treichl <Thomas.Treichl@gmx.net>
parents: 7017
diff changeset
265 %! A = arrayfun (@(x,y) array2str(x,y), {1.1, 4}, {3.1, 6}, \
bdb0a5aea9f2 Minor bug fixes, update help text and tests
Thomas Treichl <Thomas.Treichl@gmx.net>
parents: 7017
diff changeset
266 %! "UniformOutput", true, "ErrorHandler", @arrayfunerror);
bdb0a5aea9f2 Minor bug fixes, update help text and tests
Thomas Treichl <Thomas.Treichl@gmx.net>
parents: 7017
diff changeset
267 %! B = isfield (A(1), "message") && isfield (A(1), "index");
bdb0a5aea9f2 Minor bug fixes, update help text and tests
Thomas Treichl <Thomas.Treichl@gmx.net>
parents: 7017
diff changeset
268 %! assert ([(isfield (A(1), "identifier")), (isfield (A(2), "identifier"))], [true, true]);
bdb0a5aea9f2 Minor bug fixes, update help text and tests
Thomas Treichl <Thomas.Treichl@gmx.net>
parents: 7017
diff changeset
269 %! assert ([(isfield (A(1), "message")), (isfield (A(2), "message"))], [true, true]);
bdb0a5aea9f2 Minor bug fixes, update help text and tests
Thomas Treichl <Thomas.Treichl@gmx.net>
parents: 7017
diff changeset
270 %! assert ([(isfield (A(1), "index")), (isfield (A(2), "index"))], [true, true]);
bdb0a5aea9f2 Minor bug fixes, update help text and tests
Thomas Treichl <Thomas.Treichl@gmx.net>
parents: 7017
diff changeset
271 %! assert ([(isempty (A(1).message)), (isempty (A(2).message))], [false, false]);
bdb0a5aea9f2 Minor bug fixes, update help text and tests
Thomas Treichl <Thomas.Treichl@gmx.net>
parents: 7017
diff changeset
272 %! assert ([A(1).index, A(2).index], [1, 2]);
bdb0a5aea9f2 Minor bug fixes, update help text and tests
Thomas Treichl <Thomas.Treichl@gmx.net>
parents: 7017
diff changeset
273
bdb0a5aea9f2 Minor bug fixes, update help text and tests
Thomas Treichl <Thomas.Treichl@gmx.net>
parents: 7017
diff changeset
274 %% Input arguments can be of type character or strings
bdb0a5aea9f2 Minor bug fixes, update help text and tests
Thomas Treichl <Thomas.Treichl@gmx.net>
parents: 7017
diff changeset
275 %!test
bdb0a5aea9f2 Minor bug fixes, update help text and tests
Thomas Treichl <Thomas.Treichl@gmx.net>
parents: 7017
diff changeset
276 %! A = arrayfun (@(x,y) x>y, ["ad", "c", "ghi"], ["cc", "d", "fgh"]);
bdb0a5aea9f2 Minor bug fixes, update help text and tests
Thomas Treichl <Thomas.Treichl@gmx.net>
parents: 7017
diff changeset
277 %! assert (A, [false, true, false, true, true, true]);
bdb0a5aea9f2 Minor bug fixes, update help text and tests
Thomas Treichl <Thomas.Treichl@gmx.net>
parents: 7017
diff changeset
278 %!test
bdb0a5aea9f2 Minor bug fixes, update help text and tests
Thomas Treichl <Thomas.Treichl@gmx.net>
parents: 7017
diff changeset
279 %! A = arrayfun (@(x,y) x>y, ["a"; "f"], ["c"; "d"], "UniformOutput", true);
bdb0a5aea9f2 Minor bug fixes, update help text and tests
Thomas Treichl <Thomas.Treichl@gmx.net>
parents: 7017
diff changeset
280 %! assert (A, [false; true]);
bdb0a5aea9f2 Minor bug fixes, update help text and tests
Thomas Treichl <Thomas.Treichl@gmx.net>
parents: 7017
diff changeset
281 %!test
bdb0a5aea9f2 Minor bug fixes, update help text and tests
Thomas Treichl <Thomas.Treichl@gmx.net>
parents: 7017
diff changeset
282 %! A = arrayfun (@(x,y) x:y, ["a", "d"], ["c", "f"], "UniformOutput", false);
bdb0a5aea9f2 Minor bug fixes, update help text and tests
Thomas Treichl <Thomas.Treichl@gmx.net>
parents: 7017
diff changeset
283 %! assert (A, {"abc", "def"});
bdb0a5aea9f2 Minor bug fixes, update help text and tests
Thomas Treichl <Thomas.Treichl@gmx.net>
parents: 7017
diff changeset
284 %! %#!test
bdb0a5aea9f2 Minor bug fixes, update help text and tests
Thomas Treichl <Thomas.Treichl@gmx.net>
parents: 7017
diff changeset
285 %! A = arrayfun (@(x,y) cell2str(x,y), ["a", "d"], ["c", "f"], "ErrorHandler", @arrayfunerror);
bdb0a5aea9f2 Minor bug fixes, update help text and tests
Thomas Treichl <Thomas.Treichl@gmx.net>
parents: 7017
diff changeset
286 %! B = isfield (A(1), "identifier") && isfield (A(1), "message") && isfield (A(1), "index");
bdb0a5aea9f2 Minor bug fixes, update help text and tests
Thomas Treichl <Thomas.Treichl@gmx.net>
parents: 7017
diff changeset
287 %! assert (B, true);
bdb0a5aea9f2 Minor bug fixes, update help text and tests
Thomas Treichl <Thomas.Treichl@gmx.net>
parents: 7017
diff changeset
288
bdb0a5aea9f2 Minor bug fixes, update help text and tests
Thomas Treichl <Thomas.Treichl@gmx.net>
parents: 7017
diff changeset
289 %% Input arguments can be of type structure
bdb0a5aea9f2 Minor bug fixes, update help text and tests
Thomas Treichl <Thomas.Treichl@gmx.net>
parents: 7017
diff changeset
290 %!test
bdb0a5aea9f2 Minor bug fixes, update help text and tests
Thomas Treichl <Thomas.Treichl@gmx.net>
parents: 7017
diff changeset
291 %! a = struct ("a", 1.1, "b", 4.2); b = struct ("a", 3.1, "b", 2);
bdb0a5aea9f2 Minor bug fixes, update help text and tests
Thomas Treichl <Thomas.Treichl@gmx.net>
parents: 7017
diff changeset
292 %! A = arrayfun (@(x,y) (x.a < y.a) && (x.b > y.b), a, b);
bdb0a5aea9f2 Minor bug fixes, update help text and tests
Thomas Treichl <Thomas.Treichl@gmx.net>
parents: 7017
diff changeset
293 %! assert (A, true);
bdb0a5aea9f2 Minor bug fixes, update help text and tests
Thomas Treichl <Thomas.Treichl@gmx.net>
parents: 7017
diff changeset
294 %!test
bdb0a5aea9f2 Minor bug fixes, update help text and tests
Thomas Treichl <Thomas.Treichl@gmx.net>
parents: 7017
diff changeset
295 %! a = struct ("a", 1.1, "b", 4.2); b = struct ("a", 3.1, "b", 2);
bdb0a5aea9f2 Minor bug fixes, update help text and tests
Thomas Treichl <Thomas.Treichl@gmx.net>
parents: 7017
diff changeset
296 %! A = arrayfun (@(x,y) (x.a < y.a) && (x.b > y.b), a, b, "UniformOutput", true);
bdb0a5aea9f2 Minor bug fixes, update help text and tests
Thomas Treichl <Thomas.Treichl@gmx.net>
parents: 7017
diff changeset
297 %! assert (A, true);
bdb0a5aea9f2 Minor bug fixes, update help text and tests
Thomas Treichl <Thomas.Treichl@gmx.net>
parents: 7017
diff changeset
298 %!test
bdb0a5aea9f2 Minor bug fixes, update help text and tests
Thomas Treichl <Thomas.Treichl@gmx.net>
parents: 7017
diff changeset
299 %! a = struct ("a", 1.1, "b", 4.2); b = struct ("a", 3.1, "b", 2);
bdb0a5aea9f2 Minor bug fixes, update help text and tests
Thomas Treichl <Thomas.Treichl@gmx.net>
parents: 7017
diff changeset
300 %! A = arrayfun (@(x,y) x.a:y.a, a, b, "UniformOutput", false);
bdb0a5aea9f2 Minor bug fixes, update help text and tests
Thomas Treichl <Thomas.Treichl@gmx.net>
parents: 7017
diff changeset
301 %! assert (isequal (A, {[1.1, 2.1, 3.1]}));
bdb0a5aea9f2 Minor bug fixes, update help text and tests
Thomas Treichl <Thomas.Treichl@gmx.net>
parents: 7017
diff changeset
302 %!test
bdb0a5aea9f2 Minor bug fixes, update help text and tests
Thomas Treichl <Thomas.Treichl@gmx.net>
parents: 7017
diff changeset
303 %! A = arrayfun (@(x) mat2str(x), "a", "ErrorHandler", @arrayfunerror);
bdb0a5aea9f2 Minor bug fixes, update help text and tests
Thomas Treichl <Thomas.Treichl@gmx.net>
parents: 7017
diff changeset
304 %! assert (isfield (A, "identifier"), true);
bdb0a5aea9f2 Minor bug fixes, update help text and tests
Thomas Treichl <Thomas.Treichl@gmx.net>
parents: 7017
diff changeset
305 %! assert (isfield (A, "message"), true);
bdb0a5aea9f2 Minor bug fixes, update help text and tests
Thomas Treichl <Thomas.Treichl@gmx.net>
parents: 7017
diff changeset
306 %! assert (isfield (A, "index"), true);
bdb0a5aea9f2 Minor bug fixes, update help text and tests
Thomas Treichl <Thomas.Treichl@gmx.net>
parents: 7017
diff changeset
307 %! assert (isempty (A.message), false);
bdb0a5aea9f2 Minor bug fixes, update help text and tests
Thomas Treichl <Thomas.Treichl@gmx.net>
parents: 7017
diff changeset
308 %! assert (A.index, 1);
bdb0a5aea9f2 Minor bug fixes, update help text and tests
Thomas Treichl <Thomas.Treichl@gmx.net>
parents: 7017
diff changeset
309 %!test %% Overwriting setting of "UniformOutput" true
bdb0a5aea9f2 Minor bug fixes, update help text and tests
Thomas Treichl <Thomas.Treichl@gmx.net>
parents: 7017
diff changeset
310 %! A = arrayfun (@(x) mat2str(x), "a", "UniformOutput", true, \
bdb0a5aea9f2 Minor bug fixes, update help text and tests
Thomas Treichl <Thomas.Treichl@gmx.net>
parents: 7017
diff changeset
311 %! "ErrorHandler", @arrayfunerror);
bdb0a5aea9f2 Minor bug fixes, update help text and tests
Thomas Treichl <Thomas.Treichl@gmx.net>
parents: 7017
diff changeset
312 %! assert (isfield (A, "identifier"), true);
bdb0a5aea9f2 Minor bug fixes, update help text and tests
Thomas Treichl <Thomas.Treichl@gmx.net>
parents: 7017
diff changeset
313 %! assert (isfield (A, "message"), true);
bdb0a5aea9f2 Minor bug fixes, update help text and tests
Thomas Treichl <Thomas.Treichl@gmx.net>
parents: 7017
diff changeset
314 %! assert (isfield (A, "index"), true);
bdb0a5aea9f2 Minor bug fixes, update help text and tests
Thomas Treichl <Thomas.Treichl@gmx.net>
parents: 7017
diff changeset
315 %! assert (isempty (A.message), false);
bdb0a5aea9f2 Minor bug fixes, update help text and tests
Thomas Treichl <Thomas.Treichl@gmx.net>
parents: 7017
diff changeset
316 %! assert (A.index, 1);
bdb0a5aea9f2 Minor bug fixes, update help text and tests
Thomas Treichl <Thomas.Treichl@gmx.net>
parents: 7017
diff changeset
317
bdb0a5aea9f2 Minor bug fixes, update help text and tests
Thomas Treichl <Thomas.Treichl@gmx.net>
parents: 7017
diff changeset
318 %% Input arguments can be of type cell array
bdb0a5aea9f2 Minor bug fixes, update help text and tests
Thomas Treichl <Thomas.Treichl@gmx.net>
parents: 7017
diff changeset
319 %!test
bdb0a5aea9f2 Minor bug fixes, update help text and tests
Thomas Treichl <Thomas.Treichl@gmx.net>
parents: 7017
diff changeset
320 %! A = arrayfun (@(x,y) x{1} < y{1}, {1.1, 4.2}, {3.1, 2});
bdb0a5aea9f2 Minor bug fixes, update help text and tests
Thomas Treichl <Thomas.Treichl@gmx.net>
parents: 7017
diff changeset
321 %! assert (A, [true, false]);
bdb0a5aea9f2 Minor bug fixes, update help text and tests
Thomas Treichl <Thomas.Treichl@gmx.net>
parents: 7017
diff changeset
322 %!test
bdb0a5aea9f2 Minor bug fixes, update help text and tests
Thomas Treichl <Thomas.Treichl@gmx.net>
parents: 7017
diff changeset
323 %! A = arrayfun (@(x,y) x{1} < y{1}, {1.1; 4.2}, {3.1; 2}, "UniformOutput", true);
bdb0a5aea9f2 Minor bug fixes, update help text and tests
Thomas Treichl <Thomas.Treichl@gmx.net>
parents: 7017
diff changeset
324 %! assert (A, [true; false]);
bdb0a5aea9f2 Minor bug fixes, update help text and tests
Thomas Treichl <Thomas.Treichl@gmx.net>
parents: 7017
diff changeset
325 %!test
bdb0a5aea9f2 Minor bug fixes, update help text and tests
Thomas Treichl <Thomas.Treichl@gmx.net>
parents: 7017
diff changeset
326 %! A = arrayfun (@(x,y) x{1} < y{1}, {1.1, 4.2}, {3.1, 2}, "UniformOutput", false);
bdb0a5aea9f2 Minor bug fixes, update help text and tests
Thomas Treichl <Thomas.Treichl@gmx.net>
parents: 7017
diff changeset
327 %! assert (A, {true, false});
bdb0a5aea9f2 Minor bug fixes, update help text and tests
Thomas Treichl <Thomas.Treichl@gmx.net>
parents: 7017
diff changeset
328 %!test
bdb0a5aea9f2 Minor bug fixes, update help text and tests
Thomas Treichl <Thomas.Treichl@gmx.net>
parents: 7017
diff changeset
329 %! A = arrayfun (@(x,y) num2str(x,y), {1.1, 4.2}, {3.1, 2}, "ErrorHandler", @arrayfunerror);
bdb0a5aea9f2 Minor bug fixes, update help text and tests
Thomas Treichl <Thomas.Treichl@gmx.net>
parents: 7017
diff changeset
330 %! assert ([(isfield (A(1), "identifier")), (isfield (A(2), "identifier"))], [true, true]);
bdb0a5aea9f2 Minor bug fixes, update help text and tests
Thomas Treichl <Thomas.Treichl@gmx.net>
parents: 7017
diff changeset
331 %! assert ([(isfield (A(1), "message")), (isfield (A(2), "message"))], [true, true]);
bdb0a5aea9f2 Minor bug fixes, update help text and tests
Thomas Treichl <Thomas.Treichl@gmx.net>
parents: 7017
diff changeset
332 %! assert ([(isfield (A(1), "index")), (isfield (A(2), "index"))], [true, true]);
bdb0a5aea9f2 Minor bug fixes, update help text and tests
Thomas Treichl <Thomas.Treichl@gmx.net>
parents: 7017
diff changeset
333 %! assert ([(isempty (A(1).message)), (isempty (A(2).message))], [false, false]);
bdb0a5aea9f2 Minor bug fixes, update help text and tests
Thomas Treichl <Thomas.Treichl@gmx.net>
parents: 7017
diff changeset
334 %! assert ([A(1).index, A(2).index], [1, 2]);
bdb0a5aea9f2 Minor bug fixes, update help text and tests
Thomas Treichl <Thomas.Treichl@gmx.net>
parents: 7017
diff changeset
335 %!test
bdb0a5aea9f2 Minor bug fixes, update help text and tests
Thomas Treichl <Thomas.Treichl@gmx.net>
parents: 7017
diff changeset
336 %! A = arrayfun (@(x,y) num2str(x,y), {1.1, 4.2}, {3.1, 2}, \
bdb0a5aea9f2 Minor bug fixes, update help text and tests
Thomas Treichl <Thomas.Treichl@gmx.net>
parents: 7017
diff changeset
337 %! "UniformOutput", true, "ErrorHandler", @arrayfunerror);
bdb0a5aea9f2 Minor bug fixes, update help text and tests
Thomas Treichl <Thomas.Treichl@gmx.net>
parents: 7017
diff changeset
338 %! assert ([(isfield (A(1), "identifier")), (isfield (A(2), "identifier"))], [true, true]);
bdb0a5aea9f2 Minor bug fixes, update help text and tests
Thomas Treichl <Thomas.Treichl@gmx.net>
parents: 7017
diff changeset
339 %! assert ([(isfield (A(1), "message")), (isfield (A(2), "message"))], [true, true]);
bdb0a5aea9f2 Minor bug fixes, update help text and tests
Thomas Treichl <Thomas.Treichl@gmx.net>
parents: 7017
diff changeset
340 %! assert ([(isfield (A(1), "index")), (isfield (A(2), "index"))], [true, true]);
bdb0a5aea9f2 Minor bug fixes, update help text and tests
Thomas Treichl <Thomas.Treichl@gmx.net>
parents: 7017
diff changeset
341 %! assert ([(isempty (A(1).message)), (isempty (A(2).message))], [false, false]);
bdb0a5aea9f2 Minor bug fixes, update help text and tests
Thomas Treichl <Thomas.Treichl@gmx.net>
parents: 7017
diff changeset
342 %! assert ([A(1).index, A(2).index], [1, 2]);
bdb0a5aea9f2 Minor bug fixes, update help text and tests
Thomas Treichl <Thomas.Treichl@gmx.net>
parents: 7017
diff changeset
343
bdb0a5aea9f2 Minor bug fixes, update help text and tests
Thomas Treichl <Thomas.Treichl@gmx.net>
parents: 7017
diff changeset
344 ## Local Variables: ***
bdb0a5aea9f2 Minor bug fixes, update help text and tests
Thomas Treichl <Thomas.Treichl@gmx.net>
parents: 7017
diff changeset
345 ## mode: octave ***
bdb0a5aea9f2 Minor bug fixes, update help text and tests
Thomas Treichl <Thomas.Treichl@gmx.net>
parents: 7017
diff changeset
346 ## End: ***