Data structures cell >>

arrayfun


Apply a function to each element of an array.


Syntax


B = arrayfun(func, A)
B = arrayfun(func, A1, ..., An)
B = arrayfun(..., 'UniformOutput', false)
[B1, ..., Bm] = arrayfun(...)

Input argument


func

function handle to apply on each element. Must return scalar unless 'UniformOutput' is false.

A, A1, ..., An

input arrays of the same size.

'UniformOutput'

logical scalar. If false, outputs are returned in a cell array.

Output argument


B, B1, ..., Bm

outputs of function applied elementwise. Cell array if 'UniformOutput' is false.

Description


arrayfun(func, A) applies the function func to each element of array A, and returns the result in B with the same size as A.

arrayfun(func, A1, ..., An) applies func to corresponding elements of input arrays. All arrays must be the same size.

Use the 'UniformOutput' option set to false to allow output values that cannot be concatenated into a single array. In this case, the result is a cell array.

[B1, ..., Bm] = arrayfun(...) captures multiple outputs from the applied function.

Examples


Apply mean to structure field

S(1).f1 = rand(1, 5);
S(2).f1 = rand(1, 10);
S(3).f1 = rand(1, 15);
means = arrayfun(@(x) mean(x.f1), S);

Return multiple outputs from function

f = @(x) deal(x, x^2);
[A, B] = arrayfun(f, 1:4);

Return variable-sized outputs in a cell array

S(1).f1 = rand(3,5);
S(2).f1 = rand(2,6);
A = arrayfun(@(x) mean(x.f1), S, 'UniformOutput', false);

See also


cellfun.

History


Version Description
1.14.0 initial version

Author


Allan CORNET

Data structures cell >>