Professional Documents
Culture Documents
Tan Do
Vietnamese-German University
Lecture 2
In this lecture
Denition
Charlie 5120
Arrays play fundamental role in MATLAB.
Every information and data are represented as arrays in MATLAB.
Row vector
5 9 0 is a row vector.
h i
In MATLAB, type
» [5 9 0]
ans =
5 9 0
Commas are used to improve readability.
» [5, 9, 0]
ans =
5 9 0
1
−4 is a column vector.
7
In MATLAB, type
» [1; -4; 7]
ans =
1
-4
7
Matrices
In MATLAB, type
» [1 2; 3 4]
ans =
1 2
3 4
" #
−3 4 6
is a 2 × 3 matrix.
7 1 0
−2 8
is a 3 × 2 matrix.
0 1
9 37
Generally, a table of numbers with m rows and n columns is called an
m × n matrix.
a11 a12 a13 ... a1n
a21 a22 a23 . . . a2n
a
31 a32 a33 . . . a3n
. .. .. ..
.
. . . .
Multidimensional arrays
A multidimensional array
Tan Do (VGU) Introduction to MATLAB Lecture 2 8 / 43
Classication
Array classes
So far we have discussed arrays involving numbers only. These are called
numeric arrays.
More classes of arrays are available in MATLAB:
In this course, we will cover arrays of types [1], [2] and [6].
Tan Do (VGU) Introduction to MATLAB Lecture 2 9 / 43
Numeric arrays
Numeric arrays
A numeric array is an array whose elements are numbers.
In MATLAB there are several types of numbers.
single single-precision real number (oating point),
occupies 32 bits in computer memory
double double-precision real number (oating point),
occupies 64 bits in computer memory
int8, uint8 signed and unsigned 8-bit integer respectively
int16, uint16 signed and unsigned 16-bit integer respectively
...
The default numeric type in MATLAB is double.
It is possible to change from one numeric type to another. But this is not
advisable, unless you are told to do so.
Tan Do (VGU) Introduction to MATLAB Lecture 2 10 / 43
Numeric arrays
If we type
» v = a:b
then the step size s is set to 1.
Tan Do (VGU) Introduction to MATLAB Lecture 2 11 / 43
Numeric arrays
Example
» 0:2:10
ans =
0 2 4 6 8 10
» 0:2:9
ans =
0 2 4 6 8
» 1:0.2:2
ans =
1.0000 1.2000 1.4000 1.6000 1.8000 2.0000
» -3:2
ans =
-3 -2 -1 0 1 2
Tan Do (VGU) Introduction to MATLAB Lecture 2 12 / 43
Numeric arrays
» 6:-2:1
ans =
6 4 2
Example
» linspace(1, 2, 6)
ans =
1.0000 1.2000 1.4000 1.6000 1.8000 2.0000
If n is omitted, the spacing is 1.
Appending vectors
We can append one vector to another.
Example
» a = [1 2 3]; b = [4 5 6];
» v = [a, b]
ans =
1 2 3 4 5 6
M = [a; b]
ans =
1 2 3
4 5 6
Note the eect of the comma and the semicolon in the above example.
Tan Do (VGU) Introduction to MATLAB Lecture 2 14 / 43
Numeric arrays
The absolute value of v is a vector of the same length whose elements are
absolute values of elements in v correspondingly.
In MATLAB, type
» abs(v)
Tan Do (VGU) Introduction to MATLAB Lecture 2 15 / 43
Numeric arrays
Example
» v = [1, 0 , -1, -5]
v =
1 0 -1 -5
» norm(v)
ans =
5.1962
» length(v)
ans =
4
» abs(v)
ans =
1 0 1 5
Tan Do (VGU) Introduction to MATLAB Lecture 2 16 / 43
Numeric arrays
Let M be an m × n matrix.
The transpose M T of M is an n × m matrix whose rows are the columns
of M correspondingly.
" # 1 0
1 −1 8
For example, if M = , then M T = .
−1 4
0 4 −2
8 −2
In MATLAB, type
» M = [1, -1, 8; 0, 4, -2]
M =
1 -1 8
0 4 -2
» M’
ans =
1 0
-1 4
8 -2
Recall ' is called the transpose operator.
Array addressing
v1 = 1, v2 = −2, v3 = 3 and v4 = 7.
In MATLAB, type
» v = [1, -2, 3, 7];
v(4)
ans =
7
Note the way we index the elements of M : the rst index is the row
number and the second index is the column number.
In MATLAB, type
» M = [1, -1; 0, 9];
» M(1,2)
ans =
-1
Tan Do (VGU) Introduction to MATLAB Lecture 2 21 / 43
Numeric arrays
M(end,:) gives the last row in M and M(:,end) gives the last
column in M .
Empty array
We can delete rows and columns in a matrix M using the empty array.
Example
M(:,2) = [] deletes column 2.
M(:,3:5) = [] deletes columns from position 3 to 5.
M(:,[1, 3, 5]) = [] deletes columns in positions 1, 3 and 5.
Tan Do (VGU) Introduction to MATLAB Lecture 2 23 / 43
Numeric arrays
" #
1 7 3
Suppose M = .
5 9 6
Then typing
» M(4,2) = 21
results in
ans =
1 7 3
5 9 6
0 0 0
0 21 0
i.e., MATLAB expands the size of M and adds zeros to ll out the
remaining elements.
Element-by-element operations
Vector scaling
To change the magnitude of a vector, we multiply it by a scalar.
In MATLAB, type
» u = [5, -3, 7];
» 4*u
ans =
20 -12 28
The same idea is also applied to matrices. For example,
» M = [2, 3; 5, -1];
» 2*M
ans =
4 6
10 -2
Tan Do (VGU) Introduction to MATLAB Lecture 2 28 / 43
Matrix operations
Dot product
u · v = u1 v1 + u2 v2 + . . . + un vn .
Example
» u = [1,2,3]; v = [5,2,0];
» dot(u,v)
ans =
9
Cross product
u × v = [u2 v3 − u3 u2 , u3 v1 − u1 v3 , u1 v2 − u2 v1 ].
Example
» u = [1,2,3]; v = [5,2,0];
» cross(u,v)
ans =
-6 15 -8
Matrix multiplication
Example
" # 3 0 " #
1 2 3 2 33
∗ −2 9 = .
4 5 6 8 75
2×3 1 5 2×2
3×2
" # " #
1 h i 7 3
∗ 7 3 = .
2 1×2 14 6
2×1 2×2
In MATLAB, type
» A = [1, 2, 3; 4, 5, 6]; B = [3, 0; -2, 9; 1, 5];
» A*B
ans =
2 33
8 75
» u*v
ans =
7 3
14 6
Special matrices
The zero matrix has all entries equal to 0. We denote the zero matrix by 0.
For example,
" # " # 0 0 0
0 0 0 0 0
, , 0 0 0 , ...
0 0 0 0 0
0 0 0
0A = A0 = 0, IA = AI = A
Command Description
eye(n) creates an n × n identity matrix
zeros(n) creates an n × n zero matrix
zeros(m,n) creates an m × n zero matrix
zeros(size(A)) creates a zero matrix the same size as matrix A
ones(n) creates an n × n matrix whose entries are all 1's
ones(m,n) creates an m × n matrix whose entries are all 1's
ones(size(A)) creates a matrix of 1's the same size as matrix A
Character encoding
char arrays
Denition A sequence of characters is called a char array or a string.
An empty string can be created by putting two single quotes together (with no spacing
in between).
s = ”;
c = class(s)
c =
char
sprintf function
Example
» Intro = sprintf(’Hello! My name is %s.’, ’Tan’)
Intro =
Hello! My name is Tan.
An approximation of pi is 3.14.