Recently always encountered Bsxfun This function, the previous several times because it is not important just to look at the function body to compare the results, today once again met this function, think it is necessary to master, then looked up some information summarized as follows.
function Bsxfun
Function description Two elements of an array are computed one after the other.
"Application" when we want to perform certain operations on each column of a matrix A or on each row of the same length vector a (compare size, multiplication, etc.), we can only use the loop method or the vector a that the Repmat function will operate to copy the matrix of the same size sing Woo A, and then operate. From the beginning of Matlab r2007a, when encountering similar problems, we have a concise and efficient method, that is, the use of Bsxfun function.
"Function description" C=bsxfun (fun,a,b): Two elements of the array are calculated one by two, fun is a function handle or M file, can also be the following built-in function:
@plus Plus, @minus minus, @times array multiplication, @rdivide left, @ldivide right except.
Example1: enlarges each row or column element of a matrix by a different multiple
such as [1 2 3;4 5 6; 7 8 9], the first column element is multiplied by 1, the second column element is 2, and the third column element is multiplied by 4. Using the Bsxfun function, you can give the following code:
[plain] view plain Copy a = [1,2,3;4,5,6;7,8,9]; Acol = Bsxfun (@times, a,[1 2 4])
Example2: Suppose we have a list of vectors and a line of vectors.
[Plain]View plain Copy a = RANDN (3,1); b = Randn (1,3); A = 0.7516-0.8637-1.0709 B =-0.3552 0.9559 0.1593
We can easily use the external multiplication of matlab to get c=a*b,
[Plain]View plain Copy c = A * b c =-0.2670 0.7184 0.1197 0.3068-0.8256-0.1376 0.3804-1.0236 -0.1706
But what if we want to use "plus"? In other words, the multiplication sign is replaced by the plus sign in the process of solving the equation. At this point we can use C=bsxfun (@plus, A, b) to achieve.
Bsxfun's execution is this: if A and b are the same size, then c=a+b; But if there is a dimension that is different, and a or B must have a dimension of 1 in this dimension, then Bsxfun will have less of this virtual copy to make the same number of dimensions as many. In our case, the first dimension of B is only 1 (one row), so bsxfun copies B 3 times to form a 3x3 matrix, and also copies a into a 3x3 matrix. This is equivalent to C=repmat (a,1,3) +repmat (b,3,1). Over here
[Plain]View Plain copy 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 an explicit copy, which of course brings memory consumption. And Bsxfun is a virtual copy, actually implemented by for, which is equivalent to:
[Plain]View plain copy for (I=1:3) for (J=1:3) C (i,j) =a (i) +b (j); End End
However, Bsxfun will not have additional time for the use of MATLAB. The actual verification of these three ways:
[Plain]View plain copy >> C = Bsxfun (@plus, b) c = 0.3609 0.3202 0.6604 0.3296 0.2889 0.6291 0.4149 0.3742 0.7144
[Plain]View plain copy >> 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
[Plain]View Plain copy >> for (I=1:3)