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:
and many more operations.
Array broadcasting example
If A has size 4x5 and B has shape 1x5,
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.
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).
Programs earlier than these versions did require
Fortran array broadcasting
Fortran 95 brought the
spread() function, which allows efficient array broadcasting.