主要内容

bsxfun

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

描述

example

C =bsxfun(乐趣,a,b)应用函数句柄指定的元素二进制操作乐趣to arraysAB

例子

全部收缩

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 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 f ( a , b ) = a - e b

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

利用bsxfun将功能应用于向量ab。这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, specified as a function handle.乐趣必须是表单的二进制(两输入)元素函数C =乐趣(A,B)that accepts arraysABwith compatible sizes. For more information, see兼容阵列大小用于基本操作乐趣must support scalar expansion, such that ifA或者B是标量Cis 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

Input arrays, specified as scalars, vectors, matrices, or multidimensional arrays. InputsABmust have compatible sizes. For more information, see兼容阵列大小用于基本操作。Whenever a dimension ofA或者Bis singleton (equal to one),bsxfunvirtually replicates the array along that dimension to match the other array. In the case where a dimension ofA或者Bis singleton, and the corresponding dimension in the other array is zero,bsxfunvirtually 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 ofbsxfunwith 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

Version History

Introduced in R2007a