0% found this document useful (0 votes)
25 views

Chapter+2+Slides (2)

Uploaded by

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

Chapter+2+Slides (2)

Uploaded by

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

1

VECTORS AND
MATRICES
Chapter 2

Copyright © 2017 STEM Course Prep


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

 Matrices contain values of the same type


 Each value in the matrix is stored in an element
 The size of a matrix is noted by the number of rows and the
number of columns
 For m rows and n columns, we say ‘m by n’ or write m x n
 Number of rows always goes first!
 Matrix above: 4 rows, 3 columns 4 x 3 matrix
 Matrices are also known as arrays
Copyright © 2017 STEM Course Prep
3

VECTORS AND SCALARS


 A set of data with only one row or one column is a
vector
 Vectors are simply matrices that have one of the
dimensions equal to 1
 A row of data is known as a row vector
10 23 4 16 25
 1 x 5 matrix shown above. Notice the number of rows = 1
 A column of data is known as a column vector
8
 3 x 1 matrix shown here. Notice the number of
5 columns =1
1
 A 1 x 1 matrix is just a single value 21
 Known as a scalar

Copyright © 2017 STEM Course Prep


4

CREATING ROW
VECTORS
 Two methods: direct or colon operator
 Direct: Values contained in square brackets, separated
by commas or spaces

Copyright © 2017 STEM Course Prep


CREATING ROW
5

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

Copyright © 2017 STEM Course Prep


6

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)

Copyright © 2017 STEM Course Prep


7

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

Copyright © 2017 STEM Course Prep


8

CONCATENATION
• Concatenation – joining vectors together or adding
additional elements to an existing vector
• List vectors/numbers you want to combine inside
square brackets

Copyright © 2017 STEM Course Prep


9

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

Copyright © 2017 STEM Course Prep


10

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

Copyright © 2017 STEM Course Prep


11

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!

Copyright © 2017 STEM Course Prep


12

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

Copyright © 2017 STEM Course Prep


COLUMN VECTORS 13

• Colon operator is used to


create row vectors
• For column vectors,
simply create a row
vector and then
transpose it
• To transpose use
transpose function or
the apostrophe ‘

Copyright © 2017 STEM Course Prep


14

COLUMN VECTORS
Index used to Index used to
refer to elements change elements

Copyright © 2017 STEM Course Prep


15

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

Copyright © 2017 STEM Course Prep


16

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

Copyright © 2017 STEM Course Prep


17

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

mat(1,1)=9 mat(2,1)=4 mat(3,1)=6


mat(1,2)=8 mat(2,2)=17 mat(3,2)=3
mat(1,3)=29 mat(2,3)=14 mat(3,3)=21
Copyright © 2017 STEM Course Prep
18

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

mat(1,:)=9 8 29 mat(:,1)=9 mat(:,2)=8 mat(:,3)=29


mat(2,:)=4 17 14 4 17 14
mat(3,:)=6 3 21 6 3 21

Copyright © 2017 STEM Course Prep


MATRIX INDEXING 19

• Modify subsets of matrix using assignment statements

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

• end will give the last row or column of a matrix


B(end,2)=30 B(1,end)=15
Copyright © 2017 STEM Course Prep
20

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)

r= # of rows c=# of columns


Copyright © 2017 STEM Course Prep
21

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

• reshape can be used to put these elements into


matrices with the following dimensions: 1x12, 12x1, 4x3,
2x6, 6x2
• Works through the matrix columnwise
• reshape(A,1,12) gives a 1x12 matrix

Copyright © 2017 STEM Course Prep


23

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

sqrt(array)= 9.0554 9.5394 3.6056 9.5917 8.0000


3.1623 5.2915 7.4162 9.7980 9.8489

Copyright © 2017 STEM Course Prep


MIN & MAX
25

max - finds the max min – finds the min


value in a vector or in a value in a vector or the
matrix column in matrix column
>> nums=[1 -3 5 8 -4]; >> nums=[1 -3 5 8 -4];
>> max(nums) >> min(nums)
ans = ans =
8 -4
>> A=[1 5 -9;3 -1 15] >> A=[1 5 -9;3 -1 15]
A = A =
1 5 -9 1 5 -9
3 -1 15 3 -1 15
>> max(A) >> min(A)
ans = ans =
3 5 15 1 -1 -9
Copyright © 2017 STEM Course Prep
26

MIN & MAX


• To find max/min of entire matrix use function twice
>> B=randi([-10,10],2,4)
B =
-7 10 6 -2
10 0 -8 9
>> m=max(B)
m = Using max results in a vector
10 10 6 9
>> maxB=max(m) Using max again results in a scalar
maxB = that is the max value of the matrix
10
>> maxB2=max(max(B)) More efficient way to do this
maxB2 =
10
Copyright © 2017 STEM Course Prep
SUM & CUMSUM 27

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

prod - finds product of cumprod – finds the running


elements in a vector or matrix product of elements in a
column vector of matrix column
>> A=2:4:14 >> A=2:4:14
A= A=

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

Copyright © 2017 STEM Course Prep


31

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

Copyright © 2017 STEM Course Prep


32

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

inner dimensions Resulting matrix has dimensions


must be equal of outer dimensions

Copyright © 2017 STEM Course Prep


33

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

Copyright © 2017 STEM Course Prep


34

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

Copyright © 2017 STEM Course Prep


35

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

Copyright © 2017 STEM Course Prep


36

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

• Defined as 𝒂𝒂𝟐𝟐𝟏𝟏 + 𝒂𝒂𝟐𝟐𝟐𝟐 + 𝒂𝒂𝟐𝟐𝟑𝟑 for a vector 𝐚𝐚 = 𝑎𝑎1 , 𝑎𝑎2 , 𝑎𝑎3


• norm is the built-in function
>> line=[2 4 7];
>> dist=norm(line)
dist =
8.3066

Copyright © 2017 STEM Course Prep


37

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

𝑥𝑥1 𝑎𝑎11 𝑎𝑎12 𝑎𝑎13 −1 𝑏𝑏1


𝑥𝑥2 = 𝑎𝑎21 𝑎𝑎22 𝑎𝑎23 𝑏𝑏2
𝑥𝑥3 𝑎𝑎31 𝑎𝑎32 𝑎𝑎33 𝑏𝑏3

Copyright © 2017 STEM Course Prep


38

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

Copyright © 2017 STEM Course Prep


LOGICAL FUNCTIONS 40

• find – does logical test and returns indices


>> speed=[37 65 45 58 23];
>> hispeed=find(speed>55)
65 located in column 2
hispeed =
58 located in column 4
2 4
• any – logical test that returns a 1 (true) if ANY elements meet
the requirement specified
hispeed=any(speed>55)
hispeed =
1
• all – logical test that returns a 1 (true) only if ALL
elements meet specified requirement
hispeed=all(speed>55)
hispeed =
0

Copyright © 2017 STEM Course Prep


41

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

Copyright © 2017 STEM Course Prep

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy