MATLAB functions bsxfun

Transfer from: http: //blog.sina.com.cn/s/blog_9e67285801010ttn.html

Bsxfun little things on the Internet, today saw a need, since the original Bowen insert the picture does not show up, so I had great benevolence operation contrl + V and alt + ctrl + A, and for the exchange of learning.

 

bsxfun is a matlab function from a version R2007a to the provision, the role is "applies an element-by-element binary operation to arrays a and b, with singleton expansion enabled."

for example. Suppose we have a vector and a row vector.

a = randn(3,1), b = randn(1,3) a = -0.2453 -0.2766 -0.1913 b = 0.6062 0.5655 0.9057
We can simply use away by the matlab c=a*bto get as
But if we want to use the "plus" mean? That is the formula for solving the process for doing multiplication sign plus?
Then we can use c=bsxfun(@plus,a,b)to achieve.
bsxfun execution is such that, if a and b are the same size, then c = a + b. However, if there is a different dimension, and a or b must be a number of dimensions in the dimension of 1, then it will bsxfun this virtual copy to the less some of the multi-dimensionality of the same. In our case, only the first dimension b 1 (only one line), the replicated three times bsxfun b will form a 3 × 3 matrix, and also to copy a 3 × 3 matrix. This is equivalent to c=repmat(a,1,3)+repmat(b,3,1). Here
repmat (a, 1,3) ans = -0.2453 -0.2453 -0.2453 -0.2766 -0.2766 -0.2766 -0.1913 -0.1913 -0.1913

repmat is explicit copy, of course, brings memory consumption. The bsxfun is a virtual copy, in fact, be achieved by for, it is equivalent to for(i=1:3),for(j=1:3),c(i,j)=a(i)+b(j);end,end. But bsxfun no additional time for use of matlab brought. This verification is actually three ways

>> c = bsxfun(@plus,a,b) c = 0.3609 0.3202 0.6604 0.3296 0.2889 0.6291 0.4149 0.3742 0.7144 >> c = repmat(a,1,3)+repmat(b,3,1) c = 0.3609 0.3202 0.6604 0.3296 0.2889 0.6291 0.4149 0.3742 0.7144 >> for(i=1:3),for(j=1:3),c(i,j)=a(i)+b(j);end,end,c c = 0.3609 0.3202 0.6604 0.3296 0.2889 0.6291 0.4149 0.3742 0.7144

From the calculated time before it is implemented in two similar, far higher than for implementation. However, if the data is large, the second implementation may have memory problems. So bsxfun best. 


Here is an addition function of the number @plus handle, has a corresponding subtraction @minus, multiplication @times, etc. in addition to left and right, particularly visible doc bsxfun. 


 Let's look at a more realistic situation. Suppose we have data A and B, a sample of each row and each column is a feature. Gaussian kernel we have to calculate both: 


K (|| X ||-xc) = exp {- X-xc || || ^ 2 / (2 * [sigma]) ^ 2)} where xc is the center of the kernel function, [sigma] is function of the width parameter  , the control range of the radially acting function. 


Of course, you can use a double for realization (if first instinct is to use triple for words ...).

K1 = zeros(size(A,1),size(B,1)); for i = 1 : size(A,1) for j = 1 : size(B,1) K1(i,j) = exp(-sum((A(i,:)-B(j,:)).^2)/beta); end end
使用2,000×1,000大小的A和B, 运行时间为88秒。
考虑下面向量化后的版本:
sA = (sum(A.^2, 2)); sB = (sum(B.^2, 2)); K2 = exp(bsxfun(@minus,bsxfun(@minus,2*A*B', sA), sB')/beta);
使用同样数据,运行时间仅0.85秒,加速超过100倍。
如要判断两者结果是不是一样,可以如下
assert(all(all(abs(K1-K2)<1e-12)))
C = bsxfun(fun,A,B) appliesthe element-by-element binary operation specified by the functionhandlefun to arrays A and B,with singleton expansion enabled.fun can be oneof the following built-in functions:

@plus

Plus

@minus

Minus

@times

Array multiply

@rdivide

Right array divide

@ldivide

Left array divide

@power

Array power

@max

Binary maximum

@min

Binary minimum

@rem

Remainder after division

@mod

Modulus after division

@atan2

Four quadrant inverse tangent

@hypot

Square root of sum of squares

@eq

Equal

@ne

Not equal

@lt

Less than

@le

Less than or equal to

@gt

Greater than

@ge

Greater than or equal to

@and

Element-wise logical AND

@or

Element-wise logical OR

@xor

Logical exclusive OR

 

Guess you like

Origin blog.csdn.net/hhsh49/article/details/80594621