Lecture 2 Arrays
Lecture 2 Arrays
Chemical Engineering
LECTURE 2
FUNDAMENTALS: ARRAYS
ÖZGE KÜRKÇÜOĞLU-LEVİTAS
Introduction
1-D array:
A list of numbers arranged in a row or a column.
Any list of numbers can be set up as a vector.
In MATLAB: A vector is created by assigning the elements of
the vector to a variable.
This can be done in several ways depending on the source of
information
Creating 1-D Array (Vector)
Row vector: To create a row vector, type the elements with a space or a
comma (,) between elements inside the square brackets.
Column vector: To create a column vector, type the left square bracket [ and
then enter the elements with a semicolon (;) between them, or press enter
key after each element. Type the right square bracket ] after the last element.
>> r_vector=[1 2 3 4 5]
r_vector =
1 2 3 4 5
>> c_vector=[1;2;3;4;5]
c_vector =
1
2
3
4
5
Creating 1-D Array (Vector)
Vector in which the first term is m, the spacing is q, and the last
term is n is created by typing:
variable_name = [ m:q:n ] or variable_name = m:q:n
If the numbers are so that the value n cannot be obtained by adding q’s to m,
Last element in the vector is last number that does not exceed n.
If only two numbers (first and list terms) are typed,
The default for spacing is 1.
>> A=[0:2:6]
A=
0 2 4 6
>> A=0:2:6
A=
0 2 4 6
>> A=0:2:5
A=
0 2 4
>> A=0:6
A=
0 1 2 3 4 5 6
Creating 1-D Array (Vector)
x=
y=
x = a : s : b;
the number of subintervals within [a,b] is obtained by rounding (b-a)/s,
down to the nearest integer,
N = floor((b-a)/s);
length(x) is equal to N+1
x(n) = a + s*(n-1),
n = 1,2,...,N+1
How to call array entries?
>> y(4)
ans =
3.4000
clock, date
factorial(n), nthroot(n)
sin(x), cos(x), tan(x), cot(x)
asin(x), acos(x), atan(x), acot(x)
sinh(x), cosh(x), tanh(x), coth(x)
asinh(x), acosh(x), atanh(x), acoth(x)
c = a + b → ei = ai + bi i = 1, . . . , n
d = a − b → fi = ai − ci i = 1, . . . , n
Example:
Example:
Linear Combinations
Vector Dot Product
• Syntax:
C = dot(A,B) returns the scalar product of the vectors A and B.
A and B must be vectors of the same length.
When A & B are both row vectors, dot(A,B) is same as A*B’.
• Example
a = [1 2 3]; b = [4 5 6];
c = dot(a,b)
c = 32 Corresponds to: sum(a.*b)
Vector Inner Product
Matrix
Address of an element in a matrix
Its position defined by the row number and the column number
For a matrix named ma, ma(k,p) refers to the element in row k and column p.
Ex:
A=
3 2 1
5 1 0
2 1 7
ans =
0
Using a Colon
For a matrix:
A(:,n) : Refers to the elements in all the rows of column n of the matrix A.
A(n,:) : Refers to the elements in all the columns of row n of the matrix A.
A(m:n,p:q) : Refers to the elements in rows m through n and columns p
through q of the matrix A.
New vectors and matrices can be created from existing ones by using a range of
elements, or a range of rows and columns (using :)
It is also possible to select only specific elements, or specific rows and columns
of existing variables to create new variables.
This is done by typing the selected elements or rows or columns inside brackets.
: means from … to …
A=
3 2 1
5 1 0
2 1 7
Must be done carefully since the size of the added rows/columns must fit the
existing matrix.
If a matrix has a size of m x n, and a new value is assigned to an element with
an address beyond the size of the matrix, MATLAB increases the size of the
matrix to include the new element.
Zeros are assigned to the other elements that are added.
Deleting Elements
Multiplication by a Scalar
c = σ * a → ci,j = σ * ai,j i = 1, . . , m & j = 1, . . , n
• Matrix–vector product
looks like
• Vector–matrix product
looks like
• Matrix–matrix product
looks like
Matrix Multiplication
• When identity matrix multiplies another matrix (or vector), that matrix
(or vector) is unchanged.
AI = A and IA = A for any compatible matrix A
• This is like multiplying by 1 in scalar arithmetic
Matrix Inverse
Matrix B is the inverse of matrix A (A-1), if, when the two matrices are
multiplied, the product is the identity matrix.
A matrix times its inverse is the identity matrix
A* A-1 = 1
To have an inverse, a matrix must be square
MATLAB offers two approaches
The matrix inverse function:
inv(A)
Raising a matrix to the -1 power:
A-1
Matrix Inverse
Determinants
>> A'
ans =
1 2 0
2 0 8
3 4 5
Examples
A= B=
1 2 3 1 3 1
2 0 4 4 9 5
0 8 5 2 7 2
>> A’ % transpose
>> B*A % matrix multiplication
>> B.*A % element by element multiplication
>> B/A % matrix division
>> B./A % element by element division
>> [B A] % Join matrices (horizontally)
>> [B; A] % Join matrices (vertically)
A= B=
1 2 3 1 3 1 >> B./A
2 0 4 4 9 5 ans =
>> B*A 0 8 5 2 7 2 1.0000 1.5000 0.3333
ans = 2.0000 Inf 1.2500
7 10 20 Inf 0.8750 0.4000
22 48 73
16 20 44 >> [B A]
ans =
>> B.*A 1 3 1 1 2 3
ans = 4 9 5 2 0 4
1 6 3 2 7 2 0 8 5
8 0 20
0 56 10 >> [B;A]
ans =
>> B/A 1 3 1
ans = 4 9 5
11.5000 -5.2500 -2.5000 2 7 2
34.5000 -15.2500 -7.5000 1 2 3
25.5000 -11.7500 -5.5000 2 0 4
0 8 5
A=
1 2 3
2 0 4
0 8 5
>> A.^2
ans =
1 4 9
4 0 16
0 64 25
>> C=[1 2; 3 4] >> D= 10.^C
C= D=
1 2 10 100
3 4 1000 10000