Professional Documents
Culture Documents
Chapter - 4: Matrix Operations
Chapter - 4: Matrix Operations
MATRIX OPERATIONS
Vectors and Matrices
>> Z = [2,4,6,8]
Z =2 4 6 8
>> Y = [4 -3 5 -2]
Y =4 -3 5 -2
2
Adding Two Row Matrices:
Z = [2 4 6 8]
Y = [4 -3 5 -2]
>>X=Z+Y
X=
6 1 11 6
Combination of vectors to form another vector:
W =[Z,Y]
W=
2 4 6 8 4 -3 5 -2
3
The linear combination of Z and Y:
>> C=4*Z+6*Y
C =32 -2 54 20
sort(W)
ans = -3 -2 2 4 4 5 6 8
min(W)
ans =-3
max(W)
ans =8
4
sum(W) sin(sum(W)) mean(W)
log(W)
ans =
Columns 1 through 3
Columns 4 through 6
Columns 7 through 8
Randn: Generates an array with elements randomly chosen from the normal
distribution function with zero mean and standard deviation 1.
randn
ans =
0.1253
6
Column Vectors
The column vectors in matlab are formed by using a set of
numbers in a pair of square brackets and seperating them
with semi-colon. Therefore, one can define two column
vectors A and B and add them as below:
A=
2
3
1
B=
4
6
5
C=
6
9
6
7
The Notation:
n order to form a vector as a sequence of numbers, one may use the colon
notation. According to which, a:b:c yields a sequence of numbers starting with
‘a’, and possibly ending with ‘c’ in steps of ‘b’.
8
Here’s how to do it without typing each number:
>> X = [1:9]
X=
1 2 3 4 5 6 7 8 9
>> X = 0:2:10
X=
0 2 4 6 8 10
Note that in some cases, the upper limit may not be attainable thing.
For example;
>> D= 1:0.3:3
D=
1.0000 1.3000 1.6000 1.9000 2.2000 2.5000 2.8000
Example;
>> 1:-1:5
ans =
Empty matrix: 1-by-0
>> 5:-1:1
ans =
5 4 3 2 1 10
Sections of a Vector:
>> W=[1:3,7:9]
W =
1 2 3 7 8 9
>> W(6:-1:1)
ans =
9 8 7 3 2 1
>> H=1:2:19
H=
1 3 5 7 9 11 13 15 17 19
>> H(3:2:8)
ans =
5 9 13
12
Example
>> M=[3,2,7]; N =[6,8,9]; M+N , M-N , M*N
ans =
9 10 16
ans =
-3 -6 -2
As we can see, M+N and M-N are calculated but M*N can not be calculated.
Because the summation and substraction can be performed when the marix
dimensions are the same. For example, 3x3 matrix can be sum or substract by
only 3x3 matrix. But in multiplication, the number of first vector column must be
equal to the number of second vector row. For example, (2x3) by (3x1) this
results 2x1 vector as shown below.
13
>> M=[3,2,7;2,3,4], N =[6;8;9], M*N
M=
3 2 7
2 3 4
N=
6
8
9
ans =
97
72
14
If you don’t want M an N be seen in the window , we can write by
using semicolon instead of comma:
ans =
97
72
15
Transpose
We can convert a column vector into a row vector (and vice versa)
by a process called transposing denoted by '.
>> c = [ 1; 3; sqrt(5)],c‘
c=
1.0000
3.0000
2.2361
ans =
1.0000 3.0000 2.2361
16
>> w= [3 -5 9], c = [ 1; 3; sqrt(5)],t = w + 2*c'
w=
3 -5 9
c=
1.0000
3.0000
2.2361
t=
5.0000 1.0000 13.4721
>> T = 5*w'-2*c
T=
13.0000
-31.0000
40.5279
17
If x is a complex vector, then x' gives the complex conjugate
transpose of x:
x=
1.0000 + 3.0000i 2.0000 - 2.0000i
ans =
1.0000 - 3.0000i
2.0000 + 2.0000i
Example :
>> A=[1,3,6;2,7,8;0,3,9]
18
A=
1 3 6
2 7 8
0 3 9
>> size(A)
ans =3 3
>> A‘
ans =
1 2 0
3 7 3
6 8 9
19
Arrays and Matrices, Basic Information
Length Length of vector or largest array dimension
Max Largest elements in array
Min Smallest elements in array
Size Array dimensions
Eye Identity matrix
Zeros Create array of all zeros
Cross Vector cross product
Dot Vector dot product
Sum Sum of array elements
Sort Sort array elements in ascending or descending order
Det Matrix determinant
Rank Rank of matrix
İnv Matrix inverse
Eig Eigenvalues and eigenvectors
Sqrtm Matrix square root
Expm Matrix exponential
Logm Matrix logarithm
20
Working on Matrices
Multi-Dimensional Arrays
5 7 8 2 3 6
A = [5 7 8; 0 1 9; 4 3 6] 0 1 9 1 7 8
A(:,:,2) = [1 0 4; 3 5 6; 9 8 7] 4 3 6 4 3 9
A(:,:,3) = [2 1 4; 3 7 6; 4 8 7]
1 0 4 1 3 4
B =[2,3,6;1,7,8;4,3,9] 3 5 6 3 2 6
B(:,:,2) = [1 3 4; 3 2 6; 9 5 7] 9 8 7 9 5 7
B(:,:,3) = [5 2 4; 3 7 6; 5 3 7] 2 1 4 5 2 4
3 7 6 3 7 6
4 8 7 5 3 7
21
Dot product (dot)
C = dot(A,B)
C = dot(A,B,dim)
C(:,:,1) =
26 37 174
Definition: Let a and b be two vectors in
Rn, then the dot product of a and b is the
scalar a · b given by
C(:,:,2) =
a · b = a1b1 + a2b2 + a3b3 + · · · + anbn
91 50 101
C(:,:,3) =
39 75 101
23
C = dot(A,B)
C(:,:,1) =
26 37 174
C(:,:,2) =
91 50 101
C(:,:,3) =
39 75 101
24
C = dot(A,B,3)
5 1 2-------from A
2 1 5-------from B
+ ___________________
10+1+10=21
7 0 1
3 3 2
+__________
21+0+2=23
8 4 4
6 4 4
+__________
48+16+16=80
C=
21 23 80
18 66 144
117 73 152
25
Product of array elements(prod)
D = prod(A)
D = prod(A,dim)
D = prod(A) returns the products along different dimensions of an array.
If A is a vector, prod(A) returns the product of the elements.
C = prod(A)
C=[ 0 21 432]
Vector cross product(cross)
C = cross(A,B)
C = cross(A,B,dim)
28
Example
The cross product of two vectors are calculated as shown:
>> a = [1 2 3];
b = [4 5 6];
Definition: If a = ha1, a2, a3i and b =
hb1, b2, b3i, then the cross product of a and
b is the vector
c = cross(a,b)
a × b = (a2b3 − a3b2, a3b1 − a1b3, a1b2 − a2b1)
c=
NOTE: The cross product is only defined for
-3 6 -3 vectors in .
c = cross(b,a)
29
Matrix determinant(det)
d = det(X)
30
The determinant of a matrix A is denoted det(A), det A, or |A|.
In the case where the matrix entries are written out in full, the determinant is denoted
by surrounding the matrix entries by vertical bars instead of the brackets or
parentheses of the matrix. For instance, the determinant of the matrix
is written
A=
1 2 3
4 5 6
7 8 9
One way to solve this is with x = inv(A)*b. A better way, from both an
execution time and numerical accuracy standpoint, is to use the matrix
division operator x = A\b. This produces the solution using Gaussian
elimination, without forming the inverse.
33
Example . Matrix Inverse Operation
1 1 1
A 2 0 1
1 1 1
1 1 1 1 0 0
2 0 1 0 1 0
1 1 1 0 0 1
We add -2 times of first row to second row and -1 times of first row to third row
1 1 1 1 0 0
0 2 1 - 2 1 0
0 2 0 - 1 0 1
Secos row is divided by -2 and we add the result with times 2 to third row.
1 1 1 1 0 0
0 1 1 / 2 1 - 1/2 0
0 0 1 1 - 1 1
We add -1/2 times of third row to second row and -1 times of third row to first row
1 1 0 0 1 -1
0 1 0 1/2 0 - 1/2
0 0 1 1 - 1 1
1 0 0 - 1/2 1 - 1/2
0 1 0 1/2 0 - 1/2
0 0 1 1 - 1 1
We obtain inverse matrix of A. Left side is unit matrix. Right side is inverse matrix. (A-1)
- 1/2 1 - 1/2
1
A 1/2 0 - 1/2
1 - 1 1
>> A = [1 2 3; 4 5 6]; B=inv(A)
??? Error using inv
36
Sum of array elements(sum)
B = sum(A)
B = sum(A,dim)
B = sum(..., ‘double’)
B = sum(..., dim,’double’)
B = sum(..., ‘native’)
B = sum(..., dim,’native’)
sum(M’) =
15 15 15
sum(M,1)
ans =
15 15 15
40
Length of vector or largest array dimension(length)
numberOfElements = length(array)
Number Of Elements = length(array) finds the number of elements along the largest
dimension of an array. Array is an array of any matlab data type and any valid dimensions.
numberOfElements is a whole number of the matlab double class.
Example
Create a 1-by-8 array X and use length to find the number of elements in the second
(largest) dimension:
length(X)
ans = 8 41
Largest elements in array(max)
C = max(A)
C = max(A,B)
C = max(A,[],dim)
[C,I] = max(...)
If A is a multidimensional array, max(A) treats the values along the first non-
singleton dimension as vectors, returning the maximum value of each vector.
42
C = max(A,B) returns an array the same size as A and B with the
largest elements taken from A or B. The dimensions of A and B must
match, or they may be scalar.
C = max(A,[],dim) returns the largest elements along the dimension
of A specified by scalar dim. For example, max(A,[ ],1) produces the
maximum values along the first dimension (the rows) of A.
[C,I] = max(...) finds the indices of the maximum values of A, and
returns them in output vector I. If there are several identical maximum
values, the index of the first one found is returned.
43
Smallest elements in array(min)
C = min(A)
C = min(A,B)
C = min(A,[],dim)
[C,I] = min(...)
C = min(A) returns the smallest elements along different dimensions of an
array.
If A is a vector, min(A) returns the smallest element in A.
[C,I] = min(...) finds the indices of the minimum values of A, and returns
them in output vector I. If there are several identical minimum values, the index
of the first one found is returned.
45
Average or mean value of array (mean)
M = mean(A)
M = mean(A,dim)
M = mean(A,dim) returns the mean values for elements along the dimension
of A specified by scalar dim. For matrices, mean(A,2) is a column vector
containing the mean value of each row.
46
Examples
A = [1 2 3; 3 3 6; 4 6 8; 4 7 7];
mean(A)
ans =
3.0000 4.5000 6.0000
mean(A,2)
ans =
2.0000
4.0000
6.0000
6.0000
47
Median value of array(median)
M = median(A)
M = median(A,dim)
Examples
A = [1 2 4 4; 3 4 6 6; 5 6 8 8; 5 6 8 8]
A=
1 2 4 4
3 4 6 6
5 6 8 8
5 6 8 8
median(A)
ans =
4 5 7 7
median(A,2)
ans =
3
5
7
7 49
Identity matrix(eye)
Y = eye(n)
Y = eye(m,n)
Y = eye([m n])
Y = eye(size(A))
Y = eye(m, n, classname)
Y = eye(n) returns the n-by-n identity matrix.
Examples
>> x = eye(3,5,'int8')
x=
1 0 0 0 0
0 1 0 0 0
0 0 1 0 0
51
Matrix square root(sqrtm)
X = sqrtm(A)
[X, resnorm] = sqrtm(A)
[X, alpha, condest] = sqrtm(A)
X=
5 -4 1 0 0
-4 6 -4 1 0
1 -4 6 -4 1
0 1 -4 6 -4
0 0 1 -4 5
This matrix is symmetric and positive definite. Its unique positive definite
square root, Y = sqrtm(X), is a representation of the second difference operator.
Y=
2.0000 -1.0000 0.0000 0.0000 0.0000
-1.0000 2.0000 -1.0000 0.0000 -0.0000
0.0000 -1.0000 2.0000 -1.0000 0.0000
0.0000 0.0000 -1.0000 2.0000 -1.0000
0.0000 -0.0000 0.0000 -1.0000 2.0000 53
Matrix exponential (expm)
Y = expm(X)
Y = expm(X) computes the matrix exponential of X.
Examples
This example computes and compares the matrix exponential of A and the
exponential of A.
54
A = [1 1 0
0 0 2
0 0 -1 ];
expm(A)
ans =
2.7183 1.7183 1.0862
0 1.0000 1.2642
0 0 0.3679
exp(A)
ans =
2.7183 2.7183 1.0000
1.0000 1.0000 7.3891
1.0000 1.0000 0.3679
Notice that the diagonal elements of the two results are equal.
This would be true for any triangular matrix. But the off-diagonal
elements are different
55
Matrix logarithm (logm)
L = logm(A)
[L, exitflag] = logm(A)
L = logm(A) is the principal matrix logarithm of A, the inverse of expm(A).
L is the unique logarithm for which every eigenvalue has imaginary part lying
strictly between -π and π. If A is singular or has any eigenvalues on the negative
real axis, the principal logarithm is undefined. In this case, logm computes a non-
principal logarithm and returns a warning message.
[L, exitflag] = logm(A) returns a scalar exitflag that describes the exit
condition of logm: