Lecture 4
Lecture 4
Lecture 4
Identity Matrix
Zero Matrix
One’s Matrix
diag
rot90
fliplr
flipud
tril
triu
reshape
Identity Matrix: A square matrix (n×n) is known as
Identity matrix whose all diagonal elements are “1” and
off-diagonal elements are “0”.
It is usually represented by notation 𝐼𝑛 and satisfy
following condition
𝐴𝐼𝑛 = 𝐼𝑛 𝐴 = 𝐴 where A is n×n matrix and In is n×n
identity matrix
Used frequently in matrix algebra wherever applicable
1 0 0
𝐼= 0 1 0
0 0 1
Syntax:
eye => returns the scalar, 1.
>> I =eye
I=
1
eye (m)=> creates identity matrix of (m×m)
>> I =eye (2)
I=
1 0
0 1
Syntax:
eye(m,n)=> creates a matrix of m-by-n with ones on
the main diagonal and zeros elsewhere
>> I = eye(2,3)
I=
1 0 0
0 1 0
eye(size(A)) => creates a identity matrix of size of
matrix A
>> A=[1 2; 3 4];
>> P= eye(size(A))
P=
1 0
0 1
Zero Matrix: A matrix having all elements equal to “0”
is called Zero Matrix. Used frequently in matrix algebra
wherever applicable
0 0 0
𝐼= 0 0 0
0 0 0
Syntax:
zeros => returns the scalar, 0.
>> I =zeros
I=
0
zeros(m)=> creates zero matrix of (m×m)
>> I = zeros (2)
I=
0 0
0 0
zeros (m,n)=> creates a matrix of m-by-n with all
elements “0”
>> I = zeros(2,3)
I=
0 0 0
0 0 0
Syntax:
zeros (size(A)) => creates a zero matrix of the size of matrix “A”
>> A=[1 2; 3 4];
>> P=zeros(size(A))
P=
0 0
0 0
1 1 1
𝐼= 1 1 1
1 1 1
Syntax:
ones => returns the scalar, 1.
>> I =ones
I=
1
ones(m)=> creates a matrix of (m×m) whose all elements are “1”
>> I = ones (2)
I=
1 1
1 1
ones (m,n)=> creates a matrix of m×n whose all elements are “1”
>> I = ones(2,3)
I=
1 1 1
1 1 1
Syntax:
ones (size(A)) => creates a one’s matrix of the size of matrix “A”
>> A=[1 2; 3 4];
>> P=ones(size(A))
P=
1 1
1 1
1 2 3 4 5
6 7 8 9 10
𝐴= 11 12 13 14 15
16 17 18 19 20
21 22 23 24 25
k=-1
k=-2
diag
>> A=[1 2 3 4 5; 6 7 8 9 10; 11 12 13 14 15; 16 17 18 19 20; 21 22 23 24 25];
>> u=diag(A,1)
u= k=1
2
8 1 2 3 4 5
14
20 6 7 8 9 10
>> v=diag(A,-1)
v= 𝐴= 11 12 13 14 15
6 16 17 18 19 20
12
18
24 21 22 23 24 25
k=-1
diag
diag (v)=> create a square matrix whose diagonal
elements are the elements of the vector “v” and rest of
the elements are zero
>> v=[1 2 3];
>> P=diag(v)
P=
1 0 0
0 2 0
0 0 3
rot90
rot90 (A)=> rotates the matrix anti-clockwise by 90o
>> A=[1 2 3; 4 5 6; 7 8 9];
>> P=rot90(A)
1 2 3
P= 𝐴= 4 5 6
3 6 9 7 8 9
2 5 8
1 4 7
rot90
rot90 (A,k)=> rotates the matrix by “k*90o” anti-clockwise for
k =1,2,3,4 and clockwise for k=-1,-2,-3 and -4
>> A=[1 2 3; 4 5 6; 7 8 9];
>> P=rot90(A,3)
P=
7 4 1 1 2 3
8 5 2 𝐴= 4 5 6
9 6 3 7 8 9
>> Q=rot90(A,-3)
Q=
3 6 9
2 5 8
1 4 7
fliplr
fliplr (A)=> flip the column’s of the matrix A from left to right and right
to left
>> A=[ 1 2 3 ;
4 5 6 ;
7 8 9 ];
>> P=fliplr(A)
P=
3 2 1
6 5 4
9 8 7
>> A=[ 1 2 3 4 ;
5 6 7 8 ];
>> Q=fliplr(A)
Q=
4 3 2 1
8 7 6 5
flipud
flipud (A)=> flip the row’s of the matrix A from top to
bottom and bottom to top
>> A=[ 1 2 3 ;
4 5 6 ;
7 8 9 ];
>> P=flipud(A)
P=
7 8 9
4 5 6
1 2 3
flipud
flipud (A)=> flip the row’s of the matrix A from top to bottom
and bottom to top
>> A=[1 2;
3 4;
5 6;
7 8];
>> Q=flipud(A)
Q=
7 8
5 6
3 4
1 2
tril
tril (A)=> returns the lower triangular portion of
matrix A
>> A=[1 2 3; 4 5 6; 7 8 9]
>> P=tril(A)
P=
1 0 0
4 5 0
7 8 9
tril
tril (A,k)=> returns the elements on and below the kth
diagonal of A
>> A=[1 2 3; 4 5 6; 7 8 9]
>> P=tril(A,1)
P=
1 2 0
4 5 6 k=1
7 8 9
>> Q=tril(A,-1)
Q=
0 0 0
4 0 0
7 8 0
k=-1
triu
triu (A)=> returns the upper triangular portion of
matrix “A” including diagonal
>> A=[1 2 3;
4 5 6;
7 8 9]
>> P=triu(A)
P=
1 2 3
0 5 6
0 0 9
triu
triu (A,k)=> returns the elements on and above the kth
diagonal of A
>> A=[1 2 3; 4 5 6; 7 8 9]
>> P=triu(A,1)
P=
0 2 3
0 0 6 k=1
0 0 0
>> Q=triu(A,-1)
Q=
1 2 3
4 5 6
0 8 9
k=-1
reshape
A is matrix of m-by-n
reshape (A, p,q)=> returns the matrix “A” of p-by-q with
condition that m*n = p*q
>> A=[ 1 2 3;
4 5 6;
7 8 9;
10 11 12];
>> P=reshape(A,2,6)
P=
1 7 2 8 3 9
4 10 5 11 6 12
reshape
>> A=[1 2 3;
4 5 6;
7 8 9;
10 11 12];
>> Q=reshape(A,6,2)
Q=
1 8
4 11
7 3
10 6
2 9
5 12