bsxfun
Apply element-wise operation to two arrays with implicit expansion enabled
Syntax
描述
例子
矩阵元素与列平均值的偏差
Subtract the column mean from the corresponding column elements of a matrixA
。然后通过标准偏差正常化。
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
在Matlab® R2016b and later, you can directly use operators instead ofbsxfun
,由于操作员独立支持具有兼容大小的阵列的隐式扩展。万博1manbetx
(a-平均(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 witha> b
。
A = [8; 17; 20; 24]
A =4×18 17 20 24
b = [0 10 21]
b =1×30 10 21
C =bsxfun(@gt,A,B)
C =4x3逻辑阵列1 0 0 1 1 0 1 1 0 1 1 1 1
使用自定义功能扩展
Create a function handle that represents the function 。
fun = @(a,b)a -exp(b);
利用bsxfun
将功能应用于向量a
和b
。这bsxfun
乐趣ction expands the vectors into matrices of the same size, which is an efficient way to evaluate乐趣
对于输入的许多组合。
a = 1:7;b = pi*[0 1/4 1/3 1/2 2/3 3/4 1]。c = bsxfun(娱乐,a,b)
C =7×70 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 -2.1407 -22.1407 -21.1407 -20.1407 -19.1407 -1407 -18.1407 -17.17.14.14.1407 -16.1407 -16.1407-16.1407-14.14.1407-14.1407 -16.1407
Input Arguments
乐趣
—Binary function to apply
乐趣ction handle
Binary function to apply, specified as a function handle.乐趣
必须是表单的二进制(两输入)元素函数C =乐趣(A,B)
that accepts arraysA
和B
with compatible sizes. For more information, see兼容阵列大小用于基本操作。乐趣
must support scalar expansion, such that ifA
或者B
是标量C
is the result of applying the scalar to every element in the other input array.
在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 usingbsxfun
。例如,您可以替换C =bsxfun(@plus,A,B)
witha+b
。
功能 | 象征 | 描述 |
---|---|---|
加 |
|
加 |
minus |
|
Minus |
时代 |
|
数组乘以 |
rdivide |
|
Right array divide |
ldivide |
|
左阵列划分 |
力量 |
|
Array power |
eq |
|
平等的 |
NE |
|
不相等 |
gt |
|
Greater than |
ge |
|
Greater than or equal to |
上尉 |
|
Less than |
le |
|
Less than or equal to |
和 |
|
元素逻辑和 |
或者 |
|
Element-wise logical OR |
xor |
N/A |
Logical exclusive OR |
bitand |
N/A |
位和 |
bitor |
N/A |
Bit-wise OR |
Bitxor |
N/A |
位XOR |
最大限度 |
N/A |
Binary maximum |
min |
N/A |
二进制最小值 |
mod |
N/A |
Modulus after division |
rem |
N/A |
Remainder after division |
atan2 |
N/A |
四季度逆切线;导致弧度 |
atan2d |
N/A |
四季度逆切线;result in degrees |
hypot |
N/A |
Square root of sum of squares |
例子:C =bsxfun(@plus,[1 2],[2; 3])
Data Types:function_handle
a,b
—Input arrays
标量|向量|矩阵|多维阵列
Input arrays, specified as scalars, vectors, matrices, or multidimensional arrays. InputsA
和B
must have compatible sizes. For more information, see兼容阵列大小用于基本操作。Whenever a dimension ofA
或者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 ofA
或者B
is singleton, and the corresponding dimension in the other array is zero,bsxfun
virtually diminishes the singleton dimension to zero.
Data Types:single
|双倍的
|uint8
|UINT16
|uint32
|uint64
|int8
|INT16
|INT32
|INT64
|char
|logical
Complex Number Support:Yes
提示
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兼容阵列大小用于基本操作。
Extended Capabilities
Tall Arrays
Calculate with arrays that have more rows than fit in memory.
C/C ++代码生成
使用MATLAB®CODER™生成C和C ++代码。
用法注释和限制:
代码生成不支持此功能的稀疏矩阵输入。万博1manbetx
GPU Code Generation
使用GPU CODER™为NVIDIA®GPU生成CUDA®代码。
用法注释和限制:
代码生成不支持此功能的稀疏矩阵输入。万博1manbetx
Thread-Based Environment
Run code in the background using MATLAB®背景池
或者accelerate code with Parallel Computing Toolbox™螺纹池
。
This function fully supports thread-based environments. For more information, seeRun MATLAB Functions in Thread-Based Environment。
GPU Arrays
使用并行计算工具箱™在图形处理单元(GPU)上运行加速代码。
用法注释和限制:
See
bsxfun
(Parallel Computing Toolbox)。
Distributed Arrays
Partition large arrays across the combined memory of your cluster using Parallel Computing Toolbox™.
此功能完全支持分布式数组。万博1manbetx有关更多信息,请参阅运行具有分布式数组的MATLAB功能(Parallel Computing Toolbox)。
Version History
开放示例
You have a modified version of this example. Do you want to open this example with your edits?
matlab命令
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.
选择一个网站
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:。
您还可以从以下列表中选择一个网站:
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.
美洲
- AméricaLatina(Español)
- 加拿大(英语)
- 美国(英语)