Apply element-by-element binary operation to two arrays
with singleton expansion enabled


C = bsxfun(fun,A,B)


C = bsxfun(fun,A,B)applies
an element-by-element binary operation to arraysAandB,
with singleton expansion enabled.funis a function
handle, and can either be an M-file function or one of the following
built-in functions:






Array multiply


Right array divide


Left array divide


Array power


Binary maximum


Binary minimum


Remainder after division


Modulus after division


Four quadrant inverse tangent


Square root of sum of squares




Not equal


Less than


Less than or equal to


Greater than


Greater than or equal to


Element-wise logical AND


Element-wise logical OR


Logical exclusive OR

If an M-file function is specified, it must be able to accept
either two column vectors of the same size, or one column vector and
one scalar, and return as output a column vector of the size as the
input values.

Each dimension ofAandBmust
either be equal to each other, or equal to1. Whenever
a dimension ofAorBis singleton
(equal to1), the array is virtually replicated
along the dimension to match the other array. The array may be diminished
if the corresponding dimension of the other array is0.

The size of the output arrayCis equal to:
max(size(A),size(B)).*(size(A)>0 & size(B)>0).


In this example,bsxfunis used to subtract
the column means from the corresponding columns of matrixA.

A = magic(5);
A = bsxfun(@minus, A, mean(A))
A =

     4    11   -12    -5     2
    10    -8    -6     1     3
    -9    -7     0     7     9
    -3    -1     6     8   -10
    -2     5    12   -11    -4

标签: none