• Uncategorised

bsxfun

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

C = bsxfun(fun,A,B)

Description

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

@plus Plus

@minus Minus

@times Array multiply

@rdivide Right array divide

@ldivide Left array divide

@power Array power

@max Binary maximum

@min Binary minimum

@rem Remainder after division

@mod Modulus after division

@atan2 Four quadrant inverse tangent

@hypot Square root of sum of squares

@eq Equal

@ne Not equal

@lt Less than

@le Less than or equal to

@gt Greater than

@ge Greater than or equal to

@and Element-wise logical AND

@or Element-wise logical OR

@xor 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 of A and B must either be equal to each other, or equal to 1. Whenever a dimension of A or B is singleton (equal to 1), 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 is 0.

The size of the output array C is equal to:

max(size(A),size(B)).*(size(A)>0 & size(B)>0).

Examples

In this example, bsxfun is used to subtract the column means from the corresponding columns of matrix A.

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

You may also like...