When using Matlab for matrix calculations, you will often encounter situations where you want to use a for loop. But in fact a lot of operations can be replaced with some internal functions. Bsxfun, Arrayfun, Cellfun, Spfun, Structfunbsxfun:
Bsxfun can manipulate the fun function of the corresponding elements of matrix A and matrix B. Among them, fun is a function of two-dollar operation of any scalar input and output, such as basic subtraction, trigonometric functions, size comparisons, and any other custom functions that meet the criteria.
Note that fun cannot be symbols, such as +,*, which have corresponding function names. For example + corresponds to Plus, >= corresponds to GE, and so on. can be entered via MATLAB command line
Help < operations symbols >
To query.
In general, if the two matrices are the same size, we can do it directly through A+b, but Bsxfun has one advantage, that is, when any one-dimensional length of a, B is 1, the function will automatically compute the dimension and each row (column) of the dimension corresponding to the other matrix. If we do this ourselves, we either use loops or use Repmat to extend the matrix, which is much slower than Bsxfun's internal implementation, or consumes more memory.
The Netizen provides an example: suppose we have data A and B, each row is a sample, each column is a feature. We want to calculate the Gaussian core, both:
K (| | x-xc| |) =exp{-| | x-xc| | ^2/(2*σ) ^2)} Where XC is the center of the kernel function, σ is the width parameter of the function, which controls the radial scope of the function.
You can, of course, use a dual for implementation:
1 2 3 4 5 6 |
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 |
Use A and B of the 2,000x1,000 size to run for 88 seconds.
Consider a quantifiable version of the following:
1 2 3 |
SA = (SUM (a.^2, 2)); SB = (SUM (b.^2, 2)); K2 = exp (Bsxfun (@minus, Bsxfun (@minus, 2*a*b ', SA), SB ')/beta); |
Using the same data, the run time is only 0.85 seconds and accelerates more than 100 times times.
Arrayfun:
1 2 |
[B1,..., Bm] = Arrayfun (func,a1,..., an) [B1,..., Bm] = Arrayfun (func,a1,..., an,name,value) |
This function can perform the Func function operation directly on the elements in the array. wherein, the Func function accepts n inputs, m outputs. When the output can be merged, you can set ' Uniformoutput ' to true, so that all A1: The first m output of the Func will be merged into an array Bm, if ' uniformoutput ' is false, indicating that the output corresponding to different input elements cannot be merged, so that each Bm will be a cell.
There is a good example of MATLAB help, which is not posted here.
Cellfun:
1 2 |
[A1,..., Am] = Cellfun (func,c1,..., Cn) [A1,..., Am] = Cellfun (func,c1,..., cn,name,value) |
Similar to the usage of arrayfun, it is simply an operation on the corresponding element of the cell.
Structfun:
1 2 |
[A1,..., an] = Structfun (func,s) [A1,..., an] = Structfun (func,s,name,value) |
In a similar usage, the Func operation is performed on all domains of the struct S.
Spfun:
This function can perform a fun operation on each valued element of a sparse matrix S.
The purpose of this function is not only to increase speed, but also to keep the returned F, where no data is still 0. For example:
1 2 |
S = Spdiags ([1:4] ', 0,4,4) f = Spfun (@exp, S) |
S =
(1)
(2,2) 2
(3,3) 3
(BIS) 4
f =
(2.7183)
(2,2) 7.3891
(3,3) 20.0855
(bis) 54.5982
and run directly
, the place with no data becomes 1.
Ans =
2.7183 1.0000 1.0000 1.0000
1.0000 7.3891 1.0000 1.0000
1.0000 1.0000 20.0855 1.0000
1.0000 1.0000 1.0000 54.5982
Using built-in functions instead of for loops in MATLAB