2
.10 ■ Introduction to M-Function Programming 47
from matrix operations. For example, A*B indicates matrix multiplication in
the traditional sense, whereas A.*Bindicates array multiplication, in the sense
that the result is an array, the same size as Aand B, in which each element is the
product of corresponding elements of Aand B. In other words, if C=A.*B, then
C(I,J)=A(I,J)*B(I,J). Because matrix and array operations are the same
for addition and subtraction, the character pairs .+and .–are not used.
When writing an expression such as B=A, MATLAB makes a “note” that
Bis equal to A, but does not actually copy the data into Bunless the contents
of A change later in the program. This is an important point because using
different variables to “store” the same information sometimes can enhance
code clarity and readability. Thus, the fact that MATLAB does not duplicate
information unless it is absolutely necessary is worth remembering when writ-
ing MATLAB code.Table 2.5 lists the MATLAB arithmetic operators, where A
and Bare matrices or arrays and aand bare scalars.All operands can be real or
complex.The dot shown in the array operators is not necessary if the operands
are scalars. Because images are 2-D arrays, which are equivalent to matrices,
all the operators in the table are applicable to images.
Throughout the book, we
use the term array
The difference between array and matrix operations is important. For oa bp le yr awt ii ot hn st hi ne t te er rc mh ai nn og le --
example, consider the following:
ogy operations between
pairs of corresponding
elements, and also
elementwise operations.
Tꢂblꢁ 2.5 Array and matrix arithmetic operators. Characters aand bare scalars.
Operator
Name
Comments and Examples
a+b, A+B, or a+A.
+
−
Array and matrix addition
Array and matrix subtraction a−b, A−B, A−a, or a−A.
.
*
Array multiplication
Matrix multiplication
Cv=A.*B, C(I,J)=A(I,J)*B(I,J).
*
A*B, standard matrix multiplication, or a*A, multiplication
of a scalar times all elements of A.
.
/
\
Array right division†
Array left division†
Matrix right division
Matrix left division
Array power
C=A./B,C(I,J)=A(I,J)/B(I,J).
.
C=A.\B,C(I,J)=B(I,J)/A(I,J).
/
\
A/Bis the preferred way to compute A*inv(B).
A\Bis the preferred way to compute inv(A)*B.
If C=A.^B, then C(I,J)=A(I,J)^B(I,J).
See helpfor a discussion of this operator.
.
^
^
Matrix power
.'
Vector and matrix transpose A.', standard vector and matrix transpose.
'
Vector and matrix complex
conjugate transpose
A', standard vector and matrix conjugate transpose. When A
is real A.'=A'.
+
−
:
Unary plus
+Ais the same as 0+A.
Unary minus
Colon
−Ais the same as 0−Aor −1*A.
Discussed in Section 2.8.1.
†
In division, if the denominator is 0, MATLAB reports the result as Inf(denoting infinity). If both the numerator and denomina-
tor are 0, the result is reported as NaN(Not a Number).