Académique Documents
Professionnel Documents
Culture Documents
111
Numerical Methods for
Chemical Engineering Analysis
WEEK 2
Vectors and Matrices
Matrices
This matrix is 2 x 3
(2 rows by 3 columns)
a11
a12
a21
a22
"
"
am1 am2
! a1n $
&
! a2n &
& = aij (i = 1,, m
"
" &
! amn &%
j = 1,, n)
5
88
3
11
a column vector with n elements is n x 1
3
column vector with
7
dimensions 3 x 1
= [1
4]
2 3 4
= [1,2,3,4]
2
Ex 1) vec1=5:3:14
Ex 2) vec2=2:4
Ex 3) vec3=4:-1:1
Ex 4) vec4=1:2:8
ConcatenaGon
Vectors can be created by joining exisGng vectors and
valuables, or adding elements to exisGng vectors
This is called concatena3on
For example:
>> v = 2:5;
Try this!
>> x = [33 11 2];
>> w = [v x]
>>w=[v; x]
w =
>>w=[v; x 44]
2 3 4 5 33 11 2
>> newv = [v 44]
newv =
2 3 4 5 44
vec5 =
5
8
11
14
1 2 3
mat1 = 4 5 6
7 8 9
1 4 7
mat2 = 2 5 8
3 6 9
vec3
mat3 =
vec4
5 8 11 14 44
mat4 =
2 3 4 5 6
Referring to Elements
The elements in a vector are numbered sequenGally
each element number is called the index, or subscript
1
2
3
4
5
index (subscript)
5
33
11
-4
2
elements
An element in a vector can be accessed by using its index or
subscript in parentheses
Ex) vec(4) is the 4th element of a vector vec (assuming it has at least 4
elements)
index vector which is a vector of indices e.g. vec([m n]) refers to the
mnd and nth elements of vec
! The indices dont need to be sequenGal
Matrix Elements
To refer to an element in a matrix, you use the matrix variable
name followed by the index of the row, and then the index of
the column, in parentheses
>> mat = [1:3; 6 11 -2]
mat =
1 2 3
6 11 -2
Refers to the value in the
>> mat(2,1)
2nd row, 1st column
ans =
6
Ex)
>>mat(1:2,2:3)
*refers to the 1st&2nd row,
2nd&3rd column
Matrix Indexing
To refer to the last row or column use end
mat(end,m) is the mth value in the last row
Can modify an element or subset of a matrix in an
assignment statement
Linear indexing: only using one index into a matrix
(MATLAB will unwind it column-by column)
MATLAB stores matrices in memory in column
major order (columnwise)
" 11 23 35 %
Ex) mat = $
' element
# 62 114 26 & index
Modifying Vectors
Elements in a vector can be changed
Ex) >>vec5(3) = 11
A vector can be extended by referring to elements that do not
yet exist
If there is a gap between the end of the vector and the new
specied element(s), zeros are lled in
Ex) >> vec7 = [3 9];
Original vec7 has 2 elements
>> vec7(4:6) = [33 2 7]
We add index 4 to 6, thus 0 is
vec7 =
lled in index3
3 9 0 33 2 7
*Extending vectors is not a good idea if it can be avoided
Modifying Matrix
An individual element in a matrix can be modied
by assigning a new value
An enGre row or column can be changed by using
the colon operator
Any subset of a matrix can be modied as long as
assigned subset has the same number of rows and
columns
Matrix Dimensions
There are several funcGons to determine the dimensions of a vector or
matrix:
length(vec) returns the # of elements in a vector
length(mat) returns the larger dimension (row or column) for a matrix
size returns the # of rows and columns for a vector or matrix
To capture the values of the size, put a vector of 2 variables on the ler of an
assignment statement
[r c] = size(mat)
# of element in a vector
length, numel
# of element of row and column in a matrix
size
mat2 =
#
!
!
#
#" [ mat1] !
&
!
&
mat1
[
] &
%
Empty Vectors
An empty vector is a vector with no elements
an empty vector can be created using square brackets with nothing
inside [ ]
An element can be added in an empty vector
Empty vectors can also be used to grow a vector, starGng with
nothing and then adding to the vector by concatenaGng values to it
(usually in a loop, which will be covered later)
This is not ecient, however, and should be avoided if possible
3D Matrices
A three dimensional matrix has dimensions m x n x p
m: # of rows, n: # of columns, p: # of layers
38
13
25
17
10
1
Layer 1
Layer 2
Copyright 2013 Elsevier Inc. All rights
reserved
20
Try this!
>>mat3 = randi([-10 10], 3)
>>sin(mat3)
Note: the result is a scalar when the argument is a vector; the result is a 1 x n
vector when the argument is an m x n matrix
Scalar operaGons
Numerical operaGons can be performed on every element in
a vector or matrix
For example, Scalar mul3plica3on: mulGply every element by
a scalar
>> [4 0 11] * 3
ans =
12 0 33
Array OperaGons
Array opera3ons on two matrices A and B:
these are applied term-by-term, or element-by-element
this means the matrices must have the same dimensions
In MATLAB:
matrix addiGon: A + B
matrix subtracGon: A B or B A
Matrix MulGplicaGon
The elements of the matrix C are found as follows:
the sum of products of corresponding elements in
the rows of A and columns of B
cij =
EX)
aik bkj
k =1
! 1 2 3 1 $
! 3 8 0 $ #
& ! 35 46 17 19 $
#
&# 4 5 1 2 & = #
&
" 1 2 5 % # 0 2 3 4 & " 9 22 20 5 %
"
%
Vector OperaGons
Since vectors are just special cases of matrices,
the matrix operaGons described including
addiGon, subtracGon, scalar mulGplicaGon,
mulGplicaGon, and transpose work on vectors as
well, as long as the dimensions are correct
Specic vector operaGons:
The dot product or inner product of two vectors a and
b is dened as a1b1 + a2b2+ a3b3 + + anbn
built-in funcGon dot to do this
Logical Vectors
Using relaGonal operators on a vector or matrix results in a logical
vector or matrix
Logical Indexing
can use this to index into a vector or matrix (only if the index vector
is the type logical)
>> vec(logv)
ans =
44 9 11
True/False
false equivalent to logical(0)
true equivalent to logical(1)
false and true are also funcGon that create matrices
of all false or true values
EX1) >>false(3)
EX2) >>true(1,7)
ans =
0
0
0
ans =
0
0
0
0
0
0
Comparing Arrays
The isequal funcGon compares two arrays,
and returns logical true if they are equal (all
corresponding elements) or false if not
>> v1 = 1:4;
>> v2 = [1 0 3 4];
>> isequal(v1,v2)
ans =
0
>> v1 == v2
ans =
1 0 1 1
>> all(v1 == v2)
ans =
0
Equality operator ==
will return 1 or 0 for
each element
all funcGon will return
all elements are equal
or not