Linear Algebra and Programming Skills

Dr Jon Shiach, Department of Computing and Mathematics, Manchester Metropolitan University

Arrays

Learning Outcomes

On successful completion of this page readers will be able to:
In mathematics, vectors can be expressed as either a row or column of elements and matrices are a rectangular array of elements. An individual element in the vector is identified by an index denoted using a subscript, e.g., . The index is the position of the element in the vector starting at 1 for the first element. An individual element in a matrix is identified by two indices denoted in a subscript, e.g., , the first number corresponding to the row number and the second number corresponding to the column number.
In computer programming, a vector or matrix is represented using an array. Arrays can be one-dimensional where they contain a single row or column of elements, similar to a vector, or two-dimensional array similar to a matrix. It is possible to have higher dimensional arrays but this is not recommended as it can cause confusion.

Defining arrays

To define a one-dimensional array in MATLAB we use the following
A = [ a1 , a2 , ... , an ]
It is standard practice in programming to use an uppercase character for the first character in the array name. This helps to differentiate between arrays and variables.
Example 1
The MATLAB command below defines the array . Enter it into the code cell below and run the live script.
A = [ 1, 2, 3 ]
clear
A = [ 1, 2, 3 ]
A = 1×3
1 2 3
Note that the elements in the row vector are contained within square bracket. To define a two-dimensional array (i.e., a matrix) we use semi-colons ; to separate the rows.
A = [ a11 , a12 , ... , a1n ;
a21 , a22 , ... , a2n ;
: : :
am1 , am2 , ... , amn ]
Example 2
The commands below defines the array corresponding to the matrices
Enter them into the code cells below and run the live script.
A = [ 1, -2 ;
0, 5 ]
A = [ 1, -2 ;
0, 5 ]
A = 2×2
1 -2 0 5
B = [ 2 ; -4 ; 5 ]
B = [ 2 ; -4 ; 5 ]
B = 3×1
2 -4 5
C = [ 1, 0, 7 ;
4, 7, 5 ]
C = [ 1, 0, 7 ;
4, 7, 5]
C = 2×3
1 0 7 4 7 5

Exercise 1 - Defining arrays

1. Define arrays corresponding to the following vectors and matrices.
(a) ;
A = [ 6, 2, 4, -1 ]
A = 1×4
6 2 4 -1
(b) ;
B = [ 3, 5, -2 ;
-2, 4, 3 ;
7, 2, -1 ]
B = 3×3
3 5 -2 -2 4 3 7 2 -1
(c) ;
C = [ 2, 0, -1, 4 ;
7, -3, 9, -5 ]
C = 2×4
2 0 -1 4 7 -3 9 -5
(d) .
D = [ -4, 4, 2 ;
7, 5, -3 ;
5, 1, 6 ]
D = 3×3
-4 4 2 7 5 -3 5 1 6

Indexing arrays

In MATLAB , the elements of an array are indexed by their position starting at 1 for the first element. The index is written in brackets following the array name (help page).
A(i)
For two-dimensional arrays, the indices of an element are separated by a comma.
A(i, j)
To index elements at the end of a row or column we can use the index end.
A(end)
Example 3
The commands below defines the array corresponding to the matrix
and outputs individual elements from it using array indexing. Enter them into the code cells below and run the live script.
A = [ 1, 2, 3 ;
4, 5, 6 ;
7, 8, 9 ];
 
A(1, 1) % output the element in row 1 column 1 of A
A(3, 2) % output the element in row 3 column 2 of A
A(end-1, end) % output the element in the second to last row and last column of A
A = [ 1, 2, 3 ;
4, 5, 6 ;
7, 8, 9 ]
A = 3×3
1 2 3 4 5 6 7 8 9
A(1, 1) % output the element in row 1 column 1 of A
ans = 1
A(3, 2) % output the element in row 3 column 2 of A
ans = 8
A(end-1, end) % output the element in the second to last row and last column of A
ans = 6

Determining the size of an array

The number of elements in a one-dimensional array can be determined using the length command (help page)
length(A)
The number of rows and columns in an two-dimensional array can be determined using the size command (help page)
size(A)
Example 4
The following commands defines arrays corresponding to the matrices
and
and outputs their sizes. Enter them into the code cells below and run the live script.
A = [ 1, 2, 3, 4 ];
B = [ 1, 3, 5 ;
7, 9, 11 ;
13, 15, 17 ;
19, 21, 23];
 
