Matrices in Matlab: Topic 6 Arrays and Vectors
Matrices in Matlab: Topic 6 Arrays and Vectors
Matlab
Topic 6
Matrices in Matlab
An array is an elementary data structure. Almost all programming languages provide support for arrays. Matlab is a language that has been particularly specialised to support arrays (and subsequently matrices). An array is the obvious way to represent a vector. A 3D vector with coordinates [1, 2.5, 5] would be represented as an array of 3 doubles arranged sequentially in memory.
1 2.5 5
Matlab
Matlab
Operations on Arrays
This process of constructing an array involves a segment of memory being allocated and associated with the variable name, and the elements of memory being set to the specified values. In most programming languages you would have to declare an array and assign the values one at a time. In Matlab this is automatic. You can then perform arithmetic on arrays as simply as you can with scalars. For example:
>> b = a*2 b = 2 5
The square brackets indicate to Matlab that the contents represent an array:
>> whos Name Size Bytes Class a 1x3 24 double array Grand total is 3 elements using 24 bytes
10
Matlab
Matlab
Size of an Array
Matlab keeps track of the size of arrays and ensures you do not try to go beyond their bounds. For example:
>> b(4) ??? Index exceeds matrix dimensions. >> b(0) ! ??? Index into matrix is negative or zero.
You can also assign new values to individual elements of an array. For example:
>> b(3) = 6 b = 2 5 % Set the value of the 3rd element of b to 6 6
In Matlab, the index of the first element of an array is always 1. Note: this differs from languages such a C or Java where the index of the first element is always 0.
Matlab
Matlab
Matrices
An array is a collection of data objects of the same type. The data objects of the array can themselves be arrays. A matrix is typically represented by an array of arrays, or a 2D array. Matlab supports matrices in the same way that it supports vectors. Matlab uses the semi-colon (;) operator to distinguish between the different rows of a matrix. For example:
>> a = [1 2 3; 4 5 6] ! a = 1 4 2 5 3 6 % The ; separates the % individual 1D arrays.
As far as Matlab is concerned, everything is a matrix! A vector is a 1xN (or Nx1) matrix; a scalar is a 1x1 matrix.
Matlab
Matlab
>> c = a + 2 c = 3 5 4 6
For matrix addition or subtraction to work, the dimensions of the two matrices must match.
Matlab
Matlab
Matrix multiplication
Matrix multiplication is defined as in standard linear algebra. For example:
>> c = a * b c = 19 22 43 50 >> c = a * 2 % Matrix multiplication.
Point-wise Multiplication
Associated with matrices and vectors are a number of special operators, many of which are unique to Matlab. The .* operator performs point-wise multiplication on each corresponding pair of elements of two matrices (sometimes called array multiplication). For example:
>> c = a .* b % Point-wise multiplication. c = 5 21 12 32
% % % %
c = 2 6 4 8
For matrix multiplication to work, the number of columns in the first matrix must match the number of rows in the second matrix.
Matlab
Matlab
Matrix Division
Matrix division implies solving for matrix inverses. Matlab handles this automatically! Note that because matrix multiplication is not commutative, we require the concept of left and right division. Right division is post-multiplication by the inverse of a matrix: >> c = a / b; c = 3 2 -2 -1 % c = a * b-1
The .^ operator performs point-wise exponentiation on each corresponding pair of elements of two matrices. For example:
>> c = a .^ b c = 1 2187 64 65536 % Point-wise exponentiation.
Matlab
Matlab
Checking...
Left division is the most common. The expression c = a\b above would solve the equation b = a*c. Double check:
>> a*c ans = 5 7 6 8
which is the value of the b matrix. Most of these expressions would require at least 5 lines of code if programmed in some other language such as C or Java. Matlab's syntax yields very concise and readable code.
Matlab
Matlab
Colon Operator
For example:
>> x = 3 : 2 : 11 x = 3 5 7 9 11
Subarrays
As well as selecting individual elements from arrays, Matlab allows for the selection of sections of an array. For example:
>> a = [10:-1:1] a = 10 >> a(4:9) 9 8 7 6 5 4 3 2 1
The colon operator is enormously useful, not only for array creation but also for loop control.
% Use the colon operator (with a % default increment of 1) to select % elements 4 to 9 from the array. 6 5 4 3 2
ans = 7
Matlab
Matlab
Subarrays (cont.)
You can also assign values to subarrays.
>> a(1:3) = [8 9 10] a = 8 9 10 7 6 5 4 3 2 1
The size of the array being assigned must match the size of the array selected.
5 8
Matlab
Matlab
Matrix concatenation
Matlab has a very convenient syntax for concatenating matrices - just stick the matrices side by side, or on top of each other, within a set of enclosing square brackets.
>> a = [1 2 3] >> b = [a 7 8] b = 1 2 3 % % % % % 7 8 % Concatenate 7 and 8 onto the % end of a.
>> a = [a a(1:2) b ]
Construct a new matrix a. The first row is the "old a" with elements 1:2 of a concatenated to the end. The second row is made from array b.
a =
1 1
2 2
3 3
1 7
2 8
22
Matlab
Matlab
24
Matlab
Matlab
Array Multiplication
a .* b
Matrix Multiplication
a*b
26
Matlab
Matlab
a\b
a .\ b
Array Exponentiation
a .^ b