Tutorial Sheet - 2 Matrix Operation
Tutorial Sheet - 2 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)
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)
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)
A =
1. 0. 0. 0.
2. Description: - zeros(m,n) returns an m-by-n matrix of zeros.
c) ones (3,4)
ans =
1 1 1 1
1 1 1 1
1 1 1 1
ans =
A=
1 2 3
4 5 6
7 8 9
i) diag (A)
ans =
Description: - diag (A) returns the main diagonal of the matrix A, in column form.
ii) 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.
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.
ans =
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)
Description: - diag(A,2) for matrix A, returns a column vector formed from the elements
of the 2nd diagonal of A.
d=
1 2 3
4 5 6
7 8 9
d=
5 6 8
2 3 1
4 8 5
d1 =
4 5 6
3 2 1
0 8 9
d2 =
9 8 5
2 3 1
5 6 7