0% found this document useful (0 votes)
34 views

Comm 502 Communication Theory Lab: Introduction To Matlab

This document provides an introduction to MATLAB and summarizes its basic functions and syntax. It covers topics such as variables, vectors, matrices, logical operators, and conditionals. MATLAB can be used to clear the command window and workspace, define different variable types including integers, vectors, matrices and strings, perform basic arithmetic and logical operations, and conditionally execute code using if/elseif statements.

Uploaded by

Mohamed Sallah
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
34 views

Comm 502 Communication Theory Lab: Introduction To Matlab

This document provides an introduction to MATLAB and summarizes its basic functions and syntax. It covers topics such as variables, vectors, matrices, logical operators, and conditionals. MATLAB can be used to clear the command window and workspace, define different variable types including integers, vectors, matrices and strings, perform basic arithmetic and logical operations, and conditionally execute code using if/elseif statements.

Uploaded by

Mohamed Sallah
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 27

Comm 502

Communication Theory Lab


Lab1
Introduction to
Matlab
Eng. Sally Nafie
Desktop:

Workspace
Command
Window

Command
History
Getting Started:

clc Clears command


window
Getting Started:

clear Clears
workspace
Getting Started:
Variables:
Syntax Description
X=6 Integer

Y=[1 2 3 4] Vector

Z=[1 3 8 7;5 2 4 3 ] Matrix

L=3+4i Complex number

M=‘hello’ String

R=Inf ∞

S=pi π
Complex Numbers:

Syntax Description Output


L=3+4i Complex number L=3+4i

A=real(L) Real part of L A=3

B=imag(L) Imaginary part of L B=4

C=abs(L) Absolute value of L C=5

D=angle(L) Radian angle of L D=0.9273 rad

M=complex(A,B) Complex number L=3+4i


with real part =A
and imaginary
part =B
N=conj(M) Complex N=3-4i
Conjugate
Trignometric Functions:
Function Inverse Hyperbolic Inverse
Hyperbolic
Y=sin(X) Y=asin(X) Y=sinh(X) Y=asinh(X)

Y=cos(X) Y=acos(X) Y=cosh(X) Y=acosh(X)

Y=tan(X) Y=atan(X) Y=tanh(X) Y=atanh(X)

Y=sec(X) Y=asec(X) Y=sech(X) Y=asech(X)

Y=csc(X) Y=acsc(X) Y=csch(X) Y=acsch(X)

Y=cot(X) Y=acot(X) Y=coth(X) Y=acoth(X)

NB: X should be in radians


Exponential Functions:

Syntax Description
a^b
ab
exp(a) ea
log10(a) log10 a

log2(a) log2 a
log(a) ln a
sqrt(a)
Vectors:
Syntax Description Output
X=[1 3 5 7 9] X=[x1 x2 x3 x4 x5] X=[1 3 5 7 9]

X=1:2:9 X=start: step: end X=[1 3 5 7 9]

Y=X(2) Y=2nd element in X Y=3

X(3)=4 Set the 3rd element in X=[1 3 4 7 9]


