Matlab array broadcasts like Python
Array broadcasting (no bsxfun()
needed) is in
Matlab
and
GNU Octave
Array broadcasting can minimize copying of large arrays in memory or excessive looping over arrays, both of which can be very inefficient. However, there are certain cases particularly with JIT compilers where looping can be faster than array broadcasting. Often the clean, clear code enabled by array broadcasting outweighs those edge-case possible gains. As always, make a simple benchmark if you’re concerned about a particular case (broadcast vs. loop) and for Python try Numba.
bsxfun is no longer needed for:
- plus
- minus
- times
- rdivide
- ldivide
- power
and many more operations.
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.
A .* B
can result in
error: operator *: nonconformant arguments (op1 is 5x4, op2 is 1x5)
Assuming A and B are compatibly shaped when transposed, try:
A .* B.'
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).
Programs earlier than these versions did require bsxfun()
.
Program | minimum version |
---|---|
Matlab | R2016b |
Octave | 3.6.0 |
Fortran 95 brought the
spread()
function,
which allows efficient array broadcasting, at the price of replicating the array in memory.