Matlab ≥ R2016b array broadcasts like Python

1 minute read

Array broadcasting (no bsxfun() needed) is in

Array broadcasting is important as it minimizes copying of large arrays in memory, or excessive looping over arrays, which can be very inefficient. There are certain cases, particularly with JIT compilers, where counter-intuitively for loops can be faster than array broadcasting. In my opinion, usually the clean, clear code enabled by array broadcasting outweighs those edge-case possible gains.

bsxfun() is no longer needed for:

  • plus
  • minus
  • times
  • rdivide
  • ldivide
  • power

and many more operations.

Array broadcasting example

If A has size 4x5 and B has shape 1x5, A.*B, A+B etc. just work without bsxfun(), provided the N-D array dimension multiplied is of matching shape.

This is important for clarity and conciseness of syntax. bsxfun() is a factor that drove me to using Python over Matlab almost entirely.

Fix errors


error: operator *: nonconformant arguments (op1 is 5x4, op2 is 1x5)


Remember to always use .' to transpose because ' means take the Hermetian (complex conjugate) transpose. Remember that Matlab does memory copies (expensive for large arrays) on transpose. Python transposes are “free” O(1) since they’re just a view into the original array (pointer tricks).

Minimum versions

Programs earlier than these versions did require bsxfun().

Program minimum version date
Matlab R2016b Sept. 2016
Octave 3.6.0 Feb. 2012
Python/Numpy   < 2006

Fortran array broadcasting

Fortran 95 brought the spread() function, which allows efficient array broadcasting.

Leave a Comment