User`s guide

arrayfun
11-5
all have the same size or be scalar. Any scalar inputs are scalar expanded before being
input to the function FUN.
One or more of the inputs B, C, ... must be a gpuArray; any of the others can reside
in CPU memory. Each array that is held in CPU memory is converted to a gpuArray
before calling the function on the GPU. If you plan to use an array in several different
arrayfun calls, it is more efficient to convert that array to a gpuArray before making the
series of calls to arrayfun.
[A,B,...] = arrayfun(FUN,C,...), where FUN is a function handle to a function
that returns multiple outputs, returns gpuArrays A, B, ..., each corresponding to one of
the output arguments of FUN. arrayfun calls FUN each time with as many outputs as
there are in the call to arrayfun. FUN can return output arguments having different
classes, but the class of each output must be the same each time FUN is called. This
means that all elements of A must be the same class; B can be a different class from A,
but all elements of B must be of the same class, etc.
Although the MATLAB arrayfun function allows you to specify optional parameter
name/value pairs, the gpuArray arrayfun method does not support these options.
Tips
The first time you call arrayfun to run a particular function on the GPU, there is
some overhead time to set up the function for GPU execution. Subsequent calls of
arrayfun with the same function can run significantly faster.
Nonsingleton dimensions of input arrays must match each other. In other words,
the corresponding dimensions of arguments B, C, etc., must be equal to each other,
or equal to one. Whenever a dimension of an input array is singleton (equal to 1),
arrayfun uses singleton expansion to virtually replicate the array along that
dimension to match the largest of the other arrays in that dimension. In the case
where a dimension of an input array is singleton and the corresponding dimension
in another argument array is zero, arrayfun virtually diminishes the singleton
dimension to 0.
The size of the output array A is such that each dimension is the largest of the
input arrays in that dimension for nonzero size, or zero otherwise. Notice in the
following code how dimensions of size 1 are scaled up or down to match the size of the
corresponding dimension in the other argument:
R1 = rand(2,5,4,'gpuArray');