Vous êtes sur la page 1sur 4

COMPUTER SECURITY LAB 01

Introduction to MATLAB

Objective:
The purpose of this lab is to present basics of MATLAB. The main objectives are:
1. Get familiar with GUI of the software.
2. Implementation of different commands and statements.
3. Using the help command to explore a function
4. Programming in MATLAB
5. Creating and executing m-files.
6. Plotting.

Introduction:
 MATLAB (Matrix laboratory) is an interactive software system for numerical computations and
graphics.
 A numerical analyst called Cleve Moler wrote the first version of Matlab in the 1970s. It has since
evolved into a successful commercial software package.
 As the name suggests, Matlab is especially designed for matrix computations: solving systems of
linear equations, computing eigen values and eigenvectors, factoring matrices, and so forth.
 In addition, it has a variety of graphical capabilities, and can be extended through programs written
in its own programming language. Many such programs come with the system; a number of these
extend Matlab's capabilities to nonlinear problems, such as the solution of initial value problems
for ordinary differential equations.

Characteristics of MATLAB:
1. Slow (compared with fortran or C) i.e. not pre-compiled. Avoid for loops; instead use vector form.
2. Automatic memory management, i.e., you don't have to declare arrays in advance.
3. Shorter program development time than traditional programming languages such as Fortran and C.
4. Can be converted into C code via MATLAB compiler for better efficiency.
5. Many application-specific toolboxes available.
6. Main toolboxes which are going to be used are: communication toolbox, signal processing toolbox
and other supplement tool boxes.

Simple commands and statements:


 Arithmetic operations and variables.
 Matrix and vectors.
 Matrix operations and point wise operations.
 Element extraction from a matrix.
 Basic functions and special matrices
 help, who, whos and clear commands
 Creating m files and basic programs

DEPARTMENT OF COMPUTER SYSTEM ENGINEERING


1
DAWOOD UNIVERSITY OF ENGINEERING & TECHNOLOGY, KARACHI.
COMPUTER SECURITY LAB 01

Try the following codes:


Open a new m file and name it prog1 then execute the following commands:

%Always use comments

a = 5; % numeric variable
b = [1 2 3]; % A row vector which also can be written as b = [1,2,3]
c = [4; 5; 6]; % A column vector which also can be written as c = [4 5 6]' where ' indicates
the transpose of a matrix
d = [2 4 5;3 3 3; 1 2 7]; %matrix creation 3 by 3
f = [] ; %to create an empty matrix
g = ones(3,4); %3by4 matrix with all ones
h = zeros(6); %create 6by6 matrix of all zeros
size(g); %finds the size of g matrix
size(g,1); %finds number of rows in g
size(g,2); %finds number of columns in g
length(b); %number of elements in b
length(c); %number of elements in c
length(g); %number of elements in a single row in g
k = randint(3,4); %3by4 randomly generated 0s and 1s.
l = randint(4,4,[3,5]); %4by4 randomly generated integer numbers from 3 to 5.
m = randsrc(4,4,[3:5]); %same as l
n = randsrc(4,4,[3,5]); %4by4 randomly generated integer numbers of 3 and 5 only.
o = rand(2,2]); %2by2 randomly generated numbers between 0 and 1 uniformly distributed.
p = magic(5); %5by5 magic matrix. See the details of this matrix in the detailed help. Note
that the magic matrix can only be a square matrix

%Operations on matrices

q = 2*g %scalar multiplication


r = b + c' %Addition
s=g*m %matrix multiplication
t = b' .* c %element wise multiplication
u = o .^ 4 %each element in o is raised to power 4
det(m) %the determinant of m
v = eye(4) %identity matrix of 4by4

%Element extraction

m(2,2) %extracts the element intersection at 2nd row 2nd column in m


m(1:3,2) %extracts the elements at 1 to 3 rows and 2nd column in m in this way using the
range sign : , you can extract any element or elements depending on the
intersections
m(:,3) %means extract the whole 3rd column
m(1,:) %means extract the whole 1st row
m(:,:) %extracts the whole matrix m
m(:) %arrange m in a single column vector
w = [b' c d] %concatenate different matrices into a single one such that the resultant matrix
forms an acceptable shape.
z = [4:0.1:5] %creates a row vector having an increment of 0.1
diag(l) %extracts the diagonal elements of the square matrix

DEPARTMENT OF COMPUTER SYSTEM ENGINEERING


2
DAWOOD UNIVERSITY OF ENGINEERING & TECHNOLOGY, KARACHI.
COMPUTER SECURITY LAB 01

%Basic functions

sum(d)
mean(d)
max(d)
min(d)
sin(d)
cos(d)
exp(d)
log(d)
%for more built in functions, see the help on list of functions

whos %detailed information about the variables


clear d m %clears the variables d and m
whos %confirm that m and d are cleared
clear all %clears all currently assigned variables

%Basic plotting

t = [0:0.001:10];
y = sqrt(100 - t.^2);
plot(t,y) %plot y vs t
hold on %to hold the previous plot
plot(t,-y,'r') %'r' means red color
hold on
plot(-t,-y,'g')
hold on
plot(-t,y,'k')

Some basic programming. Open a new m file and name it prog2 then execute the following
commands

function res = prog2( v ) %function definition. This function can be called from
%any place after passing it the argument v
%such that v is a row vector (in this %example)

for b=1:size(v,2) %for loop


ind(b) = v(b) * randint(1);
end %terminate the loop with end

if (ind-v == 0) %if-else condition


display('all ones sequence') %to display a text or value
else
display('not all zeros sequence')
end %to terminate the if-else or if condition

%Explore different plotting commands and other useful commands from the help menu.

DEPARTMENT OF COMPUTER SYSTEM ENGINEERING


3
DAWOOD UNIVERSITY OF ENGINEERING & TECHNOLOGY, KARACHI.
COMPUTER SECURITY LAB 01

Assignment:
Perform the following tasks:
1. Rewrite prog2 without the for loop
2. A system is performing an encryption operation on a text data using the figure shown below.
The letters are mapped into bits in the following manner: the letter is converted into its
corresponding sequence number in the English alphabets and then that number is converted
into binary representation. A key in binary form with the same length of bits as the text is
generated randomly. Display the encrypted message in text form. The text to encrypt is your
complete name e.g. Engr Motia Rani. Hint: discard the spaces between name and the
capitalization of the first letter.

DEPARTMENT OF COMPUTER SYSTEM ENGINEERING


4
DAWOOD UNIVERSITY OF ENGINEERING & TECHNOLOGY, KARACHI.

Vous aimerez peut-être aussi