Skip to main content
Engineering LibreTexts

18.1: Matrix Multiplication (and Addition)

  • Page ID
    55686
  • \( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \) \( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)\(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\) \(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\)\(\newcommand{\AA}{\unicode[.8,0]{x212B}}\)

    We can think of a hypothetical computer (or scripting) language in which we must declare a "tableau" of \(m\) by \(n\) numbers to be either a double-index array or a matrix; we also introduce a hypothetical "multiplication" operator #. (Note that # is not an actual MATLAB multiplication character/operator - it is introduced here solely for temporary pedagogical purposes.) In the case in which we (say) declare A and B to be arrays then the product \(C=A \# B\) would be automatically interpreted as element-by-element multiplication: both A and B must be of the same size \(m \times n\) for the operation to make sense, and the result C would of course also be of size \(m \times n\). In the case in which we declare \(\mathrm{A}\) and \(\mathrm{B}\) to be matrices then the product \(\mathrm{A} \# \mathrm{~B}\) would be automatically interpreted as matrix-matrix multiplication: if \(\mathrm{A}\) is \(m_{1}\) by \(n_{1}\) and \(\mathrm{B}\) is \(m_{2}\) by \(n_{2}\) then \(n_{1}\) must equal \(m_{2}\) for the operation to make sense and the product \(\mathrm{C}=\mathrm{A} \# \mathrm{~B}\) would be of dimensions \(m_{1} \times n_{2}\). This is a very simple example of object-oriented programming in which an operation, say multiplication, is defined - in potentially different ways - for different classes of objects (in our case here, arrays and matrices) - but we could also envision an extension to functions and other entities as well. This model for programming languages and abstraction can be very powerful for a variety of reasons.

    However, in some cases such abstraction can arguably be more of a burden than a blessing. For example, in MATLAB we often wish to re-interpret arrays as matrices or matrices as arrays on many different occasions even with a single code or application. To avoid conversion issues between these two classes, MATLAB prefers to treat arrays and matrices as (effectively) a single class and then to distinguish the two options for multiplication through special operators. In particular, as we already know, element-by-element multiplication of two arrays is effected by the .* operator \(\mathrm{C}=\mathrm{A} . * \mathrm{~B}\) forms C as the element-by-element product of \(\mathrm{A}\) and \(\mathrm{B}\); matrix-matrix multiplication (in the sense of linear algebra) is then effected simply by \(*-C=A * B\) forms \(C\) as the matrix product of A and B. In fact, the emphasis in MATLAB at least historically is on linear algebra, and thus matrix multiplication is in some sense the default; element-by-element operations are the "special case" and require the "dotted operators."

    In principle, we should also need to distinguish element-by-element addition and subtraction as .+ and .- from matrix-matrix addition and subtraction as + and -. However, element-by-element addition and subtraction and matrix-matrix addition and subtraction are identical - both in terms of the requirements on the operands and on the result of the operation - and hence it suffices to introduce only a single addition and subtraction operator, \(+\) and \(-\), respectively. (In particular, note that there are no operators . + and . - in MATLAB.) In a similar fashion, we need only a single transpose operator, ’, which is directly applicable to both arrays and matrices. \({ }_{-}\)

    It thus follows that the matrix-matrix addition, subtraction, multiplication, and transpose are effected in MATLAB in essentially the same way as we would write such operations in the linear algebra context: in the addition or subtraction of two vectors \(x\) and \(y\), the \(x+y\) and \(x-y\) of linear algebra becomes \(\mathrm{x}+\mathrm{y}\) and \(\mathrm{x}-\mathrm{y}\) in MATLAB; in the multiplication of two matrices \(A\) and \(B\), the \(A B\) of linear algebra becomes \(\mathrm{A} * \mathrm{~B}\) in MATLAB; and in the transpose of a matrix (or vector) \(M\), the \(M^{\mathrm{T}}\) of linear algebra becomes \(\mathrm{M}\) ’ in MATLAB.

    Of course, you could also always implement these matrix operations in MATLAB "explicitly" with for loops and appropriate indexing: for example, \(z=x+y\) could be implemented as

    z = 0.*x; % initialize z to be same size as x 
    for i = 1:length(x) 
        z(i) = x(i) + y(i); 
    end
    

    however this leads to code which is both much less efficient and also much longer and indeed much less readable (and hence de-buggable). (Note also that the above does not yet contain any check on dimensions or error flags.) We have already discussed the power of function abstraction. In the case of these very ubiquitous functions - standard array and matrix manipulations - MATLAB provides the further convenience of special characters and hence very simple syntax. (Note that as these special characters are, as always, just an easy way to invoke the underlying MATLAB operator or function: for example, the element-by-element multiplication operation A.B can also be written (but less conveniently) as times \((A, B)\), and the matrix-matrix multiplication AB can also be written as mtimes (A,B).)

    We close with a simple example to again illustrate the differences between array and matrix operations. We introduce two column vectors \(x=\left(\begin{array}{ll}1 & 1\end{array}\right)^{\mathrm{T}}\) and \(y=\left(\begin{array}{ll}2 & 2\end{array}\right)^{\mathrm{T}}\) which in MATLAB we express as \(\mathrm{x}=[1 ; 1]\) and \(\mathrm{y}=[2 ; 2]\). (Note the distinction: parentheses for vectors and matrices in the linear algebra context, brackets for vectors and matrices in MATLAB; parentheses in MATLAB are used for indexing and function calls, not to define a vector or matrix.) We may then perform the linear algebra operation of inner product, \(\alpha=x^{\mathrm{T}} y\), in two fashions: with element-byelement multiplication (and hence times) as alpha \(=\operatorname{sum}(\mathrm{x} \cdot * \mathrm{y})\); with matrix multiplication (and hence mtimes) as alpha_too \(=\mathrm{x}^{\prime} * \mathrm{y}\).


    This page titled 18.1: Matrix Multiplication (and Addition) is shared under a CC BY-NC-SA 4.0 license and was authored, remixed, and/or curated by Masayuki Yano, James Douglass Penn, George Konidaris, & Anthony T Patera (MIT OpenCourseWare) via source content that was edited to the style and standards of the LibreTexts platform; a detailed edit history is available upon request.