bsxfun
Apply element-wise operation to two arrays with implicit expansion enabled
Syntax
Description
Examples
Deviation of Matrix Elements from Column Mean
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 = 3×3
-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 = 3×3
-0.8006 -1.0000 -1.0000
-0.3203 0 1.0000
1.1209 1.0000 0
Compare Vector Elements
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 = 4×1
8
17
20
24
B = [0 10 21]
B = 1×3
0 10 21
C = bsxfun(@gt,A,B)
C = 4x3 logical array
1 0 0
1 1 0
1 1 0
1 1 1
Expansion with Custom Function
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)
C = 7×7
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
fun
— Binary function to apply
function handle
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
.
Function | Symbol | Description |
---|---|---|
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 |
bitand | N/A | Bit-wise AND |
bitor | N/A | Bit-wise OR |
bitxor | N/A | Bit-wise XOR |
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
A,B
— Input arrays
scalars | vectors | matrices | multidimensional arrays
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
Tips
It is recommended that you replace most uses of
bsxfun
with direct calls to the functions and operators that support implicit expansion. Compared to usingbsxfun
, implicit expansion offers faster speed of execution, better memory usage, and improved readability of code. For more information, see Compatible Array Sizes for Basic Operations.
Extended Capabilities
Tall Arrays
Calculate with arrays that have more rows than fit in memory.
The
bsxfun
function supports tall arrays with the following usage
notes and limitations:
The specified function must not rely on persistent
variables.
For more information, see Tall Arrays.
C/C++ Code Generation
Generate C and C++ code using MATLAB® Coder™.
Usage notes and limitations:
Code generation does not support sparse matrix inputs for this function.
GPU Code Generation
Generate CUDA® code for NVIDIA® GPUs using GPU Coder™.
Usage notes and limitations:
Code generation does not support sparse matrix inputs for this function.
Thread-Based Environment
Run code in the background using MATLAB® backgroundPool
or accelerate code with Parallel Computing Toolbox™ ThreadPool
.
This function fully supports thread-based environments. For more information, see Run MATLAB Functions in Thread-Based Environment.
GPU Arrays
Accelerate code by running on a graphics processing unit (GPU) using Parallel Computing Toolbox™.
The bsxfun
function
supports GPU array input with these usage notes and limitations:
See
bsxfun
(Parallel Computing Toolbox).
Distributed Arrays
Partition large arrays across the combined memory of your cluster using Parallel Computing Toolbox™.
This function fully supports distributed arrays. For more information, see Run MATLAB Functions with Distributed Arrays (Parallel Computing Toolbox).
Version History
Introduced in R2007a
Comando de MATLAB
Ha hecho clic en un enlace que corresponde a este comando de MATLAB:
Ejecute el comando introduciéndolo en la ventana de comandos de MATLAB. Los navegadores web no admiten comandos de MATLAB.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list:
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)