bsxfun
Apply element-wise operation to two arrays with implicit expansion enabled
Syntax
Description
Examples
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 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 = 4×3 logical array
1 0 0
1 1 0
1 1 0
1 1 1
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
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
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
bsxfunwith 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
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.
Usage notes and limitations:
Code generation does not support sparse matrix inputs for this function.
Usage notes and limitations:
Code generation does not support sparse matrix inputs for this function.
This function fully supports thread-based environments. For more information, see Run MATLAB Functions in Thread-Based Environment.
The bsxfun function
supports GPU array input with these usage notes and limitations:
See
bsxfun(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
MATLAB Command
You clicked a link that corresponds to this MATLAB command:
Run the command by entering it in the MATLAB Command Window. Web browsers do not support MATLAB commands.
Seleziona un sito web
Seleziona un sito web per visualizzare contenuto tradotto dove disponibile e vedere eventi e offerte locali. In base alla tua area geografica, ti consigliamo di selezionare: .
Puoi anche selezionare un sito web dal seguente elenco:
Come ottenere le migliori prestazioni del sito
Per ottenere le migliori prestazioni del sito, seleziona il sito cinese (in cinese o in inglese). I siti MathWorks per gli altri paesi non sono ottimizzati per essere visitati dalla tua area geografica.
Americhe
- América Latina (Español)
- Canada (English)
- United States (English)
Europa
- 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)