# bsxfun

Binary singleton expansion function for gpuArray

## Syntax

## Description

**Note**

The function

`arrayfun`

offers improved functionality compared to`bsxfun`

.`arrayfun`

is recommended.This function behaves similarly to the MATLAB

^{®}function`bsxfun`

, except that the evaluation of the function happens on the GPU, not on the CPU. Any required data not already on the GPU is moved to GPU memory. The MATLAB function passed in for evaluation is compiled and then executed on the GPU. All output arguments are returned as gpuArray objects. You can retrieve gpuArray data using the`gather`

function.

applies the element-by-element binary operation specified by `C`

= bsxfun(`FUN`

,`A,B`

)`FUN`

to
arrays `A`

and `B`

, with singleton expansion
enabled.

## Examples

## Input Arguments

## Output Arguments

## Tips

The first time you call

`bsxfun`

to run a particular function on the GPU, there is some overhead time to set up the function for GPU execution. Subsequent calls of`bsxfun`

with the same function can run faster.Nonsingleton dimensions of input arrays must match each other. In other words, the corresponding dimensions of arguments

`A`

,`B`

, etc., must be equal to each other, or equal to one. Whenever a dimension of an input array is singleton (equal to 1),`bsxfun`

uses singleton expansion. The array is replicated along the singleton dimension to match the largest of the other arrays in that dimension. When a dimension of an input array is singleton and the corresponding dimension in another argument array is zero,`bsxfun`

virtually diminishes the singleton dimension to 0.Each dimension of the output array

`C`

is the same size as the largest of the input arrays in that dimension for nonzero size, or zero otherwise. The following code shows how dimensions of size 1 are scaled up or down to match the size of the corresponding dimension in other arguments.R1 = rand(2,5,4,'gpuArray'); R2 = rand(2,1,4,3,'gpuArray'); R = bsxfun(@plus,R1,R2); size(R)

2 5 4 3

R1 = rand(2,2,0,4,'gpuArray'); R2 = rand(2,1,1,4,'gpuArray'); R = bsxfun(@plus,R1,R2); size(R)

2 2 0 4

Because the operations supported by

`bsxfun`

are strictly element-wise, and each computation of each element is performed independently of the others, certain restrictions are imposed:Input and output arrays cannot change shape or size.

Functions such as

`rand`

do not support size specifications. Arrays of random numbers have independent streams for each element.

Like

`bsxfun`

in MATLAB, matrix exponential power, multiplication, and division (`^`

,`*`

,`/`

,`\`

) perform element-wise calculations only.Operations that change the size or shape of the input or output arrays (

`cat`

,`reshape`

, and so on), are not supported.Read-only indexing (

`subsref`

) and access to variables of the parent (outer) function workspace from within nested functions is supported. You can index variables that exist in the function before the evaluation on the GPU. Assignment or`subsasgn`

indexing of these variables from within the nested function is not supported. For an example of the supported usage, see Stencil Operations on a GPUAnonymous functions do not have access to their parent function workspace.

Overloading the supported functions is not allowed.

The code cannot call scripts.

There is no

`ans`

variable to hold unassigned computation results. Make sure to explicitly assign to variables the results of all calculations.The following language features are not supported: persistent or global variables,

`parfor`

,`spmd`

,`switch`

, and`try`

/`catch`

.P-code files cannot contain a call to

`bsxfun`

with gpuArray data.

## Version History

**Introduced in R2012a**