Documentation

This is machine translation

Translated by Microsoft
Mouse over text to see original. Click the button below to return to the English verison of the page.

bsxfun

Apply element-wise operation to two arrays with implicit expansion enabled

Syntax

Description

example

C = bsxfun(fun,A,B) applies the element-wise binary operation specified by the function handle fun to arrays A and B.

Examples

collapse all

Subtract the column mean from the corresponding column elements of a matrix A. Then normalize by the standard deviation.

A = [1 2 10; 3 4 20; 9 6 15];
C = bsxfun(@minus, A, mean(A));
D = bsxfun(@rdivide, C, std(A))
D =

   -0.8006   -1.0000   -1.0000
   -0.3203         0    1.0000
    1.1209    1.0000         0

In MATLAB® R2016b and later, you can directly use operators instead of bsxfun, since the operators independently support implicit expansion of arrays with compatible sizes.

(A - mean(A))./std(A)
ans =

   -0.8006   -1.0000   -1.0000
   -0.3203         0    1.0000
    1.1209    1.0000         0

Compare the elements in a column vector and a row vector. The result is a matrix containing the comparison of each combination of elements from the vectors. An equivalent way to execute this operation is with A > B.

A = [8; 17; 20; 24]
A =

     8
    17
    20
    24

B = [0 10 21]
B =

     0    10    21

C = bsxfun(@gt,A,B)
C =

  4×3 logical array

   1   0   0
   1   1   0
   1   1   0
   1   1   1

Create a function handle that represents the function $f(a,b) = a - e^b$.

fun = @(a,b) a - exp(b);

Use bsxfun to apply the function to vectors a and b. The bsxfun function expands the vectors into matrices of the same size, which is an efficient way to evaluate fun for many combinations of the inputs.

a = 1:7;
b = pi*[0 1/4 1/3 1/2 2/3 3/4 1].';
C = bsxfun(fun,a,b)
C =

         0    1.0000    2.0000    3.0000    4.0000    5.0000    6.0000
   -1.1933   -0.1933    0.8067    1.8067    2.8067    3.8067    4.8067
   -1.8497   -0.8497    0.1503    1.1503    2.1503    3.1503    4.1503
   -3.8105   -2.8105   -1.8105   -0.8105    0.1895    1.1895    2.1895
   -7.1205   -6.1205   -5.1205   -4.1205   -3.1205   -2.1205   -1.1205
   -9.5507   -8.5507   -7.5507   -6.5507   -5.5507   -4.5507   -3.5507
  -22.1407  -21.1407  -20.1407  -19.1407  -18.1407  -17.1407  -16.1407

Input Arguments

collapse all

Binary function to apply, specified as a function handle. fun must be a binary (two-input) element-wise function of the form C = fun(A,B) that accepts arrays A and B with compatible sizes. For more information, see Compatible Array Sizes for Basic Operations. fun must support scalar expansion, such that if A or B is a scalar, then C is the result of applying the scalar to every element in the other input array.

In MATLAB® R2016b and later, the built-in binary functions listed in this table independently support implicit expansion. With these functions, you can call the function or operator directly instead of using bsxfun. For example, you can replace C = bsxfun(@plus,A,B) with A+B.

FunctionSymbolDescription
plus

+

Plus

minus

-

Minus

times

.*

Array multiply

rdivide

./

Right array divide

ldivide

.\

Left array divide

power

.^

Array power

eq

==

Equal

ne

~=

Not equal

gt

>

Greater than

ge

>=

Greater than or equal to

lt

<

Less than

le

<=

Less than or equal to

and

&

Element-wise logical AND

or

|

Element-wise logical OR

xor

N/A

Logical exclusive OR

max

N/A

Binary maximum

min

N/A

Binary minimum

mod

N/A

Modulus after division

rem

N/A

Remainder after division

atan2

N/A

Four-quadrant inverse tangent; result in radians

atan2d

N/A

Four-quadrant inverse tangent; result in degrees

hypot

N/A

Square root of sum of squares

Example: C = bsxfun(@plus,[1 2],[2; 3])

Data Types: function_handle

Input arrays, specified as scalars, vectors, matrices, or multidimensional arrays. Inputs A and B must have compatible sizes. For more information, see Compatible Array Sizes for Basic Operations. Whenever a dimension of A or B is singleton (equal to one), bsxfun virtually replicates the array along that dimension to match the other array. In the case where a dimension of A or B is singleton, and the corresponding dimension in the other array is zero, bsxfun virtually diminishes the singleton dimension to zero.

Data Types: single | double | uint8 | uint16 | uint32 | uint64 | int8 | int16 | int32 | int64 | char | logical
Complex Number Support: Yes

See Also

|

Introduced in R2007a

Was this topic helpful?