MATLAB®is optimized for operations involving matrices and vectors. The process of revising loop-based, scalar-oriented code to use MATLAB matrix and vector operations is calledvectorization. Vectorizing your code is worthwhile for several reasons:
Appearance: Vectorized mathematical code appears more like the mathematical expressions found in textbooks, making the code easier to understand.
Less Error Prone: Without loops, vectorized code is often shorter. Fewer lines of code mean fewer opportunities to introduce programming errors.
Performance: Vectorized code often runs much faster than the corresponding code containing loops.
This code computes the sine of 1,001 values ranging from 0 to 10:
i = 0;fort = 0:.01:10 i = i + 1; y(i) = sin(t);end
This is a vectorized version of the same code:
t = 0:.01:10; y = sin(t);
The second code sample usually executes faster than the first and is a more efficient use of MATLAB. Test execution speed on your system by creating scripts that contain the code shown, and then use thetic
andtoc
functions to measure their execution time.
This code computes the cumulative sum of a vector at every fifth element:
x = 1:10000; ylength = (length(x) - mod(length(x),5))/5; y(1:ylength) = 0;forn= 5:5:length(x) y(n/5) = sum(x(1:n));end
Using vectorization, you can write a much more concise MATLAB process. This code shows one way to accomplish the task:
x = 1:10000; xsums = cumsum(x); y = xsums(5:5:length(x));
Array operators perform the same operation for all elements in the data set. These types of operations are useful for repetitive calculations. For example, suppose you collect the volume (V
) of various cones by recording their diameter (D
) and height (H
). If you collect the information for just one cone, you can calculate the volume for that single cone:
V = 1/12*pi*(D^2)*H;
Now, collect information on 10,000 cones. The vectorsD
andH
each contain 10,000 elements, and you want to calculate 10,000 volumes. In most programming languages, you need to set up a loop similar to this MATLAB code:
forn = 1:10000 V(n) = 1/12*pi*(D(n)^2)*H(n);end
With MATLAB, you can perform the calculation for each element of a vector with similar syntax as the scalar case:
% Vectorized CalculationV = 1/12*pi*(D.^2).*H;
Note
Placing a period (.
) before the operators*
,/
, and^
, transforms them into array operators.
Array operators also enable you to combine matrices of different dimensions. This automatic expansion of size-1 dimensions is useful for vectorizing grid creation, matrix and vector operations, and more.
假设矩阵A
represents test scores, the rows of which denote different classes. You want to calculate the difference between the average score and individual scores for each class. Using a loop, the operation looks like:
A = [97 89 84; 95 82 92; 64 80 99;76 77 67;...88 59 74; 78 66 87; 55 93 85]; mA = mean(A); B = zeros(size(A));forn = 1:size(A,2) B(:,n) = A(:,n) - mA(n);end
A more direct way to do this is withA - mean(A)
, which avoids the need of a loop and is significantly faster.
devA = A - mean(A)
devA = 18 11 0 16 4 8 -15 2 15 -3 -1 -17 9 -19 -10 -1 -12 3 -24 15 1
Even thoughA
is a 7-by-3 matrix andmean(A)
is a 1-by-3 vector, MATLAB implicitly expands the vector as if it had the same size as the matrix, and the operation executes as a normal element-wise minus operation.
The size requirement for the operands is that for each dimension, the arrays must either have the same size or one of them is 1. If this requirement is met, then dimensions where one of the arrays has size 1 are expanded to be the same size as the corresponding dimension in the other array. For more information, seeCompatible Array Sizes for Basic Operations.
Another area where implicit expansion is useful for vectorization is if you are working with multidimensional data. Suppose you want to evaluate a function,F
, of two variables,x
andy
.
F(x,y) = x*exp(-x2- y2)
To evaluate this function at every combination of points in thex
andy
向量,您需要定义一个网格的值。为this task you should avoid using loops to iterate through the point combinations. Instead, if one of the vectors is a column and the other is a row, then MATLAB automatically constructs the grid when the vectors are used with an array operator, such asx+y
orx-y
. In this example,x
is a 21-by-1 vector andy
是一个1-by-16向量,所以operation produces a 21-by-16 matrix by expanding the second dimension ofx
and the first dimension ofy
.
x = (-2:0.2:2)';% 21-by-1y = -1.5:0.2:1.5;% 1-by-16F = x.*exp(-x.^2-y.^2);% 21-by-16
In cases where you want to explicitly create the grids, you can use themeshgrid
andndgrid
functions.
arra批量处理的逻辑延伸ys is to vectorize comparisons and decision making. MATLAB comparison operators accept vector inputs and return vector outputs.
为example, suppose while collecting data from 10,000 cones, you record several negative values for the diameter. You can determine which values in a vector are valid with the>=
operator:
D = [-0.2 1.0 1.5 3.0 -1.0 4.2 3.14]; D >= 0
ans = 0 1 1 1 0 1 1
Vgood
, for which the corresponding elements ofD
are nonnegative:Vgood = V(D >= 0);
MATLAB allows you to perform a logical AND or OR on the elements of an entire vector with the functionsall
andany
, respectively. You can throw a warning if all values ofD
are below zero:
ifall(D < 0) warning('All values of diameter are negative.')returnend
MATLAB can also compare two vectors with compatible sizes, allowing you to impose further restrictions. This code finds all the values where V is nonnegative andD
is greater thanH
:
V((V >= 0) & (D > H))
To aid comparison, MATLAB contains special values to denote overflow, underflow, and undefined operators, such asInf
andNaN
. Logical operatorsisinf
andisnan
exist to help perform logical tests for these special values. For example, it is often useful to excludeNaN
values from computations:
x = [2 -1 0 3 NaN 2 NaN 11 4 Inf]; xvalid = x(~isnan(x))
xvalid = 2 -1 0 3 2 11 4 Inf
Note
Inf == Inf
returns true; however,NaN == NaN
always returns false.
When vectorizing code, you often need to construct a matrix with a particular size or structure. Techniques exist for creating uniform matrices. For instance, you might need a 5-by-5 matrix of equal elements:
A = ones(5,5)*10;
v = 1:5; A = repmat(v,3,1)
A = 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5
The functionrepmat
possesses flexibility in building matrices from smaller matrices or vectors.repmat
creates matrices by repeating an input matrix:
A = repmat(1:3,5,2) B = repmat([1 2; 3 4],2,2)
A = 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 B = 1 2 1 2 3 4 3 4 1 2 1 2 3 4 3 4
In many applications, calculations done on an element of a vector depend on other elements in the same vector. For example, a vector,x, might represent a set. How to iterate through a set without afor
orwhile
loop is not obvious. The process becomes much clearer and the syntax less cumbersome when you use vectorized code.
A number of different ways exist for finding the redundant elements of a vector. One way involves the functiondiff
. After sorting the vector elements, equal adjacent elements produce a zero entry when you use thediff
function on that vector. Becausediff(x)
produces a vector that has one fewer element thanx
, you must add an element that is not equal to any other element in the set.NaN
always satisfies this condition. Finally, you can use logical indexing to choose the unique elements in the set:
x = [2 1 2 2 3 1 3 2 1 3]; x = sort(x); difference = diff([x,NaN]); y = x(difference~=0)
y = 1 2 3
unique
function:y=unique(x);
unique
function might provide more functionality than is needed and slow down the execution of your code. Use thetic
andtoc
functions if you want to measure the performance of each code snippet.
Rather than merely returning the set, or subset, ofx
, you can count the occurrences of an element in a vector. After the vector sorts, you can use thefind
function to determine the indices of zero values indiff(x)
and to show where the elements change value. The difference between subsequent indices from thefind
function indicates the number of occurrences for a particular element:
x = [2 1 2 2 3 1 3 2 1 3]; x = sort(x); difference = diff([x,max(x)+1]); count = diff(find([1,difference])) y = x(find(difference))
count = 3 4 3 y = 1 2 3
find
function does not return indices forNaN
elements. You can count the number ofNaN
andInf
values using theisnan
andisinf
functions.
count_nans = sum(isnan(x(:))); count_infs = sum(isinf(x(:)));
Function | Description |
---|---|
all |
Determine if all array elements are nonzero or true |
any |
Determine if any array elements are nonzero |
cumsum |
Cumulative sum |
diff |
Differences and Approximate Derivatives |
find |
Find indices and values of nonzero elements |
ind2sub |
Subscripts from linear index |
ipermute |
Inverse permute dimensions of N-D array |
logical |
Convert numeric values to logicals |
meshgrid |
Rectangular grid in 2-D and 3-D space |
ndgrid |
Rectangular grid in N-D space |
permute |
Rearrange dimensions of N-D array |
prod |
Product of array elements |
repmat |
Repeat copies of array |
reshape |
Reshape array |
shiftdim |
Shift dimensions |
sort |
Sort array elements |
squeeze |
Remove singleton dimensions |
sub2ind |
Convert subscripts to linear indices |
sum |
Sum of array elements |