length_of_A = length(A);
[rows, cols] = size(B);
 
fprintf('The array A has %1i elements.', length_of_A)
fprintf('The array B has %1i rows and %1i columns.', rows, cols)
A = [ 1, 2, 3, 4 ];
B = [ 1, 3, 5 ;
7, 9, 11 ;
13, 15, 17 ;
19, 21, 23];
 
fprintf('The array A has %1i elements.', length(A))
The array A has 4 elements.
fprintf('The array B has %1i rows and %1i columns.', size(B, 1), size(B, 2))
The array B has 4 rows and 3 columns.

Array slicing

Array slicing allows us to return multiple elements from an array
A(start : stop)
This command will return all the elements of the array A between start and stop inclusive. If start and stop are not specified then MATLAB will return all of the elements in the appropriate dimension of A.
A(start : step : stop)
This command will return all of the elements of A between start and stop with increments of step. If step is not specified MATLAB assumes a value of 1.
Example 5
Define an array for the matrix A below and use array slicing to print the following
,
(i) the first row of A
A = [ 1, 2, 3 ;
4, 5, 6 ;
7, 8, 9 ];
 
A(1, :)
A = [ 1, 2, 3 ;
4, 5, 6 ;
7, 8, 9 ];
 
A(1, :)
ans = 1×3
1 2 3
(ii) the second column of A
A(:, 2)
A(:, 2)
ans = 3×1
2 5 8
(iii) the elements from the second column onwards in the second row of A
A(2, 2:end)
A(2, 2:end)
ans = 1×2
5 6
(iv) the last row of A in reverse order
A(end, end:-1:1)
A(end, end:-1:1)
ans = 1×3
9 8 7

Exercise 2 - Indexing arrays

2. Define an array corresponding to the matrix
.
A = [ 6, -2, 4, 0 ;
-4, 6, -1, 2 ;
4, -3, -5, 6 ]
A = 3×4
6 -2 4 0 -4 6 -1 2 4 -3 -5 6
3. Use the array defined in question 2 and array indexing to output:
(a) ;
A(1, 2)
ans = -2
(b) ;
A(3, 2)
ans = -3
(c) the first row of A;
A(1, :)
ans = 1×4
6 -2 4 0
(d) the middle two elements of the second row of A;
A(2, 2:end-1)
ans = 1×2
6 -1
(e) the even columns of A;
A(:, 2:2:end)
ans = 3×2
-2 0 6 2 -3 6
(f) the matrix A flipped upside-down (i.e., the rows of A in reverse order).
A(end:-1:1, :)
ans = 3×4
4 -3 -5 6 -4 6 -1 2 6 -2 4 0

Generating special matrices

MATLAB has some commands that can be used to generate special arrays. To generate an array containing all zeros, all ones or the identity matrix we can use the following commands
zeros(m, n)
ones(m, n)
eye(n)
Example 6
Use MATLAB commands to generate the following arrays
(i)
zeros(3, 3)
zeros(3, 3)
ans = 3×3
0 0 0 0 0 0 0 0 0
(ii)
ones(3, 2)
ones(3, 2)
ans = 3×2
1 1 1 1 1 1
(iii) (the identity matrix)
eye(4)
eye(4)
ans = 4×4
1 0 0 0 0 1 0 0 0 0 1 0 0 0 0 1

Sequences of numbers

A one-dimensional array containing a sequence of numbers can be generated using
start : end
start : step : end
If step is not included then MATLAB assumes this is 1.
Example 7
Genarate arrays which contain the following sequences of numbers
(i)
1 : 9
1 : 9
ans = 1×9
1 2 3 4 5 6 7 8 9
(ii)
2 : 3 : 38
2 : 3 : 38
ans = 1×13
2 5 8 11 14 17 20 23 26 29 32 35 38
(iii)
100 : -8 : 4
100 : -8 : 4
ans = 1×13
100 92 84 76 68 60 52 44 36 28 20 12 4

Exercise 3 - Generating arrays

