0% found this document useful (0 votes)
31 views43 pages

1st - Yr - Lecture 05 - NEW

This document discusses arrays and matrices in MATLAB. It begins with an engineering example of modeling sea temperatures which requires storing data in arrays and matrices. It then covers constructing simple arrays in MATLAB using brackets and the colon operator. Addressing elements in arrays using indices is demonstrated, as is array orientation with row and column vectors. The document shows how to perform scalar-array operations like multiplication, division, and power functions on entire arrays element-wise. Finally, it briefly introduces array-array operations like multiplication.

Uploaded by

Shaun
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
31 views43 pages

1st - Yr - Lecture 05 - NEW

This document discusses arrays and matrices in MATLAB. It begins with an engineering example of modeling sea temperatures which requires storing data in arrays and matrices. It then covers constructing simple arrays in MATLAB using brackets and the colon operator. Addressing elements in arrays using indices is demonstrated, as is array orientation with row and column vectors. The document shows how to perform scalar-array operations like multiplication, division, and power functions on entire arrays element-wise. Finally, it briefly introduces array-array operations like multiplication.

Uploaded by

Shaun
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 43

ENG 1060

Computing for Engineers

Lecture 5:
Arrays and Matrices
Last Lecture

• We discussed writing M files


– Good programming techniques
• Comments
• Meaningful variable names
– Mfile basics
– Executing Mfiles
– Mfile functions.

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.

• Today we discuss how to construct


both arrays and matrices in MATLAB.

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.

• Let’s start by choosing a finite number of points in


this range, lets evaluate:

• How would we get MATLAB to store all of this


information?
6
A simple Array
• Here is the information we want to store as a
variable:
x 0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 

y 0 0.31 0.59 0.81 0.95 1 0.95 0.81 0.59 0.31 0

• We want MATLAB to store this information as


an array.

• To construct this array in MATLAB, all we did


was start with a left bracket “[”, and close the
array with a right bracket “]”.

• Each term in the array is separated by a space.

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.

Note that sin(x)


solves sine of x
for every value
of x.

8
Array Construction
• There are other (easier) ways of
constructing an array. This is especially
useful for large array constructions.

• Lets construct the same array, I can do this


by typing the following:

>> 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.

• Note that to use linspace we write:


linspace (first_value, last_value, number_of_values)

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’

Imagine we need to know


0.8 1 what the third element of
x is…
2.1 2
In Matlab we write:
3.12 3
vec >> vec(3)
5.76 4 ans =
9.00 5 3.1200
>>
11
Array addressing

• What would I type if I need to have


the 3rd, 4th and 5th elements of vec?

0.8 1 In Matlab we write:

2.1 2 >> vec(3:5)


Note the
3.12 3 use of the
ans =
Colon “:”
5.76 4 3.1200 5.7600 9.0000

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.

0.8 1 0.8 1 In Matlab we write:

2.1 2 0.8 2 >> vec2 = vec([1 1 3 5 2])

3.12 3 3.12 3 vec2 =

5.76 4 9.0 4 0.8000 0.8000 3.1200


9.0000 2.1000
9.00 5 2.1 5
>>
Vec Vec2 13
Array addressing

• When addressing an array, you must


use an integer value between 1 and
the length of the array. Otherwise
you will get the following error
messages:

>> 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

We can raise every element to a power,


or take the square root of every element
Note the use of the
>> vec “.^” term to raise
vec = every element in the
0.8000 2.1000 3.1200 5.7600 9.0000
array to the power of
>> vec.^2
ans =
2.
When we want to
0.6400 4.4100 9.7344 33.1776 81.0000 explicitly tell
>>
MATLAB to operate
>> sqrt(vec) on each element
ans =
individually, we
0.8944 1.4491 1.7664 2.4000 3.0000 place a “.” in front of
>> the operator.
17
Scaler-Array Mathematics

We can also operate with just some


elements of an array.

>> 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 =

0.8944 2.4000 3.0000


>>
18
Array-Array Mathematics
• Usually we will need to multiply two arrays
together, this is also very easy to do. However
we have to take care that the two arrays have
the same number of elements.
• Additionally one needs to be a column vector
and the other a row vector to satisfy Matrix
algebra.
• To check the number of elements of the array
“vec” in MATLAB, we can write length(vec),
Matlab will return the value size as an integer.
>> length_of_vec = length(vec)

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.8 0.8 0.64 In Matlab we write:


2.1 0.8 1.68 >> vec.*vec2
3.12 .* 3.12 = 9.7
ans =
5.76 9.0 51.8
0.6400 1.6800 9.7344
9.00 2.1 18.9
51.8400 18.9000
Vec Vec2 >>
21
Array-Array Mathematics

• Addition and subtraction of arrays also


occurs on an element by element basis,
and is very simple to implement, for
example…

