bsxfun

Applies a binary function with implicit expansion of singleton dimensions.

Category: Language Fundamentals > Operators and Elementary Operations

Syntax

C = bsxfun(fun, A, B)

Applies fun to A and B after stretching size-1 dimensions to match.

Description

Before R2016b a row and a column could not be combined with +, so bsxfun did the stretching. Now the arithmetic operators expand implicitly and bsxfun(@plus, A, B) is the same as A + B.

It remains useful for functions that have no operator form, such as @max, @min, @atan2 or @hypot, and in code that must run in older releases.

Input arguments

fun
Function handle of a binary element-wise function, such as @plus, @minus, @times, @max or @(a,b) a.^b.
A, B
Arrays whose sizes are equal or 1 in each dimension.

Output arguments

C
Result of the function, with the expanded size.

Example

bsxfun(@plus, [1; 2], [10 20])
bsxfun(@max, [1 5], [3; 4])
Output
ans = 2×2

    11    21
    12    22
ans = 2×2

     3     5
     4     5

See also

arrayfun repmat + max