NotePlacing 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.
Suppose that matrix 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));
for n = 1:size(A,2)
B(:,n) = A(:,n) - mA(n);
end
A more direct way to do this is with A - 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 though A is a 7-by-3 matrix and mean(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
Vectorization