Arrays: Vietnamese-German University
Arrays: Vietnamese-German University
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
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 / 41
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 lecture, we will cover most classes, except for Java arrays.
Tan Do (VGU) Introduction to MATLAB Lecture 2 9 / 41
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 / 41
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 / 41
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 / 41
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 / 41
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 / 41
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 / 41
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.
If the matrix M has complex entries, the transpose operator returns the
conjugate transpose of M .
For example,
» M = [1+i, -2, 4; 3, 3-4i, 10]
M =
1.0000 + 1.0000i -2.0000 + 0.0000i 4.0000 + 0.0000i
3.0000 + 0.0000i 3.0000 - 4.0000i 10.0000 + 0.0000i
» M’
ans =
1.0000 - 1.0000i 3.0000 + 0.0000i
-2.0000 + 0.0000i 3.0000 + 4.0000i
4.0000 + 0.0000i 10.0000 + 0.0000i
To obtain a transpose without complex conjugation, you can use the dot
transpose operator .' instead.
» M.’
ans =
1.0000 + 1.0000i 3.0000 + 0.0000i
-2.0000 + 0.0000i 3.0000 - 4.0000i
4.0000 + 0.0000i 10.0000 + 0.0000i
Note that a row vector of length n can be considered as a 1 × n matrix.
Similarly, a column vector of length n can be considered as a n × 1 matrix.
Tan Do (VGU) Introduction to MATLAB Lecture 2 19 / 41
Numeric arrays
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 22 / 41
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 24 / 41
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.
Variable editor
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 30 / 41
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
Matrix division
6x + 12y + 4z = 70
7x − 2y + 3z = 5
2x + 8y − 9z = 64
where
6 12 4 x 70
A=
7 −2 3 ,
x=
y
and b =
5 .
2 8 −9 z 64
In MATLAB, type
» A = [6, 12, 4; 7, -2, 3; 2, 8, -9]; b = [70; 5; 64];
» x = A \ b
x =
3
5
-2
You should check to see that the above is indeed the solution to the given
system.
The machinery behind this left division method will be covered in later
lecture.
It is important to note that this method only works for system with unique
solution.
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