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.

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.

Create a function handle that represents the function .

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)

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.

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