X to 4
Y=X(2:1:3) Y=2nd to 3rd element Y=[3 4]
in X
Y=X(1:2:end) Y=1st to last element Y=[1 4 9]
in X with a step of 2
Y=X(5:-2:2) Y=5th to 2nd element Y=[9 4 ]
in X with a step of -2
Y=X([2 3 5]) Y=2nd 3rd and 5th Y=[3 4 9]
elements in X
Vectors:
Syntax Description Output
Y=X.’ Y=transpose of X Y=[1
3
4
7
9]
Y=X’ Y= Conjugate Y=[1
transpose of X 3
in case of complex 4
numbers 7
9]
Y=X.^2 Y=[x12 x22 x32 x42 x52] Y=[1 9 16 49 81]
Vectors:
Syntax Description Output
Y=length(X) Y=number of Y=5
elements in X
Y=sum(X) Y=sum of elements Y=24
of X
Y=mean(X) Y=sum(X)/length(X) Y=24/5
=4.8
Y=find(X==4) Y=location of Y=3
element 5 in X
Y=find(X>5) Y=location of Y=[4 5]
elements >5 in X
Y=find(X>5 & X<9) Y=location of Y=[4]
elements >5 and <9
in X
Y=find(X<3 |X>5) Y=location of Y=[1 4 5 ]
elements <3and >9
in X
Matrices:
Syntax Description Output
X=[2 6 7 3;4 1 9 5] X=[x11 x12 x13 x14 X=[2 6 7 3
x21 x22 x23 x24 ] 4 1 9 5]
X=zeros(2,3) 0s matrix with 2 X=[0 0 0
rows and 3 columns 0 0 0]
X=ones(1,5) 1s matrix with 1 row X=[1 1 1 1 1 ]
and 5 columns
X=eye(3) 3x3 Identity matrix X=[1 0 0
010
0 0 1]
X=randint(2,3) 2x3 random matrix X=[0 1 1
of 1s and 0s 1 0 1]
X=randsrc(2,3) 2x3 random matrix X=[1 -1 -1
of -1s and 1s 1 -1 -1]
X=rand(2,3) 2x3 random matrix X =[0.153 0.69 0.47
between 1 and 0 0.67 0.72 0.55]
Matrices:
Syntax Description Output
Y=X(2,4) Y=The element in Y=5
the 2nd row and the
4th column in X
Y=X(1,2:4) Y=The element in Y=[6 7 3]
the 1st row and the
2nd to 4th column in X
Y=X(1:end,3) Y=The element in Y=[7
the 1st to last row 9]
and the 3rd column
in X
Y=X(1:end,2:3) Y=The element in Y=[6 7
the 1st to last row 1 9]
and the 2nd to 3rd
column in X
X(2,4)=8 Set the element in X=[2 6 7 3
the 2nd row and the 4 1 9 8]
4th column in X to 8
Matrices:
Syntax Description Output
[r,c]=size(X) Returns r=2
r=number of rows c=4
c=number of
columns in X
Y=X.’ Y=transpose of X Y=[2 4
6 1
7 9
3 5]
Y=X’ Y=conjugate Y=[2 4
transpose of X in 6 1
case of complex 7 9
numbers 3 5]
Y=X.^2 Y=The square of Y=[4 36 49 9
each element in X 16 1 81 25]
Matrices:
Syntax Description Output
X=[2 5 8;6 4 2] Defining Matrix X X=[2 5 8
6 4 2]
Y=[1 4 7;5 3 6] Defining Matrix Y Y=[1 4 7
5 3 6]
Z=[X ;Y] Concatenating X and Z=[2 5 8
Y vertically in one 6 4 2
matrix Z 1 4 7
5 3 6]
Z=[X Y] Concatenating X and Z=[2 5 8 1 4 7
Y horizontally in one 6 4 2 5 3 6]
matrix Z
Z=X.*Y Multiplying each Z=[2 20 56
element in X by its 30 12 12]
corresponding one
in Y
Matrices:
Syntax Description Output
X=[2 5 8;6 4 2] Defining Matrix X X=[2 5 8
6 4 2]
Y=[1 4 ;7 5; 3 6] Defining Matrix Y Y=[1 4
7 5
3 6]
Z=X*Y Multiplying matrix X Z=[61 81
by matrix Y 40 56]
Z=Y*X Multiplying matrix Y Z=[26 21 16
by matrix X 44 55 66
42 39 36]
[M P]=max(X) Returns M=[ 6 5 8]
M=the maximum of P=[2 1 1]
every column in X
P=the position of the
max in every column
Matrices:
Syntax Description Output
[M P]=min(X) Returns M=[2 4 2]
M=the minimum of P=[1 2 2]
every column in X
P=the position of the
min in every column
Matrices:
Syntax Description Output
X=[2 5 8;6 4 2] Defining Matrix X X=[2 5 8
6 4 2]
Y=fliplr(X) Y=Flipped columns of X Y=[8 5 2
(left-right direction) 2 4 6]
Y=flipud(X) Y=Flipped rows of X (up- Y=[6 4 2
down direction) 2 5 8]
Y=repmat(X,2,3) Y= repeated version of X Y=[2 5 8 2 5 8 2 5 8
with the rows repeated 2 642 642 642
times and the columns 258 258 258
repeated 3 times 6 4 2 6 4 2 6 4 2]

Y=reshape(X,3,2) Y= reshaped version of X Y=[2 4


with 3 rows and 2 6 8
columns 5 2]
Logical Operators:

Syntax Description Output


X=0 Defining Variable X X=0

Y=1 Defining Variable Y Y=1

Z=X & Y Z = X AND Y Z=0

Z=X | Y Z = X OR Y Z=1

Z=xor(X ,Y) Z = X XOR Y Z=1

Z=~X Z=NOT X Z=1


Conditionals:

If Statements:

Syntax Example Output


if expression1 X=1 Z=-1
statements1 Y=0
if (X==1)& (Y==1)
elseif expression2 Z=1
statements2
elseif (X==0) & (Y==1)
else Z=0
statements3
end else
Z=-1
end
Conditionals:

Switch:
Syntax Example
Output
switch switch_expr X=[1 8 7 -4 6] Y=12
case case_expr
statement,...,statement switch X(3)
case case {5}
{case_expr1,case_expr2,case_expr3,...} Y=11
statement,...,statement case {3,7}
... Y=12
otherwise otherwise
statement,...,statement Y=13
end end
Loops:
Syntax Example Output
for index = start:increment:end X=ones(1,6) X=[1 2 4 8 16 32]
statements for n = 2:1:6
end X(n) = 2 * X(n - 1)
end
while expression X=10 Y =8
statements Y=0 X=2
end while X>=3
Y=Y+1
X=X-1
end
Plotting
Syntax Description
X=[x1 x2 x3 x4 x5…………xn] Define both vectors
Y=[y1 y2 y3 y4 y5…………yn] then plot
plot(X,Y)
X=-pi:0.01*pi:pi; Define one vector and
Y=sin(X); define the other as a
plot(X,Y) function of it

xlabel(‘x’) label the x axis

ylabel(‘sin(x)’) label the y axis

title(‘Sine Wave’) Give a title

hold on /hold off Plot more than one


graph in the same
figure
figure Create a new figure

legend(‘str1’,’str2’,…..) Display a legend


Help:
Help:

Type the
functionality
you are
searching for
Notes:
• Matlab is case sensetive which means that X is not
equivalent to x.

• Typing a ; at the end of every command means not to


display the values in the command window after executing
the command.

• Typing a % before a command means it is a comment and


the program will not execute it.

You might also like