DSP - Lab 2
DSP - Lab 2
DSP - Lab 2
LAB # 2
Creation and Operations on matrices and vectors
Vectors and Matrices:-
MATLAB has been designed to work with matrices. A matrix is a rectangular object (e.g. a table) consisting
of rows and columns. A vector is a special type of matrix, having only one row or one column.
1) Createing matrices
i. A=[ 1 2 3; 4 5 6; 7 8 9]
>> A=[ 1 2 3; 4 5 6; 7 8 9]
A=
1 2 3
4 5 6
7 8 9
ii. A= [1,2,3;4,5,6;6,7,8]
>> A= [1,2,3;4,5,6;6,7,8]
A=
1 2 3
4 5 6
6 7 8
iii. A= [1 2 3(enter) 4 5 6(enter) 7 8 9]
>> A= [1 2 3
456
7 8 9]
A=
1 2 3
4 5 6
7 8 9
S=
0 2 4 6 8
v. A(n,m) % select n row’s and m columns elemnt
>> A(3,3)
ans =
>> A(3:3)
ans =
ans =
1
5
9
viii. A(:,n) or A(n,:) % to display elements of nth row of a matrix
>> A(:,3)
ans =
3
6
9
>> A(3,:)
ans =
7 8 9
ix. A(:) % to display all the elements of matrix in one coloumn.
>> A(:)
ans =
1
4
7
2
5
8
3
6
9
ans =
1 4 7
2 5 8
3 6 9
A=
1 2 3
7 8 9
A=
1 2
4 5
7 8
xiii. repmat(A,m,n) % creates a large matrix consisting of an m-by-n tiling of copies of A.
>> repmat(A,3,2)
ans =
1 2 3 1 2 3
4 5 6 4 5 6
7 8 9 7 8 9
1 2 3 1 2 3
4 5 6 4 5 6
7 8 9 7 8 9
1 2 3 1 2 3
4 5 6 4 5 6
7 8 9 7 8 9
xiv. reshape(A,m,n) returns the m-by-n matrix whose elements are taken column wise from A.
>> A=[ 1 2 3; 4 5 6]
A=
1 2 3
4 5 6
>> reshape(A,3,2)
ans =
1 5
4 3
2 6
xv. zeros(n) or zeros(m,n) % to create null matrix of n by n or m by n
>> zeros(3)
ans =
0 0 0
0 0 0
0 0 0
>> zeros(2,3)
ans =
0 0 0
0 0 0
xvi. ones(n) or ones(m,n) % to create n by n or m by n matrix of ones
>> ones(3)
ans =
1 1 1
1 1 1
1 1 1
xvii. eye(n) or eye(m,n) % to create an identity matrix of n by n or m by n
>> eye(2)
ans =
1 0
0 1
xviii. fliplr(A) % returns A with the order of elements flipped left to right(Horizentally)
>> fliplr(A)
ans =
3 2 1
6 5 4
9 8 7
xix. flipud(A) % returns A with the order of elements flipped upside down
>> flipud(A)
ans =
7 8 9
4 5 6
1 2 3
ans =
3
xxi. size(A) % for matrix A, returns the number of rows and columns in A
>> size(A)
ans =
3 3
xxii. magic(N) % is an N-by-N matrix constructed from the integers 1 through N^2 with equal
row, column, and diagonal sums.
>> magic(4)
ans =
16 2 3 13
5 11 10 8
9 7 6 12
4 14 15 1
2) Operation on matrices:
i. C=A+B % Addition of two matrices
>> A=[ 1 2 3; 4 5 6; 7 8 9];
>> B=[ 2 0 1; 3 2 1; 4 3 2];
>> C=A+B
C=
3 2 4
7 7 7
11 11 11
D=
-1 2 2
1 3 5
3 5 7
iii. E=A*B % Product of two matrices
>> E=A*B
E=
20 13 9
47 28 21
74 43 33
iv. F=A.*B
>> F=A.*B
F=
2 0 3
12 10 6
28 24 18
G=
H=
I=
2 4 6
8 10 12
14 16 18
3) Vectors:
Perform the following operations
i) Create a row vector ‘a’ of length 10
>> A=1:10
A=
1 2 3 4 5 6 7 8 9 10
B=
1
2
3
4
5
6
7
8
9
10
ans =
1 2 3 4 5 6 7 8 9 10
iv) Create a row vector ‘c’ that has the elements: 32, 4, 81, 63, cos(pi/3) and 14.12
>> C=[32,4,81,63,cos(pi/3),14.12]
C=
32 4 81 63 0.500 14.1199
v) Create a column vector ‘d’that has the elements: 55, 14, log(51), 987, 0 & 5sin(2.5pi)
>> D = [55;14;log(51);987;0;5*sin(2.5*pi)]
D=
1.0e+02 *
0.550000000000000
0.140000000000000
0.039318256327243
9.869999999999999
0
0.050000000000000
ans =
25
ans =
729
iii) a*b+c^2= ?
>> a*b+c^2
ans =
91
iv) a^3+ b*c= ?
>> a^3+ b*c
ans =
143
5) Complex number:
Let w= 2 + 3*j y= 0.8 – i*6 z= j*0.9 +5
>> w= 2 + 3*j;
>> x= 5 - 7*i;
>> y= 0.8 - i*6;
>> z= j*0.9 + 5;
S=
7.000000000000000 - 4.000000000000000i
P=
1.200000000000000 + 9.000000000000000i
Q=
31.300000000000001 -30.500000000000000i
R=
0.621621621621622 - 0.329729729729730i
Lab Tasks:
1) Calculate:
𝟑𝟓.𝟕∗𝟔𝟒−𝟕𝟑
a.
𝟒𝟓+ 𝟓𝟐
>> (35.7*64-7^3)/(45+ 5^2 )
ans =
27.740000000000002
𝟓 𝟑𝟐
b. ∗ 𝟕 ∗ 𝟔𝟐 + (𝟗𝟑 −𝟔𝟓𝟐)
𝟒
>> (5/4)*(7)*(6^2)+((3^2)/((9^3)-652))
ans =
3.151168831168831e+02
𝟐𝟕𝟑𝟐/𝟑 𝟓𝟓𝟐
c. (𝟐 + 𝟕)𝟑 + +
𝟐 𝟑
>> (2+7)^3+(273^(2/3)/2)+((55^2)/3)
ans =
1.758374917600349e+03
ans =
6.604175000000000e+03
√𝟏𝟒 𝐱 𝟑
b)
𝐞𝟑𝐱
>> e=2.7184;
>> (sqrt(14*x^3))/e^3*x
ans =
1.247263905499257e+02
c) 𝐥𝐨𝐠|𝐱 𝟐 − 𝐱 𝟑 |
>> log(abs(x^2- x^3))
ans =
7.731108015197023
3. Define the variable x & z as x = 9.6, & z=8.1, then evaluate:
>> x=9.6;
>> z=8.1;
𝟑
𝟐 𝟐𝒛 𝟓
a) 𝒙𝒛 − (𝟑𝒙)
>> (x)*((z)^2)-(2*z/3*x)^(3/5)
ans =
6.191703979639052e+02
𝟒𝟒𝟑𝒛 𝒆−𝒙𝒛
b) +
𝟐𝒙𝟑 (𝒙+𝒛)
>> e=2.7184;
>> ((443*z)/(2*x^3 )) + (e^(-x*z)/(x+z))
ans =
2.027893066406250
>> a=15.62;
>> b=-7.08;
>> c=62.5;
>> d = 0.5*(a*b - c)
d=
-86.544799999999995
𝑎𝑏 (𝑎+𝑑)2
a) 𝑎 + ( )( )
𝑐 √|𝑎𝑏|
>> a+((a*b)/c)*((a+d)^2)/(sqrt(abs(a*b)))
ans =
-8.307755393430498e+02
𝑎𝑑+𝑐𝑑
𝑑 20 30
(2 ) +
𝑎 𝑏
b) 𝑑𝑒 +
𝑎+𝑏+𝑐+𝑑
>> e=2.7184;
>> d*(e^(d/2))+((a*d+c*d)/(20/a + 30/b))/(a+b+c+d)
ans =
-1.474699657132773e+02
x=
2 3 5 7 11 13 17 19 23 29
Now use the colon operator to display only the even entries of the vector, that is, the output should
be the vector 3 7 13 19 29.
>> X=x(2:2:end)
X=
3 7 13 19 29
Now do the same thing but display the output in reverse order. The output should be 29 19 13 7 3.
Use
>> X=x(end:-2:2)
X=
29 19 13 7 3
6. Create a row vector in which the first element is 1, the last element is 33, with an increment of 2
between the elements (1,3,5,…….,33)
>> Y=[1:2:33]
Y=
1 3 5 7 9 11 13 15 17 19 21 23 25 27 29 31 33
7. Create a row vector in which the first element is 15, the elements decrease with increments
of -5 and the last elements is -25
>> Z=[15:-5:-25]
Z=
8. Create a Matrix T =
T=
6 43 2 11 87
12 6 34 0 5
34 18 7 41 9
Use the matrix T to:
a) Create a five-element row vector named ‘va’ that contains the elements of the second row of T.
>> va=T(2,:)
va =
12 6 34 0 5
>> va = [12 6 34 0 5]
va =
12 6 34 0 5
b) Create a three-element row vector named ‘vb’ that contains the elements of the fourth column of T.
>> vb=T(:,4)
vb =
87 5 9
>> vb = [87 5 9]
vb =
87 5 9
c) Create a ten-element row vector named ‘vc’ that contains the elements of the first and second rows
of T.
>> vc=[T(1,:) T(2,:)]
vc =
6 43 2 11 87 12 6 34 0 5
d) Create a six-element row vector named ‘vd’ that contains the elements of the second and fifth
columns of T.
>> vd = [T(:,2); T(:,5)]’
vd =
87 5 9 43 6 18
e) Create a three-element column vector named ‘ua’ that contains the elements of the third column of
T.
>> ua = T(:,3)
ua =
2
34
7
f) Create a five-element column vector named ‘ub’ that contains the elements of the second row of T.
>> ub = T(2,:)’
ub =
12
6
34
0
5
g) Create a nine-element column vector named ‘uc’ that contains the elements of the first, third and
fifth columns of T.
>> uc = cat(1,T(:,1),T(:,3),T(:,5))
uc =
6
12
34
2
34
7
87
5
9
h) Create a ten-element column vector named ‘ud’ that contains the elements of the first and second
rows of T.
>> ud = [T(1,:)’;T(2,:)’]
ud =
6
43
2
11
87
12
6
34
0
5