Function evaluation with arguments in lists.
listout = apply(fun, listin) listout = apply(fun, listin, nargout) listout = apply(fun, listin, na) listout = apply(fun, listin, nargout, na)
listout=apply(fun,listin) evaluates function fun with input arguments taken from the elements of list listin. Output arguments are grouped in list listout. Function fun is specified by either its name as a string, a function reference, or an anonymous or inline function.
The number of expected output arguments can be specified with an optional third input argument nargout. By default, the maximum number of output arguments is requested, up to 256; this limit exists to prevent functions with an unlimited number of output arguments, such as deal, from filling memory.
With a 4th argument na (or 3rd if nargout is not specified), named arguments can be provided as a structure.
apply(@size, {magic(3)}, 2) {3, 3} apply(@(x,y) 2*x+3*y, {5, 10}) {40}
The maximum number of output arguments of min is 2 (minimum value and its index):
apply(@min, {[8, 3, 4, 7]}) {3, 2}
Two equivalent ways of calling disp with a named argument fd to specify the standard error file descriptor 2:
disp(123, fd=2); apply(@disp, {123}, 0, {fd=2});
map, feval, inline, operator @, varargin, namedargin, varargout
List concatenation.
list = join(l1, l2, ...)
join(l1,l2,...) joins elements of lists l1, l2, etc. to make a larger list.
join({1,'a',2:5}, {4,2}, {{'xxx'}}) {1,'a',[2,3,4,5],4,2,{'xxx'}} join() {}
operator ,, operator ;, replist
Test for a list object.
b = islist(obj)
islist(obj) is true if the object obj is a list, false otherwise.
islist({1, 2, 'x'}) true islist({}) true islist([]) false ischar('') false
isstruct, isnumeric, ischar, islogical, isempty
Conversion from list to numeric array.
A = list2num(list)
list2num(list) takes the elements of list, which must be numbers or arrays, and concatenates them on a row (along second dimension) as if they were placed inside brackets and separated with commas. Element sizes must be compatible.
list2num({1, 2+3j, 4:6}) 1 2+3j 4 5 6
num2list, operator [], operator ,
Function evaluation for each element of a list
(listout1,...) = map(fun, listin1, ...)
map(fun,listin1,...) evaluates function fun successively for each corresponding elements of the remaining arguments, which must be lists or cell arrays. It returns the result(s) of the evaluation as list(s) or cell array(s) with the same size as inputs. Input lists which contain a single element are repeated to match other arguments if necessary. Function fun is specified by either its name as a string, a function reference, or an anonymous or inline function.
map('max', {[2,6,4], [7,-1], 1:100}) {6, 7, 100} map(@(x) x+10, {3,7,16}) {13, 17, 26} (nr, nc) = map(@size, {1,'abc',[4,7;3,4]}) nr = {1,1,2} nc = {1,3,2} s = map(@size, {1,'abc',[4,7;3,4]}) s = {[1,1], [1,3], [2,2]} map(@disp, {'hello', 'lme'}) hello lme
Lists with single elements are expanded to match the size of other lists. The following example computes atan2(1,2) and atan2(1,3):
map(@atan2, {1}, {2,3}) {0.4636,0.3218}
apply, cellfun, for, inline, operator @
Conversion from array to list.
list = num2list(A) list = num2list(A, dim)
num2list(A) creates a list with the elements of non-cell array A.
num2list(A,dim) cuts array A along dimension dim and creates a list with the result.
num2list(1:5) {1, 2, 3, 4, 5} num2list([1,2;3,4]) {1, 2, 3, 4} num2list([1, 2; 3, 4], 1) {[1, 2], [3, 4]} num2list([1, 2; 3, 4], 2) {[1; 3], [2; 4]}
Replicate a list.
listout = replist(listin, n)
replist(listin,n) makes a new list by concatenating n copies of list listin.
replist({1, 'abc'}, 3) {1,'abc',1,'abc',1,'abc'}