4. Define a array where each element has the value 1.
A = ones(10, 10)
A = 10×10
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
5. Define a identity matrix.
I = eye(8)
I = 8×8
1 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 1
6. Define an array containing the first ten multiples of 3.
B = 3 : 3 : 30
B = 1×10
3 6 9 12 15 18 21 24 27 30

Concatenating (merging) arrays

Two arrays A and B can be concatenated (merged) using
[ A, B ]
This will form a new matrix where B is placed below A. To form a matrix where B is placed to the right of A, we can use
[ A ; B ]
Example 8
The commands below define array corresponding to the matrices
and
and concatenates them. Enter them into the code cell below and run the live script.
A = [ 1, 2 ;
3, 4 ];
B = [ 5, 6 ;
7, 8 ];
 
[ A, B ] % merge A and B side-by-side
[ A ; B ] % merge A and B with A on top of B
A = [ 1, 2 ;
3, 4 ];
B = [ 5, 6 ;
7, 8 ];
 
[ A, B ] % merge A and B side-by-side
ans = 2×4
1 2 5 6 3 4 7 8
[ A ; B ] % merge A and B with A on top of B
ans = 4×2
1 2 3 4 5 6 7 8

Matrix and array operations

The MATLAB commands for the common operations on matrices and arrays are summarised in the table below (help page).
* Other mathematical functions are treated similarly.
Example 9
Define arrays for the matrices A and B below and print the result of the the following operations:
(i)
A = [1, 2 ; 3, 4];
B = [5, 6 ; 7, 8];
 
A + B
A = [1, 2 ; 3, 4];
B = [5, 6 ; 7, 8];
 
A + B
ans = 2×2
6 8 10 12
(ii)
3 * A
3 * A
ans = 2×2
3 6 9 12
(iii) (element-wise multiplication)
A .* B
A .* B
ans = 2×2
5 12 21 32
(iv)
A * B
A * B
ans = 2×2
19 22 43 50
(v)
A * B * A
A * B * A
ans = 2×2
85 126 193 286
(vi) (element-wise power)
A .^ 3 % element-wise power
A .^ 3
ans = 2×2
1 8 27 64
(vii) (matrix power)
A ^ 3
ans = 2×2
37 54 81 118
(viii) (matrix transpose)
A'
A'
ans = 2×2
1 3 2 4
(ix) (the determinant of A)
det(A)
det(A)
ans = -2
(x) (inverse of a matrix)
inv(A)
inv(A)
ans = 2×2
-2.0000 1.0000 1.5000 -0.5000
(xi)
sin(A)
sin(A)
ans = 2×2
0.8415 0.9093 0.1411 -0.7568

Exercise 4 - Matrix and array operations

7. Define arrays corresponding to
and .
A = [ 1, 4, -2 ;
0, 5, 7 ;
4, 1, -9 ]
A = 3×3
1 4 -2 0 5 7 4 1 -9
B = [ 5, 1, 8 ;
-4, -2, 0 ;
5, 11, 3 ]
B = 3×3
5 1 8 -4 -2 0 5 11 3
8. Using the arrays you defined in question 7 calculate:
(a) ;
A - 3 * B
ans = 3×3
-14 1 -26 12 11 7 -11 -32 -18
(b) ;
A .* B
ans = 3×3
5 4 -16 0 -10 0 20 11 -27
(c) ;
A * B
ans = 3×3
-21 -29 2 15 67 21 -29 -97 5
(d) ;
B * A
ans = 3×3
37 33 -75 -4 -26 -6 17 78 40
(e) ;
A * B * A
ans = 3×3
-13 -227 -179 99 416 250 -9 -596 -666
(f) all elements of B raised to the power of 3;
B .^ 3
ans = 3×3
125 1 512 -64 -8 0 125 1331 27
(g) ;
B ^ 3
ans = 3×3
261 583 680 -220 -364 -192 161 503 115
(h) ;
A'
ans = 3×3
1 0 4 4 5 1 -2 7 -9
(i) ;
det(A)
ans = 100
(j) ;
inv(A)
ans = 3×3
-0.5200 0.3400 0.3800 0.2800 -0.0100 -0.0700 -0.2000 0.1500 0.0500
(j) .
cos(B)
ans = 3×3
0.2837 0.5403 -0.1455 -0.6536 -0.4161 1.0000 0.2837 0.0044 -0.9900