Chapter+2+Slides (2)
Chapter+2+Slides (2)
VECTORS AND
MATRICES
Chapter 2
MATRICES
10 15.7 23.1 Experimental data is recorded in tabular
20 18.2 25.8 format as shown to the left
30 19.2 26.9 There are rows and columns of data
This gives a matrix
40 19.8 28.3
CREATING ROW
VECTORS
Two methods: direct or colon operator
Direct: Values contained in square brackets, separated
by commas or spaces
VECTORS
Colon operator
Creates values in the form
first:step:last
Example: To get a vector
that starts at 5, ends at 20,
in steps of 5, use: 5:5:20
If a step size is not
specified, a step size of 1
will be used
To decrement numbers use
a step size of -1
LINSPACE
• linspace is used when evenly spaced data points
are needed
• Usage: linspace(x,y,n)
• Gives n data points in the range of x to y
• If n is omitted 100 data points are created
• linspace(x,y)
LOGSPACE
• logspace creates a logarithmically spaced vector
• Usage: logspace(x,y,n)
• Gives n data points in the range of 10𝑥𝑥 to 10𝑦𝑦
• If n is omitted 50 data points generated
CONCATENATION
• Concatenation – joining vectors together or adding
additional elements to an existing vector
• List vectors/numbers you want to combine inside
square brackets
REFERRING TO ELEMENTS
• Values in a matrix/vector are stored as elements
• Vector elements are numbered sequentially from
left to right or top to bottom
• The value of an element’s row or column number is
the index
• Use the index to refer to elements – this allows
individual matrix/vector values to be used
1 7
1 2 3 4 5 2 9
index
10 12 14 16 18 numbers 3 13
REFERRING TO ELEMENTS
• To use the index, type the
vector name followed by
the index in parenthesis
• To reference more than one
element use a index vector
• Index vector lists the
element numbers in
brackets
• Colon operator can be
used
MODIFYING
ELEMENTS
Once vector has been
created, individual
values can be
changed using index
Need to add data to
your vector?
Extend the vector to
include new data
Notice gaps in data
are filled in with zeros
This method is
inefficient!
COLUMN VECTORS
• m x 1 vector is a column
vector
• Two methods to create:
Direct and colon
operator with transpose
• Direct Method: values are
separated by semi-colons
in square brackets
• Semi-colons indicate
when to move to next
row
COLUMN VECTORS
Index used to Index used to
refer to elements change elements
CREATING MATRICES
• Will have m rows and n
columns – m x n matrix
• Values within rows
separated by spaces or
commas
• Direct method or
colon method can be
used to form rows
• Must have same
number of elements in
each row!!!
• Semi-colons separate
the rows
CREATING MATRICES
• Many built-in functions to create matrices
• Random number generators:
• rand(n)-generates square nxn matrix of random real
numbers in the open interval (0,1)
• rand(n,m)-generates nxm matrix of random real numbers
in the open interval (0,1)
• randi([range],n,m) -generates nxm matrix of random
integers in the range specified by the closed interval
• Zeros and ones:
• zeros(n) -generates square nxn matrix of zeros
• zeros(n,m) -generates nxm matrix of zeros
• ones(n) -generates nxn matrix of ones
• ones(n,m) -generates nxm matrix of ones
• Why might we want this? We’ll cover it later on
MATRIX ELEMENTS
• Subscripted indexing – referring to an element of the matrix
• Row first, column second – ALWAYS!
1 2 3
1 9 8 29
2 4 17 14
3 6 3 21
Index numbers
MATRIX ELEMENTS
• Use a colon (:) to refer to an entire row or column
• : indicates all rows or columns
• mat(m,:) – refers to the entire mth row
• : indicates all columns – all columns of the mth row
• mat(:,n) – refers to entire nth column
• : indicates all rows – all rows of the nth column
1 2 3
1 9 8 29
2 4 17 14
3 6 3 21
12 0 0
B = 102 108 0
0 30 120
12 0 10
′
B : , 3 = 10 20 30 gives B = 102 108 20
0 30 30
5 10 15
B 1, : = 5: 5: 15 gives B = 102 108 20
0 30 30
MATRIX DIMENSIONS
• Functions to determine the dimensions of a vector or matrix:
• length
• Vector: returns the # of elements
• Matrix: returns the larger dimension (row or column)
• Usage: length(name)
• numel
• Returns number of elements in vector or matrix
• Usage: numel(name)
• size
• Returns the number of rows and columns for a vector or
matrix
• Returns two values – # of rows, # of columns
• Must assign variables to both these values
• Usage: [r c] = size(mat)
MATRIX DIMENSIONS
• Examples:
7 5 3
A=
9 5 1
length(A)=3
numel(A)=6
[r c]=size(A) r=2 c=3
C= 6 4 3 8 7
length(C)=5
numel(C)=5
[r c]=size(A) r=1 c=5
Copyright © 2017 STEM Course Prep
22
CHANGING DIMENSIONS
• Functions to change matrix dimensions
• reshape – converts a matrix to a new matrix with
different dimensions but the same number of elements
1 5 3 4
𝐴𝐴 = 8 5 7 18
6 12 23 14
• A has 12 elements
CHANGING DIMENSIONS
• Other functions:
• rot90 rotates a matrix 90 degrees counter-
clockwise
• fliplr flips columns of a matrix from left to
right (about a vertical line)
• flipud flips rows of a matrix up to down
(about a horizontal line)
• repmat replicates a matrix; creates m x n
copies of the matrix
Copyright © 2017 STEM Course Prep
24
ARRAYS AS FUNCTION
ARGUMENTS
• Let’s say we have a 1x10 array of data and we need to
find the square root of each element in the array
• Easy to do in MATLAB
• Use the array as a function argument
• Operation will be performed on every element
array=randi([1,100],1,10)
array =
82 91 13 92 64 10 28 55 96 97
cumsum (cumulative
sum – returns the sum of sum) – gives the running
all elements in a vector sum of vector elements
or matrix column or matrix column
C = D =
2 4 6 8 3 6 9 12
>> sum(C) >> cumsum(D)
ans = ans =
20 3 9 18 30
matC = matD =
1 4 7 10 6 -10 4
3 5 7 6 10 7 5
>> sum(matC) 3 9 5
ans = >> cumsum(matD)
4 9 14 16 ans =
>> sum(sum(matC)) 6 -10 4
4+5
ans = 16 -3 9
43 19 6 14 9+5
Copyright © 2017 STEM Course Prep
PROD & CUMPROD 28
2 6 10 14 2 6 10 14
>> prod(A)
>> cumprod(A) 12*10
ans =
ans =
2 12 120 1680
1680 2*6*10*14
>> matA=randi([1,10],3,4)
>> matA=randi([1,10],3,4) matA =
matA = 4 8 1 7
4 8 1 7 7 1 1 4
7 1 1 4 2 3 9 10
2 3 9 10 >> cumprod(matA)
ans =
>> prod(matA)
ans =
4 8 1 7 7*4
28 8 1 28
56 24 9 280 7*4*10 28*10
56 24 9 280
Copyright © 2017 STEM Course Prep
29
>> matnums=randi([-5,5],2,5)
matnums =
-3 -3 2 5 -4
SCALAR 3 0 4 1 -4
OPERATIONS >> matnums*-1
Need to ans =
perform an 3 3 -2 -5 4
operation on -3 0 -4 -1 4
every element >> matnums+10
in a matrix? ans =
Easy to do 7 7 12 15 6
using math 13 10 14 11 6
operators
Copyright © 2017 STEM Course Prep
30
>> A=randi([1,5],2,2)
A =
2 2
5 5
>> B=randi([1,5],2,2)
MATRIX MATH
B =
2 2
o Matrix Addition & 5 1
Subtraction >> A+B
o Performed element ans =
by element
4 4
o Must have the same
dimensions in each 10 6
matrix >> B-A
ans =
0 0
0 -4
MATRIX MATH
• Use the following operators for element by element
matrix operations
• Multiplication - .*
• Division - ./
• Exponentiation - .^
• Notice the use of the dot (.) before the regular
operator
• Indicates element by element operations instead of
standard matrix multiplication
a = 3 6 5 b = 3 5 4
>> a.*b
ans =
9 30 20
MATRIX MATH
• Matrix multiplication is not the same as element by element
multiplication
• * is the matrix multiplication operator
• Let A have dimensions m x n and B have dimensions n x q
• A*B=C
• C has dimensions m x q
• mxn*nxq=mxq
MATRIX MULTIPLICATION
• Dot product of rows and columns used to multiply
matrices
• Example:
12 11
1 3 5 82 73
× 10 9 =
2 4 6 112 100
8 7
1*12+3*10+5*8 = 82
VECTOR OPERATIONS
• Dot product of vectors 𝐚𝐚 = 𝑎𝑎1 , 𝑎𝑎2 , 𝑎𝑎3 and 𝐛𝐛 = [𝑏𝑏1 , 𝑏𝑏2 , 𝑏𝑏3 ]
defined as 𝑎𝑎1 ∗ 𝑏𝑏1 + 𝑎𝑎2 ∗ 𝑏𝑏2 + 𝑎𝑎3 ∗ 𝑏𝑏3
• dot is built-in function for this
>> a=[1 2 3];
>> b=[4 5 6];
>> c=dot(a,b)
c =
32
• Uses: finding angle between two lines, finding projection
on a line, etc.
• dot works for vectors with more than 3 elements
VECTOR OPERATIONS
• Cross product is used extensively in engineering
• Most commonly used to find perpendicular components
• Finding moments
• Calculating the vector of a line perpendicular to a surface,
etc.
• cross is the built-in function
• Only works with 3-element vectors
• For 2-D vectors make the third component equal to zero
>> a=[1 2 3]; >> d=cross(a,b)
>> b=[4 5 6]; d =
-3 6 -3
VECTOR OPERATIONS
• Magnitude of vectors is commonly found by engineers
• Examples
• To calculate speed from a velocity vector, you find the
magnitude of the velocity vector
• If you have the vector for the line between two points, finding
the magnitude of the line vector gives the distance between
the points
VECTOR OPERATIONS
• Matrix inverse
• Operator ^-1
• Useful for solving systems of equations
• Example: Solve the following system of equations for
𝑥𝑥1 , 𝑥𝑥2 , 𝑥𝑥3
𝑎𝑎11 𝑥𝑥1 + 𝑎𝑎12 𝑥𝑥2 + 𝑎𝑎13 𝑥𝑥3 = 𝑏𝑏1
𝑎𝑎21 𝑥𝑥1 + 𝑎𝑎22 𝑥𝑥2 + 𝑎𝑎23 𝑥𝑥3 = 𝑏𝑏2
𝑎𝑎31 𝑥𝑥1 + 𝑎𝑎32 𝑥𝑥2 + 𝑎𝑎33 𝑥𝑥3 = 𝑏𝑏3
LOGICAL VECTORS
• Recall: a logical results in a 0 or 1
• 0 for FALSE
• 1 for TRUE
• Logicals can be used on vectors to find elements meeting
certain requirements
• Example: a vector containing speeds is given. We need to
find speeds greater than 55
>> speed=[37 65 45 58 23]
speed =
37 65 45 58 23
>> speed>55
ans=
0 indicates speed at that index is not > 55
0 1 0 1 0
1 indicates speed is >55
Copyright © 2017 STEM Course Prep
39
LOGICAL VECTORS
• Want to pull out the actual values instead of getting 0s
and 1s?
• Use the logical vector from the logical test to index into
vector
speed=[37 65 45 58 23]
vals=speed>55
vals =
0 1 0 1 0 Logical vector
>> hispeed=speed(vals)
hispeed =
65 58
ISEQUAL
• Need to compare two arrays and get a true/false
answer?
• isequal - compares two arrays and returns a 0 or 1
• 0 – arrays not equal
• 1 – arrays are equal
>> accel1=[2.5 5.6 7.3 0.4];
>> accel2=[2.5 5.7 7.3 0.4];
>> isequal(accel1,accel2)
ans =
0 5.6 ≠ 5.7