9.3. Array operations#

The MATLAB commands for the common operations on arrays are summarised in Table 9.2.

Table 9.2 Operations on NumPy arrays#

Operation

Description

MATLAB syntax

\(A + B\)

addition of two arrays

A + B

\(A - B\)

subtracting an array

A - B

\(kA\)

multiplying an array by a scalar

k * A

\(A^\mathsf{T}\)

matrix transpose

A'

\(A \odot B\)

element-wise matrix multiplication

A .* B

\(A B\)

matrix multiplication

A * B

\(A B C\)

multiple matrix multiplication

A * B * C

\(A^{\circ k}\)

raising elements of an array to a scalar power

A .^ k

\(A^k\)

raising a square matrix to a power

A ^ k

\(\det(A)\)

matrix determinant

det(A)

\(A^{-1}\)

matrix inverse

inv(A)

Enter the following code into your program (you may want to run your program after entering each line).

% Array operations
A
B = [5, 6 ; 7, 8]

fprintf("A + B")
A + B

fprintf("A - B")
A - B

fprintf("2A")
2 * A

fprintf("A^T")
A'

Run your program and your should see the following added to the command window.

A =

     1     2
     3     4


B =

     5     6
     7     8

A + B
ans =

     6     8
    10    12

A - B
ans =

    -4    -4
    -4    -4

2A
ans =

     2     4
     6     8

A^T
ans =

     1     3
     2     4

Here we have performed some basic arithmetic operations on the 2D arrays A and B.


9.3.1. Element-wise multiplication#

When dealing with multiplication of arrays we need to make sure we distinguish between multiplying the elements of two arrays and matrix multiplication. Element-wise multiplication of two matrices is defined by

\[ \begin{align*} [A \odot B]_{ij} = a_{ij} b_{ij}. \end{align*} \]

i.e., the corresponding elements are multipied together. Given the matrices

\[\begin{split} \begin{align*} A &= \begin{pmatrix} 1 & 2 \\ 3 & 4 \end{pmatrix}, & B &= \begin{pmatrix} 5 & 6 \\ 7 & 8 \end{pmatrix}, \end{align*} \end{split}\]

we have

\[\begin{split} \begin{align*} A \odot B = \begin{pmatrix} 1(5) & 2(6) \\ 3(7) & 4(8) \end{pmatrix} = \begin{pmatrix} 5 & 12 \\ 21 & 32 \end{pmatrix}. \end{align*} \end{split}\]

Enter the following code into your program

fprintf("A .* B")
A .* B

Run your program and your should see the following added to the command window.

A .* B
ans =

     5    12
    21    32

9.3.2. Matrix multiplication#

The other way which we multiply matrice is matrix multiplication which, for a \(m\times n\) matrix \(A\) and a \(p \times q\) matrix \(B\), is defined by

\[ \begin{align*} [AB]_{ij} = \sum_{k=1}^n a_{ik}b_{kj}. \end{align*} \]

So to multiply the matrices \(A\) and \(B\) defined above we have

\[\begin{split} \begin{align*} AB = \begin{pmatrix} 1(5) + 2(7) & 1(6) + 2(8) \\ 3(5) + 4(7) & 3(6) + 4(8) \end{pmatrix} = \begin{pmatrix} 5 + 14 & 6 + 16 \\ 15 + 28 & 18 + 32 \end{pmatrix} = \begin{pmatrix} 19 & 22 \\ 43 & 50 \end{pmatrix}. \end{align*} \end{split}\]

To perform matrix multiplication of two NumPy arrays A and B we use the A * B command. Enter the following code into your program.

fprintf("A * B")
A * B

Run your program and your should see the following added to the command window.

A * B
ans =

    19    22
    43    50

To calculate multiple matrix multiplications we simply chain multiple multiplication commands. Enter the following code into your program.

fprintf("ABA")
A * B * A

Run your program and your should see the following added to the command window.

ABA
ans =

    85   126
   193   286

9.3.3. Element-wise power#

Element-wise power is defined by

\[ [A ^{\circ k}]_{ij} = a_{ij}^k, \]

so for the matrix \(A\) defined above

\[\begin{split} A^{\circ 2} = \begin{pmatrix} 1^2 & 2^2 \\ 3^2 & 4^2 \end{pmatrix} = \begin{pmatrix} 1 & 4 \\ 9 & 16 \end{pmatrix}.\end{split}\]

Enter the following code into your program.

fprintf("A .^ 2")
A .^ 2

Run your program and your should see the following added to the command window.

A .^ 2
ans =

     1     4
     9    16

9.3.4. Matrix power#

The matrix power of a square matrix \(A\) is denoted by \(A^n\) and is defined by

\[ A = \underbrace{A \cdot A \cdots A}_{n \textsf{ times}}. \]

To calculate \(A^k\) we use the A ^ k command. Enter the following code into your program.

fprintf("A ^ 2")
A ^ 2

Run your program and your should see the following added to the command window.

A ^ 2
ans =

     7    10
    15    22

9.3.5. Matrix determinant#

The determinant of an \(n \times n\) square matrix is a numerical value associated with the matrix. The MATLAB command to calculate the determinant of the matrix A is det(A). Enter the following code into your program.

fprintf("det(A) = %0.2f \n", det(A))

Run your program and your should see the following added to the command window.

det(A) = -2.00

A matrix that has a determinant of 0 is known as a singular matrix.


9.3.6. Matrix inverse#

The inverse of a non-singular (a matrix with a non-zero determinant) square matrix \(A\), denoted by \(A^{-1}\), is a square matrix such that \(AA^{-1} = I\). The MATLAB command to calculate the inverse of the matrix A is inv(A). Enter the following code into your program.

fprintf("\ninv(A)")
inv(A)

Run your program and your should see the following added to the command window.

inv(A)
ans =

   -2.0000    1.0000
    1.5000   -0.5000

To check whether this is the inverse of \(A\) we can calculate \(A \cdot A^{-1}\) which should return the identity matrix. Enter the following code into your program.

fprintf("\nA * inv(A)")
A * inv(A)

Run your program and you should see the following added to the console output.

A * inv(A)
ans =

    1.0000         0
    0.0000    1.0000

9.3.7. Exercise#

Exercise 9.3

Using the arrays defined in Exercise 9.1 to calculate the following:

  1. \(2\vec{a}\)

  2. \(B + D\)

  3. \(C^\mathsf{T}\)

  4. \(B \odot D\)

  5. \(DB\)

  6. \(DBB^\mathsf{T}\)

  7. \(D^{\circ 3}\)

  8. \(B^4\)

  9. \(\det(B)\)

  10. \(B^{-1}\)