0.8 0.8 1.6 In Matlab we write:


2.1 0.8 2.9 >> vec + vec2
3.12 + 3.12 = 6.24
ans =
5.76 9.0 14.8
1.6000 2.9000 6.2400
9.00 2.1 11.1
14.7600 11.1000
Vec Vec2 >>
22
A simple Matrix

Often (for example when solving Ordinary


Differential Equations) we need several
numbers expressed as an array or a matrix.

In this case, we are going to ask MATLAB to


generate the following simple 3x3 matrix:

 0 0
 0 1 0
 
 0 0 3
23
A simple Matrix

We construct the Matrix in a way very


similar to constructing an array. In the
following line, I construct the first row of
the matrix:
>> A(1,:) = [pi 0 0]
A=
3.14159265358979 0 0
Note the use of the colon (:). The
line A(1,:) literally translates to:  0 0
"ALL the elements in the first
row of the matrix A”.
 0 1 0
The colon tells MATLAB that
 
you want all the elements in that  0 0 3
row.
24
A simple Matrix
• Lets continue constructing the 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:

>> A = zeros(3,3) The function zeros creates a


A= matrix of zeros. The first
0 0 0 parameter is the number of
0 0 0 rows, the second parameter
• I0can0 then
0 add in the values
is thethat I want
number on the
of columns.
diagonal:

I have now created precisely


>> A(1,1) = pi; the same matrix as I did
>> A(2,2) = 1; previously. Note, the
>> A(3,3) = 3; semicolons are there to stop
MATLAB writing to the screen.27
Matrix addressing

• Now that we have constructed our


matrix, how do we get information
out of the matrix? Imagine I have
constructed a matrix called ‘M’

Imagine we need to know


what the element in the
3 6 9 first column and second
row of M is…
12 8 4
10 15 20 In Matlab we write:

>> M(2,1)
ans =
12
>>
28
Matrix addressing

• What if I needed to know the


elements in the third column?

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

• Finally, what if I need to know the


subset matrix highlighted in the
figure below?

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

• As with arrays, you have to use an


integer value between 1 and the
length of either the row or the
column of the matrix when
addressing the matrix. Otherwise you
will get the following error messages:
>> M(1,3.098)
??? Subscript indices must either be real positive integers or logicals.

>> 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

Note the use of the “.^”


>> M
M= term to raise every
3 6 9 element in the array to
12 8 4 the power of 2.
10 15 20

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

• We can raise every element to a power,


or take the square root of every element

>> M The function sqrt


M= takes the square
3 6 9
12 8 4 root of every
10 15 20 element of the matrix
individually.
>> sqrt(M)
ans =
1.73205080756888 2.44948974278318 3.00000000000000
3.46410161513775 2.82842712474619 2.00000000000000
3.16227766016838 3.87298334620742 4.47213595499958
>>
36
Scaler-Matrix Mathematics

• We can also operate with just some


elements of a matrix.

>> 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

It is easy to perform matrix –


array Multiplication, for example:

>> 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

What if the dimensions are not those


required by the operations? For
example:

0.8 3 6 9 >> example_vec'*M


0.6 X 12 8 4 ??? Error using ==> mtimes
Inner matrix dimensions must agree.
5 10 15 20
>>

This matrix multiplication


makes no sense, the
dimensions of the matrix “M”
are incorrect.
39
Matrix – Matrix multiplication

We can also easily multiply two


matrices if the dimensions of both
matrices are correct. For example:

>> M1(1,:) = [1 0 5];


3 6 >> M1(2,:) = [9 4 7];
1 0 5 >> M2(:,1) = [3 12 10];
X 12 8
>> M2(:,2) = [6 8 15];
9 4 7
10 15 >> M1*M2
ans =
53 81
53 81 145 191
=
145 191
40
Matrix – Matrix multiplication
• Finally, every element of a matrix may be
individually multiplied by every element of
another matrix, provided both matrices have
the same dimensions:

1 0 3 6 >> M1(1,:) = [1 0];


.* >> M1(2,:) = [9 4];
9 4 12 8 >> M2(1,:) = [3 6];
>> M2(2,:) = [12 8];
>> M1.*M2
ans =
3 0
108 32
3 0 >>
=
108 32
41
Lecture summary

• You should now be able to


construct and manipulate arrays
and matrices in MATLAB.
• You should go through the
examples in this lecture in your
own time using MATLAB.
• You should read page 15-26 of
Chapra.

42
Next Lecture
Next lecture we consider loops and decisions.

-This will enable you to write code that does two


things:

-Tell Matlab to perform a task multiple times


-Get Matlab to make decisions about which
task it should perform

-We will be using M files (programming), if


you don’t remember how to use Mfiles, review
lecture 3.

43

You might also like