# arrayfun

Apply function to each element of array

## Syntax

## Description

applies the function `B`

= arrayfun(`func`

,`A`

)`func`

to the elements of
`A`

, one element at a time. `arrayfun`

then
concatenates the outputs from `func`

into the output array
`B`

, so that for the `i`

th element of
`A`

, `B(i) = func(A(i))`

. The input argument
`func`

is a function handle to a function that takes one input
argument and returns a scalar. The output from `func`

can have any
data type, so long as objects of that type can be concatenated. (See Limitations for one
exception.) The arrays `A`

and `B`

have the same
size.

You cannot specify the order in which `arrayfun`

calculates the
elements of `B`

or rely on them being done in any particular
order.

applies `B`

= arrayfun(___,`Name,Value`

)`func`

with additional options specified by one or more
`Name,Value`

pair arguments. For example, to return output
values in a cell array, specify `'UniformOutput',false`

. You can
return `B`

as a cell array when `func`

returns
values that cannot be concatenated into an array. You can use
`Name,Value`

pair arguments with the input arguments of
either of the previous syntaxes.

`[B1,...,Bm] = arrayfun(___)`

returns multiple
output arrays `B1,...,Bm`

when `func`

returns
`m`

output values. `func`

can return output
arguments that have different data types, but the data type of each output must be
the same each time `func`

is called. You can use this syntax with
any of the input arguments of the previous syntaxes.

The number of output arguments from `func`

need not be the same
as the number of input arguments specified by `A1,...,An`

.

## Examples

## Input Arguments

## Output Arguments

## Limitations

**Heterogeneous Arrays**`arrayfun`

does not support heterogeneous arrays when`UniformOutput`

is set to`true`

.**Difference in Behavior for Input Arrays of Complex Numbers**If the input array

`A`

is an array of complex numbers, and some of the elements have imaginary parts equal to zero, then calling`arrayfun`

and indexing into the array can lead to different results. The`arrayfun`

function always treats such numbers as complex numbers with imaginary parts equal to zero. However, indexing returns such values as real numbers.To illustrate the difference in behavior, first create an array of complex numbers.

A = zeros(2,1); A(1) = 1; A(2) = 0 + 1i

A = 1.0000 + 0.0000i 0.0000 + 1.0000i

Then create a cell array and assign the elements of

`A`

to it. When you index into`A(1)`

, its value is returned as a real number because its imaginary part is equal to zero. And you can store real and complex values in different cells of`C1`

because cell arrays can store data having different types.C1 = cell(2,1); C1{1} = A(1); C1{2} = A(2)

C1 = 2×1 cell array {[ 1]} {[0.0000 + 1.0000i]}

Call

`arrayfun`

and access the elements of`A`

. Assign its values to a cell array. When`arrayfun`

accesses`A(1)`

, it treats that value as a complex number and assigns it to`C2{1}`

.`C2 = arrayfun(@(x) x, A, 'UniformOutput', false)`

C2 = 2×1 cell array {[1.0000 + 0.0000i]} {[0.0000 + 1.0000i]}

## Extended Capabilities

## Version History

**Introduced before R2006a**

## See Also

`structfun`

| `cellfun`

| `spfun`

| `cell2mat`

| `splitapply`

| `varfun`

| `rowfun`

| `groupsummary`