Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
137 views

DSP - Lab 2

This document provides instructions for performing various operations in MATLAB including: 1) Creating and manipulating matrices and vectors through functions like zeros, ones, eye, diag, transpose, deletion of rows/columns, replication, and reshaping. 2) Performing basic arithmetic operations on matrices like addition, subtraction, multiplication, division, and scalar multiplication. 3) Creating and manipulating row and column vectors. 4) Performing simple math operations like exponents, multiplication, addition and subtraction on scalar values. 5) Working with complex numbers through functions like addition, subtraction, multiplication, division and evaluation of expressions involving complex values. 6) Solving example expressions and defining variables to evaluate multi-step

Uploaded by

Shahid Khan
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
137 views

DSP - Lab 2

This document provides instructions for performing various operations in MATLAB including: 1) Creating and manipulating matrices and vectors through functions like zeros, ones, eye, diag, transpose, deletion of rows/columns, replication, and reshaping. 2) Performing basic arithmetic operations on matrices like addition, subtraction, multiplication, division, and scalar multiplication. 3) Creating and manipulating row and column vectors. 4) Performing simple math operations like exponents, multiplication, addition and subtraction on scalar values. 5) Working with complex numbers through functions like addition, subtraction, multiplication, division and evaluation of expressions involving complex values. 6) Solving example expressions and defining variables to evaluate multi-step

Uploaded by

Shahid Khan
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 15

Practical Date: 10-04-2018 Submission Date: 17-04-2018

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

iv. S = [0:2:8] % single Row Vector


>> S = [0:2:8]

S=

0 2 4 6 8
v. A(n,m) % select n row’s and m columns elemnt
>> A(3,3)

ans =

vi. A(n:m) % select elements from n to m

>> A(3:3)

ans =

vii. diag(A) % return diagonal elements of a matrix


>> diag(A)

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

x. A’ % take transpose of a matrix


>> A'

ans =

1 4 7
2 5 8
3 6 9

xi. A( n , : ) = [] %deleting nth row


>> A( 2 , : )=[]

A=

1 2 3
7 8 9

xii. A( : , m ) = [] %deleting mth column


>> A( : , 3 ) = []

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

xx. length(A) % returns the length of vector X.


>> length(A)

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

ii. D=A-B % Subtraction of two matrices


>> D=A-B

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

v. G=A/B or G=A\B % Quotient of two matrices


>> G=A/B

G=

-0.000000000000000 -5.000000000000000 4.000000000000000


0 -8.000000000000000 7.000000000000000
0 -11.000000000000000 10.000000000000000

vi. H=A./B or H=A.\B


>> H=A./B

H=

0.500000000000000 Inf 3.000000000000000


1.333333333333333 2.500000000000000 6.000000000000000
1.750000000000000 2.666666666666667 4.500000000000000

vii. I=2*A or I=0.5*B % Scalar multiplication


>> I=2*A

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

ii) Create a column vector ‘b’ length of 10


>> B=[1:10]'

B=

1
2
3
4
5
6
7
8
9
10

iii) Change row vector into column vector


Use B.’ or B’
>> B.'

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

4) Simple math operations


Let a=5; b=2; c=9;
>> a=5;
>> b=2;
>> c=9;

i) a^2 =? % Square of a number


>> a^2

ans =

25

ii) c^3 =? % Cube cube of a number


>> c^3

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;

i) S= w+x % Addition of complex numbers


>> S= w+x

S=

7.000000000000000 - 4.000000000000000i

ii) P= w-y % Subtraction of complex numbers


>> P= w - y

P=

1.200000000000000 + 9.000000000000000i

iii) Q= z*x % Product of complex numbers


>> Q= z*x

Q=

31.300000000000001 -30.500000000000000i

iv) R=y/x % Quotient of complex numbers


>> R=y/x

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

2) Define the variable x as x = 13.5, then evaluate:


>> x=13.5;
a) 𝐱 𝟑 + 𝟓𝐱 𝟐 -26.7x – 52
>> x^3 + (5*x)^2 - 26.7*x - 52

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

4. Define the variable a, b, c & d as:


a = 15.62, b = -7.08, c = 62.5 & d = 0.5(ab – c)
evaluate:

>> 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

5. Create a row vector x = [2 3 5 7 11 13 17 19 23 29] in MATLAB.


>> x = [2 3 5 7 11 13 17 19 23 29]

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=

15 10 5 0 -5 -10 -15 -20 -25

8. Create a Matrix T =

>> T=[6 43 2 11 87; 12 6 34 0 5; 34 18 7 41 9]

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

You might also like