Skip to content

bsxfun

Apply a binary function with broadcasting.

C = bsxfun(fun, A, B)

Applies the binary function fun to corresponding elements of A and B, broadcasting along singleton dimensions. fun must be a function handle of the form @(x, y) ... (or a built-in name like @plus, @times).

MathJet’s +, -, .*, etc. operators already broadcast natively, so bsxfun(@plus, A, B) is equivalent to A + B. Provided primarily for compatibility with MATLAB code that predates implicit broadcasting.

A = [1 2 3];
B = [10; 20; 30];
bsxfun(@plus, A, B) % 3×3: [11 12 13; 21 22 23; 31 32 33]
bsxfun(@times, A, B) % 3×3 elementwise broadcast multiply
  • mtimes — Matrix multiplication (A * B).