Upload
joepepperone
View
19
Download
9
Embed Size (px)
DESCRIPTION
monash.
Citation preview
2015 – Semester 2
Lecturer: Dr Tony Vo
Slides by: Dr Tony Vo
Photo credit: Janet Waters
Lecture 3
Matrices
ENG1060: Computing for Engineers
Using MATLAB as a calculator (command window)– A = 3 * 10 ^ 4 + 5 / 8– A = 3 * 10 ^ (4 + 5) / 8 (order of operation)
Proper variable naming– Queensland | Queen | Que | Q
Built-in functions– sin, cos, log, floor, round
Solving problems– Think before you do– Challenge your answers (realistic?)– Do your work in m-files
Lecture 2 recapENG1060: Computing for Engineers
What are matrices?
Creating matrices
Matrix addressing
Lecture 3 overviewENG1060: Computing for Engineers
A matrix is a variable with multiple values– Matrices allow us to perform many calculations– Matrices are often called arrays in programming
MATLAB stands for MATrix LABoratory– MATLAB excels at matrix operations
Matrices are used everywhere in engineering and science
What is a matrix?ENG1060: Computing for Engineers
Matrix structure: rows-by-columns
Multi-dimensional matrices
𝐴𝐴 = 1 (scalar)
𝐵𝐵 = 1 2 3 4 5 (row vector, 1-by-5 matrix)
C=
12345
(column vector, 5-by-1 matrix)
D=147
258
369
47
10(3-by-4 matrix)
ENG1060: Computing for Engineers
Thinking about it physically…– 0D matrices are points– 1D matrices are edges/sides– 2D matrices are faces– 3D matrices are surfaces
Multi-dimensional matrices
An edge A face A surface
ENG1060: Computing for Engineers
Audio example– playaudio.m– .wav: Less than 1 second of audio and it's a 13680 × 1 matrix– .mp3: A 597883 × 2 matrix
Graphics example– playimage.m– Black and white image: 135 × 90– Colour image: 135 × 90 × 3
Other examplesENG1060: Computing for Engineers
Matrices are used everywhere in engineering and science– We deal with large and complex problems
Audio processing– E.g. 2 minute audio contains 5,777,100 data
Image processing– E.g. 10 Megapixels colour image contains 2592 × 3872 × 3 =
30,108,672 data
Video processing– E.g. High speed video (1280 x 1024, 1000 fps) contains 1,310,720,000
data per second
Why use matrices?ENG1060: Computing for Engineers
Humans are incapable of retaining a lot of information and we're slow!– Instead we use computers to store data in matrices
Example: Hawkeye (Tennis video referee)– 10 cameras, each capturing 1000 images/second– Each image is 1280 x 1024 pixels (1.3 Megapixels)– In 1 second, 1.2 × 1010 values need to be processed
Matrix exampleENG1060: Computing for Engineers
Square brackets [ ] to create matrices
Row vectors are horizontalSyntax: A = [1 2 3 4] or A = [1, 2, 3, 4]
Column vectors are verticalSyntax: B = [5; 6; 7; 8]
To convert row vectors to column vectors and vice versa– Use the transpose operator ' (apostrophe)– Or use the transpose command
Creating 1D matrices (vectors)ENG1060: Computing for Engineers
What if I require numbers from 1 to 106?
We do not want to type it all out! – Remember, engineers are lazy…
Typing it outENG1060: Computing for Engineers
Colon operator :– Creates a vector with equally spaced values using a specified spacing
Syntax: start_value : step : end_value– Default step of 1 if step is omitted
Examples:A = [6 7 8 9 10] or A = 6 : 1 : 10 or A = 6 : 10B = [6 11 16 21 26] or B = 6 : 5 : 26C = [6 4 2 0 -2 -4] or C = 6 : -2 : -4
The colon operatorENG1060: Computing for Engineers
How to create a vector with the following requirements?– First value is 3– Last value is less than or equal to 40– Step size of 5
What if the value of 40 is needed?– Is the end value important?
Colon operator to create column vectors?– Use round brackets with apostrophe: A = (1 : 20)'
The colon operator: LimitationsENG1060: Computing for Engineers
What if I require 7 equally spaced numbers between 3.851 and 7.84?– I can manually calculate the step size
Step size = (end value – start value)/(number of points - 1)
Step size = 7.84 −3.8517 −1
= 0.6648
→ A = 3.851 : 0.6648 : 7.84
Is there an alternative?
What if …ENG1060: Computing for Engineers
The linspace function:– Create a vector with equally spaced values using a specified number of
points
Syntax: linspace(start_value, end_value, num_of_points)– Default of 100 points if num_of_points is omitted
Examples:A = [1 2 3 4 5 6] or linspace(1, 6, 6)
B = [3.8510 4.5158 5.1807 5.8455 6.5103 7.1752 7.84]or B = linspace(3.851, 7.84, 7)
The linspace functionENG1060: Computing for Engineers
You are working as a consultant for a company that installs street lamps
Objective: They want these lamps to be spread out in 20 metre intervals along the street
You have been asked to determine the displacements (x) to install lamps along a street that is 400 metre long
MATLAB command:X = ???
Example: Use of the colon operatorENG1060: Computing for Engineers
They've changed their minds. They now want a fixed number of lamps to be spread evenly along the street using 23 lamps
You have been asked to determine the displacements (x) to install 23 lamps along a street that is 525 metres long
MATLAB command:X = ???
Example: Use of the linspace functionENG1060: Computing for Engineers
Both create vectors of equally spaced values– Colon operator uses a specified step size– Linspace function uses a specified number of values
Use both appropriately throughout your coding in this unit
What does logspace do?
Colon operator vs. linspace functionENG1060: Computing for Engineers
A 2-dimensional matrix contains multiple rows and columns
Use square brackets [ ] to create a 2 dimensional matrix– MATLAB refers to the rows first, then the columns
A = [1 2 3; 4 5 6] (2 × 3 matrix)
Use the colon operator to create vectors within matricesA = [1:3; 4:6]
Creating 2D matricesENG1060: Computing for Engineers
Matrices can be concatenated to make larger matrices– Matrices can only join if one of their dimensions are the same– Dimension mismatch in MATLAB will give an error in red text
Matrix concatenation
arms =
7777
7777
body =
1001
0101
0011
dino = 9 9 99 9 9
9 99 9
9 99 9
Possible to concatenate two arms a body and a dino?X = [arms; body dino] ?X = [arms; body; dino] ?X = [arms body arms; dino] ?
ENG1060: Computing for Engineers
X = [arms body arms; dino]
Matrix concatenation: Result
X =
7 7 1 0 0 7 77 7 0 1 0 7 77 7 0 0 1 7 77 7 1 1 1 7 79 9 9 9 9 9 99 9 9 9 9 9 9
ENG1060: Computing for Engineers
Similar to the colon operator and linspace
Matrices can be created using:A = zeros(rows, columns)B = ones(rows, columns)C = eye(rows, columns)D = rand(rows, columns)
Matrix properties can be obtained using Longest_side = length(matrix)[rows, columns] = size(matrix)
Built-in functions for matricesENG1060: Computing for Engineers
We have already seen that matrices can contain a lot of information– How do we display the data? Is the entire matrix needed?
What would Y = rand(1000, 1000) print to screen?– Not important to show but important for future calculations
To suppress an output in MATLAB, place a semi-colon (;) after the command
Y = rand(1000, 1000);
Information overloadENG1060: Computing for Engineers
The semi-colon is used to suppress the printing of outputs– Recall: semi-colon creates a new row in the matrix environment [ ]
Example: We're only interested in the final resultA = 10;B = 40;C = 2*A – B + 887/B;D = A*B*C – B + 887/C
The semi-colonENG1060: Computing for Engineers
All values in a vector or matrix are assigned an address– Refer to numbers within a vector or a matrix to perform calculations– Create smaller matrices from a larger matrix
To address elements in a vectorSyntax: A(index)
Example: A = [5 10 15 20 25 30 35 40]A(1) → 5 (first element)A(4) → 20 (fourth element)A(end) → ???A(first) → ???
Matrix addressing: VectorsENG1060: Computing for Engineers
So we can use a scalar index to address an element vector
What if we want multiple values from a vector? A = [10, 20, 30, 40, 50, 60]Individually… A(1) = 10, A(4) = 40, A(5) = 50 and A(6) = 60
Alternatively, we can use an index that is a vectorA([1 4 5 6]) = [10, 40, 50, 60] any other ways?
Matrix addressing: VectorsENG1060: Computing for Engineers
Remember that– Square brackets [ ] are used to create matrices– Round brackets ( ) are used to address matrices
Example:A = [6, 9, 15, 20, 98, 241, 259]A([1, 3:5, 7]) = [6, 15, 20, 98, 259]
If elements of a vector have an index (or an address), do elements of other multi-dimensional matrices have an index?
Matrix addressing: SyntaxENG1060: Computing for Engineers
A 2D matrix has rows and columns– Therefore we give an index to the element's row and column
MATLAB takes in the row argument first, then the columnSyntax: A(row_index, column_index)
Again, vectors can be used as indices
A colon (:) by itself tells MATLAB to return either all rows or all columns
Matrix addressing: 2D matricesENG1060: Computing for Engineers
Matrix addressing: 2D matricesENG1060: Computing for Engineers
Consider the following matrixT = [7 1 5 9; 2 6 4 9; 8 9 3 9]
T =7 1 5 92 6 4 96 9 3 9
Matrix addressing: Example
What would the following commands return?D = T( :, [2 4])Z = T(end-1, [1 3 4])B = [Z(1) Z(end)]
ENG1060: Computing for Engineers
1. Introduction to ENG10602. MATLAB basics3. Matrices4. Matrix calculations and plotting5. Good programming practices6. Functions7. Input and output8. IF statements9. Loops, loops, loops…10. Debugging MATLAB programs11. Advanced functions and limitations of MATLAB12. The workings of MATLAB
Part A: MATLAB programmingENG1060: Computing for Engineers