9.3. Array operations#
The MATLAB commands for the common operations on arrays are summarised in Table 9.2.
| Operation | Description | MATLAB syntax | 
|---|---|---|
| \(A + B\) | addition of two arrays | 
 | 
| \(A - B\) | subtracting an array | 
 | 
| \(kA\) | multiplying an array by a scalar | 
 | 
| \(A^\mathsf{T}\) | matrix transpose | 
 | 
| \(A \odot B\) | element-wise matrix multiplication | 
 | 
| \(A B\) | matrix multiplication | 
 | 
| \(A B C\) | multiple matrix multiplication | 
 | 
| \(A^{\circ k}\) | raising elements of an array to a scalar power | 
 | 
| \(A^k\) | raising a square matrix to a power | 
 | 
| \(\det(A)\) | matrix determinant | 
 | 
| \(A^{-1}\) | matrix inverse | 
 | 
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
i.e., the corresponding elements are multipied together. Given the matrices
we have
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
So to multiply the matrices \(A\) and \(B\) defined above we have
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
so for the matrix \(A\) defined above
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
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:
- \(2\vec{a}\) 
- \(B + D\) 
- \(C^\mathsf{T}\) 
- \(B \odot D\) 
- \(DB\) 
- \(DBB^\mathsf{T}\) 
- \(D^{\circ 3}\) 
- \(B^4\) 
- \(\det(B)\) 
- \(B^{-1}\)