Tutorial Sheet - 2
Questions on Matrix Operation: -
1. Commands →
a) eye (2,3)
b) A=zeros(1,4)
c) ones (3,4)
d) rand (2,4)
e) Execute following commands for given matrix A,
1 2 3
A= 4 5 6
7 8 9
i) diag (A)
ii) diag (A,1)
iii) diag (A, -1)
iv) diag (A,3)
v) diag (A,2)
2. Perform the expression given below →
D = diag (d) + diag (d₁,1) + diag (d₂, -2)
Where: -
5 6 8 9 8 5 4 5 6
d = 2 3 1 d₂ = 2 3 1 d₁ = 3 2 1
4 8 5 5 6 7 0 8 9
SOLUTIONS
1. Commands →
a) eye (2,3)
Syntax >> eye (2,3)
ans =
1 0 0
0 1 0
Description: - Y = eye(m,n) returns an m-by-n matrix with 1's on the diagonal and 0's
elsewhere. The size inputs m and n should be nonnegative integers. Negative integers are treated
as 0.
b) A=zeros(1,4)
Syntax --> A=zeros(1,4)
A =
1. 0. 0. 0.
2. Description: - zeros(m,n) returns an m-by-n matrix of zeros.
c) ones (3,4)
Syntax >> ones (3,4)
ans =
1 1 1 1
1 1 1 1
1 1 1 1
Description: - ones(m,n) returns an m-by-n matrix of ones.
d) rand (2,4)
Syntax >> rand (2,4)
ans =
0.8147 0.1270 0.6324 0.2785
0.9058 0.9134 0.0975 0.5469
Description: - r = rand(m,n) returns an m-by-n matrix containing pseudorandom values
drawn from the standard uniform distribution. The all elements of the resultant matrix are
positive.
e) Execute following commands for given matrix A,
Syntax >> A = [1 2 3;4 5 6;7 8 9]
A=
1 2 3
4 5 6
7 8 9
i) diag (A)
Syntax >> diag (A)
ans =
Description: - diag (A) returns the main diagonal of the matrix A, in column form.
ii) diag (A,1)
Syntax >> diag (A,1)
ans =
Description: - diag(A,1) for matrix A, returns a column vector formed from the elements
of the 1st diagonal of A.
iii) diag (A, -1)
Syntax >> diag (A, -1)
ans =
Description: - diag(A, -1) for matrix A, returns a column vector formed from the elements
of the 1st diagonal of A ,but from opposite side.
iv) diag (A, 3)
Syntax >> diag (A,3)
ans =
Empty matrix: 0-by-1
Description: - diag(A,3) for matrix A, returns a column vector formed from the elements
of the 3rd diagonal of A, but here it is not available, so resultant is empty matrix.
v) diag (A,2)
Syntax >> diag (A,2)
ans =
Description: - diag(A,2) for matrix A, returns a column vector formed from the elements
of the 2nd diagonal of A.
2. Perform the expression given below →
D = diag (d) + diag (d1,1) + diag(d2, -2)
syntax >> d = [ 1 2 3;4 5 6;7 8 9]
d=
1 2 3
4 5 6
7 8 9
syntax >> d = [ 5 6 8;2 3 1;4 8 5]
d=
5 6 8
2 3 1
4 8 5
syntax >> d1 = [ 4 5 6;3 2 1;0 8 9]
d1 =
4 5 6
3 2 1
0 8 9
syntax >> d2 = [ 9 8 5;2 3 1;5 6 7]
d2 =
9 8 5
2 3 1
5 6 7
Syntax >> D = diag(d) + diag(d1,1) + diag(d2,2)
Matrix dimensions must agree.