Accelerating the pace of engineering and science

# Documentation Center

• Trial Software

# bsxfun

Apply element-by-element binary operation to two arrays with singleton expansion enabled

## Syntax

C = bsxfun(fun,A,B)

## Description

C = bsxfun(fun,A,B) applies the element-by-element binary operation specified by the function handle fun to arrays A and B, with singleton expansion enabled. fun can be one of the following built-in functions:

 @plus Plus @minus Minus @times Array multiply @rdivide Right array divide @ldivide Left array divide @power Array power @max Binary maximum @min Binary minimum @rem Remainder after division @mod Modulus after division @atan2 Four-quadrant inverse tangent; result in radians @atan2d Four-quadrant inverse tangent; result in degrees @hypot Square root of sum of squares @eq Equal @ne Not equal @lt Less than @le Less than or equal to @gt Greater than @ge Greater than or equal to @and Element-wise logical AND @or Element-wise logical OR @xor Logical exclusive OR

fun can also be a handle to any binary element-wise function not listed above. A binary element-wise function of the form C = fun(A,B) accepts arrays A and B of arbitrary, but equal size and returns output of the same size. Each element in the output array C is the result of an operation on the corresponding elements of A and B only. fun must also support scalar expansion, such that if A or B is a scalar, C is the result of applying the scalar to every element in the other input array.

The corresponding dimensions of A and B must be equal to each other or equal to one. Whenever a dimension of A or B is singleton (equal to one), bsxfun virtually replicates the array along that dimension to match the other array. In the case where a dimension of A or B is singleton, and the corresponding dimension in the other array is zero, bsxfun virtually diminishes the singleton dimension to zero.

The size of the output array C is equal to:
max(size(A),size(B)).*(size(A)>0 & size(B)>0).

## Examples

expand all

### Deviations from Mean

Use bsxfun to subtract the column mean from the corresponding column elements of a matrix, A.

```A = [1 2 10; 1 4 20;1 6 15] ;
C = bsxfun(@minus, A, mean(A))
```
```C =

0    -2    -5
0     0     5
0     2     0```

### Singleton Expansion with Custom Function

Call a custom-defined binary function with bsxfun by specifying a handle to the function.

```fun = @(A,B) A.*sin(B);
A = 1:7;
B = pi*[0 1/4 1/3 1/2 2/3 3/4 1].';
C = bsxfun(fun,A,B)```
```C =

0         0         0         0         0         0         0
0.7071    1.4142    2.1213    2.8284    3.5355    4.2426    4.9497
0.8660    1.7321    2.5981    3.4641    4.3301    5.1962    6.0622
1.0000    2.0000    3.0000    4.0000    5.0000    6.0000    7.0000
0.8660    1.7321    2.5981    3.4641    4.3301    5.1962    6.0622
0.7071    1.4142    2.1213    2.8284    3.5355    4.2426    4.9497
0.0000    0.0000    0.0000    0.0000    0.0000    0.0000    0.0000```

Singleton expansion allows bsxfun to expand the input vectors into a full matrix.