1st - Yr - Lecture 05 - NEW
1st - Yr - Lecture 05 - NEW
Lecture 5:
Arrays and Matrices
Last Lecture
2
Lecture Outline
• All variables that we have considered
so far only contain one value.
• A majority of calculations require
many values to be stored.
• Many calculations require that data
be stored as arrays or Matrices.
3
An Engineering Example…
Sea temperatures have long been of significant interest to
engineers and scientists. In order to model sea
temperatures, arrays and matrices are required. For
example, we need to know the temperature at many
positions within a volume of water.
4
An Engineering Example
Ocean Temperature Profile (Thermocline)
5
A simple Array
Usually, we don’t just need a single number as a
solution. Often we need several numbers
expressed as an array or a matrix.
• For example, consider the simple problem of
computing values of the sine function over half a
cycle.
7
Array Construction
• Below is an example of how I constructed the
x and y arrays. In this case I used the
Command Window, although I could have
used an Mfile.
8
Array Construction
• There are other (easier) ways of
constructing an array. This is especially
useful for large array constructions.
>> x = (0:0.1:1)*pi
Note the colon (:).
x=
The line that I typed:
Columns 1 through 7 x = (0:0.1:1)*pi
0 0.3142 0.6283 0.9425 1.2566 Literally translates as:
1.5708 1.8850
‘Make an array from 0
Columns 8 through 11 to 1 with a step of 0.1.
2.1991 2.5133 2.8274 3.1416 Multiply each term of
>> this array by pi’ 9
Array Construction
• There is one other way to construct the same
array
• We use the function linspace
>> x = linspace(0,pi,11) What I actually
x= typed in the
Columns 1 through 7 command
0 0.3142 0.6283 0.9425 window is in
1.2566 1.5708 1.8850 red. The rest
Columns 8 through 11 was output by
2.1991 2.5133 2.8274 3.1416 MATLAB in
>> response.
10
Array addressing
• Now that we have constructed our
array, how do we get information out
of the array? Imagine I have
constructed a vector called ‘vec’
9.00 5 >>
12
Array addressing
• We can even construct a new array, using
components from ‘vec’, lets call that ‘vec2’.
• Lets assume we need the 1st element of vec
twice, then the third element, then the fifth
element and finally the 2nd element.
>> vec(3.098)
??? Subscript indices must either be real positive integers or
logicals.
>> vec(210)
??? Index exceeds matrix dimensions. 14
Array orientation
Matlab automatically creates row vectors.
However, later, when you are using Matrix
multiplication you will need column vectors.
Lets create a column vector called
“column_vec”, it is the transpose of vec.
>> vec
vec =
0.8000 2.1000 3.1200 5.7600 9.0000 Note the use of the
transpose operator (’).
>> column_vec = vec'
column_vec =
If I type A’, MATLAB
0.8000 interprets this as the
2.1000 transpose of A.
3.1200
5.7600
9.0000
>>
15
Scaler-Array Mathematics
We have learnt to construct an array.
What if we want to operate on every
element of the array?
>> vec
vec = Note that
0.8000 2.1000 3.1200 5.7600 9.0000
multiplication,
>> 2*vec division addition and
ans =
subtraction of a
1.6000 4.2000 6.2400 11.5200 18.0000 scalar with an array
>> performs the
>> 2*vec/5 + 1 operation on every
ans =
individual element of
1.3200 1.8400 2.2480 3.3040 4.6000 the array.
>>
16
Scaler-Array Mathematics
>> vec
vec =
0.8000 2.1000 3.1200 5.7600 9.0000
>> 2*vec(3:5)
If you don’t know
ans = why MATLAB is
doing what it is
6.2400 11.5200 18.0000
>>
doing, refer back to
the previous slides.
>> sqrt(vec([1 4 5]))
ans =
length_of_vec =
5
>> 19
Array-Array Mathematics
• The two example vectors, vec and vec2
used in this lecture are the same size, so
we will use them to demonstrate array
multiplication addition and subtraction.
Lets start with array multiplication.
In Matlab we write:
>> vec2*vec'
0.8 0.8 3.12 9.0 2.1 X 0.8
ans =
2.1
3.12 82.7944
5.76 = 82.79
Vec2’
9.00
Vec 20
Array-Array Mathematics
• Sometimes we want to multiply every
element of an array by every element of
another array, individually, to create a new
array. We do this by using the ‘.* ’ operator.
0 0
0 1 0
0 0 3
23
A simple Matrix
>> A(2,:) = [0 1 0]
A=
0 0
3.14159265358979 0 0 0 1 0
0 1.00000000000000 0
0 0 3
>> A(3,:) = [0 0 3] 0 0
A=
3.14159265358979 0 0
0 1 0
0 1.00000000000000
0
0
0 3.00000000000000
0 0 3
25
Matrix Construction
• Here’s how I constructed the matrix A. In this
case I used the Command Window, although I
could have used an Mfile.
Note that
cos(A) solves
cosine of A for
every value of
A.
26
Matrix Construction
• There are other (possibly easier) ways of
constructing a matrix. This may be useful for large
matrix constructions.
• Lets construct the same array, I can do this by
typing the following:
>> M(2,1)
ans =
12
>>
28
Matrix addressing
In Matlab we write:
3 6 9 >> M(:,3)
12 8 4 Note the use ans =
of the Colon 9
10 15 20 “:” 4
20
>>
29
Matrix addressing
In Matlab we write:
3 6 9 >> M(2:3,2:3)
ans =
12 8 4 Note the use 8 4
of the Colon 15 20
10 15 20 “:” >>
30
Matrix addressing
>> M(8,3)
??? Index exceeds matrix dimensions.
31
Matrix orientation
• As with arrays, it is easy to find the transpose
of a matrix using MATLAB. For a matrix, the
row index is swapped with the column index.
You will learn more about transposing a matrix
later in the course.
>> M
M=
3 6 9 3 12 10 3 6 9
12 8 4
12 8 4 6 8 15 10 15 20
>> M'
10 15 20 9 4 20 ans =
3 12 10
6 8 15
Transpose 9 4 20
>>
32
Scaler-Matrix Mathematics
• We have learnt to construct a Matrix. What
if we want to operate on every element of
the matrix?
>> M
M= Note that as with an
3 6 9
12 8 4
array, multiplication,
10 15 20 division, addition
and subtraction of a
>> 2*M scalar with an array
ans = performs the
6 12 18 operation on every
24 16 8
20 30 40 individual element of
>> the matrix.
33
Scaler-Matrix Mathematics
• We have learnt to construct a Matrix. What
if we want to operate on every element of
the matrix?
>> M + 8
ans =
11 14 17
20 16 12
>> M 18 23 28
M= >>
3 6 9
12 8 4
10 15 20
>> M - 3
ans =
0 3 6
9 5 1
7 12 17
>>
34
Scaler-Matrix Mathematics
We can raise every element to a power,
or take the square root of every element
When we want to
>> M.^2
ans =
explicitly tell MATLAB to
9 36 81 operate on each element
144 64 16 individually, we place a
100 225 400
>>
“.” in front of the
operator.
35
Scaler-Matrix Mathematics
>> M
M=
3 6 9
3 6 9 2X 12 8 4
12 8 4
10 15 20 10 15 20
>> 2*M(2:3,1:2)
ans =
24 16 =
24 16
20 30 20 30
>>
37
Matrix – Array multiplication
>> example_vec = [5 6 7]
example_vec =
3 6 9 5 114 5 6 7
>> M*example_vec'
12 8 4 X 6 = 136 ans =
10 15 20 7 280 114
136
280
>>
38
Matrix – Array multiplication
42
Next Lecture
Next lecture we consider loops and decisions.
43