来自matlab 2011b的帮助文件,有时间会翻译一下。
Vectorizing Loops
The MATLAB software uses a matrix language, which means it is designed for vector and matrix operations. You can often speed up your code by using vectorizing algorithms that take advantage of this design. Vectorization means converting for and while loops to equivalent vector or matrix operations.
Simple Example of Vectorizing
Here is one way to compute the sine of 1001 values ranging from 0 to 10:
i = 0;
for t = 0:.01:10
i = i + 1;
y(i) = sin(t);
end
A vectorized version of the same code is
t = 0:.01:10;
y = sin(t);
The second example executes much faster than the first and is the way MATLAB is meant to be used. Test this on your system by creating scripts that contain the code shown, and then using the tic and toc functions to measure the performance.
Advanced Example of Vectorizing
repmat is an example of a function that takes advantage of vectorization. It accepts three input arguments: an array A, a row dimension M, and a column dimension N.
repmat creates an output array that contains the elements of array A, replicated and "tiled" in an M-by-N arrangement:
A = [1 2 3; 4 5 6];
B = repmat(A,2,3);
B =
1 2 3 1 2 3 1 2 3
4 5 6 4 5 6 4 5 6
1 2 3 1 2 3 1 2 3
4 5 6 4 5 6 4 5 6
repmat uses vectorization to create the indices that place elements in the output array:
function B = repmat(A, M, N)
% Step 1 Get row and column sizes
[m,n] = size(A);
% Step 2 Generate vectors of indices from 1 to row/column size
mind = (1:m)';
nind = (1:n)';
% Step 3 Create index matrices from vectors above
mind = mind(:,ones(1, M));
nind = nind(:,ones(1, N));
% Step 4 Create output array
B = A(mind,nind);
Step 1, above, obtains the row and column sizes of the input array.
Step 2 creates two column vectors. mind contains the integers from 1 through the row size of A. The nind variable contains the integers from 1 through the column size of A.
Step 3 uses a MATLAB vectorization trick to replicate a single column of data through any number of columns. The code is
B = A(:,ones(1,nCols))
where nCols is the desired number of columns in the resulting matrix.
Step 4 uses array indexing to create the output array. Each element of the row index array, mind, is paired with each element of the column index array, nind, using the following procedure:
The first element of mind, the row index, is paired with each element of nind. MATLAB moves through the nind matrix in a columnwise fashion, so mind(1,1) goes with nind(1,1), and then nind(2,1), and so on. The result fills the first row of the output array.
Moving columnwise through mind, each element is paired with the elements of nind as above. Each complete pass through the nind matrix fills one row of the output array.
Caution While repmat can take advantage of vectorization, it can do so at the expense of memory usage. When this is the case, you might find the bsxfun function be more appropriate in this respect.
Functions Used in Vectorizing
Function:Description
all:Test to determine if all elements are nonzero
any:Test for any nonzeros
cumsum:Find cumulative sum
diff:Find differences and approximate derivatives
find:Find indices and values of nonzero elements
ind2sub:Convert from linear index to subscripts
ipermute:Inverse permute dimensions of a multidimensional array
logical:Convert numeric values to logical
meshgrid:Generate X and Y arrays for 3-D plots
ndgrid:Generate arrays for multidimensional functions and interpolation
permute:Rearrange dimensions of a multidimensional array
prod:Find product of array elements
repmat:Replicate and tile an array
reshape:Change the shape of an array
shiftdim:Shift array dimensions
sort:Sort array elements in ascending or descending order
squeeze:Remove singleton dimensions from an array
sub2ind:Convert from subscripts to linear index
sum:Find the sum of array elements
Vectorizing Loops
The MATLAB software uses a matrix language, which means it is designed for vector and matrix operations. You can often speed up your code by using vectorizing algorithms that take advantage of this design. Vectorization means converting for and while loops to equivalent vector or matrix operations.
Simple Example of Vectorizing
Here is one way to compute the sine of 1001 values ranging from 0 to 10:
i = 0;
for t = 0:.01:10
i = i + 1;
y(i) = sin(t);
end
A vectorized version of the same code is
t = 0:.01:10;
y = sin(t);
The second example executes much faster than the first and is the way MATLAB is meant to be used. Test this on your system by creating scripts that contain the code shown, and then using the tic and toc functions to measure the performance.
Advanced Example of Vectorizing
repmat is an example of a function that takes advantage of vectorization. It accepts three input arguments: an array A, a row dimension M, and a column dimension N.
repmat creates an output array that contains the elements of array A, replicated and "tiled" in an M-by-N arrangement:
A = [1 2 3; 4 5 6];
B = repmat(A,2,3);
B =
1 2 3 1 2 3 1 2 3
4 5 6 4 5 6 4 5 6
1 2 3 1 2 3 1 2 3
4 5 6 4 5 6 4 5 6
repmat uses vectorization to create the indices that place elements in the output array:
function B = repmat(A, M, N)
% Step 1 Get row and column sizes
[m,n] = size(A);
% Step 2 Generate vectors of indices from 1 to row/column size
mind = (1:m)';
nind = (1:n)';
% Step 3 Create index matrices from vectors above
mind = mind(:,ones(1, M));
nind = nind(:,ones(1, N));
% Step 4 Create output array
B = A(mind,nind);
Step 1, above, obtains the row and column sizes of the input array.
Step 2 creates two column vectors. mind contains the integers from 1 through the row size of A. The nind variable contains the integers from 1 through the column size of A.
Step 3 uses a MATLAB vectorization trick to replicate a single column of data through any number of columns. The code is
B = A(:,ones(1,nCols))
where nCols is the desired number of columns in the resulting matrix.
Step 4 uses array indexing to create the output array. Each element of the row index array, mind, is paired with each element of the column index array, nind, using the following procedure:
The first element of mind, the row index, is paired with each element of nind. MATLAB moves through the nind matrix in a columnwise fashion, so mind(1,1) goes with nind(1,1), and then nind(2,1), and so on. The result fills the first row of the output array.
Moving columnwise through mind, each element is paired with the elements of nind as above. Each complete pass through the nind matrix fills one row of the output array.
Caution While repmat can take advantage of vectorization, it can do so at the expense of memory usage. When this is the case, you might find the bsxfun function be more appropriate in this respect.
Functions Used in Vectorizing
Function:Description
all:Test to determine if all elements are nonzero
any:Test for any nonzeros
cumsum:Find cumulative sum
diff:Find differences and approximate derivatives
find:Find indices and values of nonzero elements
ind2sub:Convert from linear index to subscripts
ipermute:Inverse permute dimensions of a multidimensional array
logical:Convert numeric values to logical
meshgrid:Generate X and Y arrays for 3-D plots
ndgrid:Generate arrays for multidimensional functions and interpolation
permute:Rearrange dimensions of a multidimensional array
prod:Find product of array elements
repmat:Replicate and tile an array
reshape:Change the shape of an array
shiftdim:Shift array dimensions
sort:Sort array elements in ascending or descending order
squeeze:Remove singleton dimensions from an array
sub2ind:Convert from subscripts to linear index
sum:Find the sum of array elements