MATLAB (Matrix Laboratory) Tutorial
COMMAND WINDOW executes one instruction and provides the output there itself, When there is a
requirement of individual instruction execution then use command window.
SCRIPT FILE Executes the set of instructions (Program) at once and provides its corresponding output in the
command window, after finishing the execution of the entire program.
WORK SPACE is window which stores information of all the variables being used along with dimension of the
variables.
SCALAR OPERATIONS
Command window Operations and remarks / Notes
>> a=2.5 1. Variable names are case sensitive.
a = 2.5000 2. Variable name should not start with number
>> B=4.5 ; 3. Variable name should not contain any spaces
>> C=6e+3 4. Variable name should not contain signs : +, - , * , /
C = 6000 5. ; Symbol suppresses output display
>> D=a+B >> E=B-a Here addition and subtraction operation of for the above assigned variables is
D=7 E=2 shown.
Similarly try for multiplication and division by using symbols *, / respectively.
>> F=3+4i Representation of complex numbers.
F = 3.0000 + 4.0000i
SOME USEFUL COMMANDS
>> who Lists out all variables in the work space. >> clear a B Clears the variables a & B.
>> whos Lists out all variables and their respective >> clear all Clears all the variables from the work
sizes. space but does not clear the command
window screen.
>> clc Clears the Command window. >> close all Closes all the Simulink models.
PERMANENT ans » Default result variable Inf » infinity i.e 1 / 0
VARIABLES pi » π Pre-defined variable NaN » Not a number Eg. 0/0 , inf / inf
CHARACTER STRINGS Try this example
My_Name = 'XYZ' f = 71; c = (f-32)/1.8;
disp ( 'College name is = ' ) temp = [ 'Temperature is ', num2str(c), 'C']
MATHEMATICAL OPERATORS
sin(pi/4), cos(pi/6), tan(pi/2) sinh(pi/4), cosh(pi/4) Trigonometric functions where angles to be in radians.
asin (0.5), acos(0.5), atan ( - 0.8 ) Inverse trigonometric functions (arc sine, arc cosine, etc)
Log(2), exp(2), sqrt(2), abs (1+4i), angle(1+4i) Other math functions
RELATIONAL OPERATORS LOGICAL OPERATORS
>> 3< 4 Less than symbol >> 3 >= 4 » Greater than or equal to & » Logical AND
>> 3 <= 4 Less than or equal to >> 4 = = 4 » Equal to | » Logical OR
>> 3 > 4 Greater than >> 4 ~ = 4 » Not equal to ~ » Logical NOT
If condition holds good then gives an output as 1 else it will be 0. Instead of
numbers assigned variables can be used in relational operators.
MATRIX OPERATIONS
>>A = [1 2 3] Row vector creation B = [5; 6; 7] » Column vector creation
>>C= [1 2 3; 4 5 6; 8 5 4] Matrix creation, Consisting of 3 rows and 3 columns.
>> G=[1:1:4] G= [Initial: Increment: Final] » A vector with equally spaced elements and the
G=1 2 3 4 increment can be even negative.
>> C(1,1) Indexing of a matrix; Extracting the element pertaining to 1st row and 1st column.
>> C (2 , : ) Extract the entire second row C ( : , 2 ) » Extract the entire second column
>> C (2:3 , : ) Extraction of multiple rows C (: , 2:3 ) » Extraction of multiple Columns
>> sum (C, 1) » Gives a row vector containing the sum of prod (C,1) » Gives a row vector containing the
each column. products of each column.
>> sum (C, 2) » gives a column vector containing the sum prod (C,2) » gives a column vector containing the
of each row. products of each row.
>> C (2:3 , 2:3 ) Extracting portion of a matrix Try and see the answer for C (2:3 , 1:3 )
>> triu (C) Upper triangular matrix. Check the answer for tril (C)
ans = 1 2 3 It gives a matrix with only Lower triangular matrix. It gives a matrix with
0 5 6 upper elements present and only upper elements present and making lower
0 0 4 making lower triangle triangle elements zero.
elements zero.
>> diag(C) Diagonal elements of matrix. >> det (C) Determinant of a matrix.
>> C’ Complex conjugate transpose of a matrix. >> inv (C) » Inverse of a matrix.
>> rank(C) Rank of matrix A >> eig(C) » Eigen values of a matrix.
>> G= linspace (0, 20, 5) G= [Initial, Final, n]
G = 0 5 10 15 20 A vector with n elements linearly placed.
>> G=logspace (1, 5, 3) G= [Initial, Final, n]
G = 10 1000 100000 A vector with n elements linearly placed in the specified range.
Create two matrix Perform all the following operations Concatenation of matrix: D = [A B]
A = [1 3 5; 7 4 8; 9 3 12] A + B » Matrix addition. D = [A ; B]
A - B » Matrix subtraction. blkdiag (A,B) » Block diagonal matrix.
B = [4 7 3; 6 7 12; 4 7 3] A*B » Matrix Multiplication Resultant matrix can be virtually divided
A / B » Matrix Division in to four blocks where matrix A and B
A(1, end - 1) , A( : , end - 1) are set diagonally to the whole block.
A(1,:)=[9 8 7] Other block elements are to be zeros.
STANDARD ARRAYS
>> zeros(3) A 3 - by -3 matrix of zeros. >> zeros (2,5) A 2 - by - 5 matrix of zeros
>> ones(3) 3 by 3 matrix of ones. >> ones (2,4) A 2 - by - 4 matrix of zeros
>> eye (3) Identity matrix of size 3 by 3. >> rand(3) Forms a 3 by 3 matrix random numbers.
PERIOD OPERATORS
A = [1 2 3; 4 5 7 ; 5 9 3] A.*2 » Each element of matrix A is multiplied by 2.
B = [4 7 3; 6 9 12; 4 2 3] B. ^2 » Each element of matrix A is raised by 2.
A.*B » Each element of matrix is multiplied with same dimensional element of B.
A. /B » Each element of matrix is divided with same dimensional element of B.
DATA VISUALISATION , PLOTTING OPTIONS and CONTROL LOOPS
% Obtaining the plot % Obtaining the plot and % Obtaining the multiple plots
clc labeling in a graph
clear all clc clc
clf clear all clear all
x= [0:2*pi/20:2*pi] clf clf
y= sin(x); x= [0:2*pi/200:2*pi] x= [0:2*pi/200:4*pi]
plot(x,y); y= sin(x); y= sin(x);
Edit plot options to be shown in plot(x,y); z= cos(x)
figure window. title('Plotting the sine curve') plot(x,y, '--g')
To know more about plot options, xlabel('Angle in radians') hold on
Type help plot in the command ylabel('Angle in radians') plot (x,z, '.r')
window. grid on
If condition if else condition else if condition
clc Clc clc
clear all clear all clear all
Val=input('Enter the Value = '); Val=input('Enter the Value = '); Val=input('Enter the Value = ');
if Val==1
if Val==1 if Val==1 disp('Answer is correct')
disp('Correct Answer') disp('Answer is correct') elseif Val==2
end else disp('Answer is Considered')
disp('Answer is Wrong') else
end disp('Answer is Wrong')
end
For loop Switch Condition While loop
This is used for a definite Clc The command block will execute
number of iterations. clear all while the conditional expression
Val=input('Enter the Value = '); is true. Don't need to know number
clc switch Val of iterations. Note: Beware of
clear all case 1 infinite loops.(Use ctrl c to come
disp('Belongs to section1') out of infinite loop)
for n=1:4 case 2 clc; clear all;
disp('count') disp('Belongs to section2') Val=input('Enter the Value = ');
end otherwise while Val==0
disp('Invalid Entry') disp('Execute the commands ')
end end