Lab 2

You might also like

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 9

SPECIAL FUNCTIONS

USED IN LOOPS
 length or numel : The functions returns
the total number of elements of the
vector.

 size : The function returns the size of a


matrix in terms of number of rows and
columns .

 end : The function returns the value at


the last index of the vector. It is also
used as syntax to end the loop. Using it
at the end of the loop indicates the one
complete cycle of the loop.
LOOPS
 Two types of loops

 ‘for’ loop
 Syntax:
 for var=j:s:k (for i=1:numel(a))
‘code to be executed’
end

 ‘while’ loop
 Syntax:
 while (condition is true)
‘code to be executed’
end
IF Statement
 Syntax:
 if (condition1)
‘code executed if condition1
true’
elseif (condition2)
‘code executed if condition2
true’
elseif(condition3)
‘code executed if condition3
true’
.
.
.
else
== < ‘code executed
> if all above
<= >= ~=
conditions false’
Equal to Less than Greater than Less than &equal to Greater than &equal to Not equal to
end
FOR Loop Example
DIFFERENCE BETWEEN ASSIGNMENT
(=) AND COMPARISON (==) OPERATOR
 Assignment (=) operator is used to assign a calculated value or a variable on
the right side to the variable on the left side.
 a = 5+4 => means first 5+4=9 is calculated and then assigned to a.
 b=a => means b will be equal to a because a is assigned to b.
 Now, if a=[1 2 3 4], then b=a+4 => means first calculate a+4 i.e. [5 6 7 8]
and then assign it to b, so b=[5 6 7 8].
 Comparison operator is used to compare two variables, arrays, matrices,
alphabets or strings. If the expression on left hand side is equal to the right
hand side then the statement will return 1 otherwise zero.
 a==4; ‘a’ is a scalar (variable); statement returns 1 if 4 is stored in ‘a’
otherwise 0.
 a==b; ’a’ and ‘b’ both are scalar; returns 1 if ‘a’ equals ‘b’ otherwise 0.
 A==B; ‘A’ and ‘B’ are matrices; returns 1 if ‘A’ equals ‘B’ otherwise 0.
 R==‘a’; ‘R’ is a character (string) variable and is being compared with
alphabet ‘a’.
UNDERSTANDING HOW
THE CODE WORKS
RAND command
• rand() = generates a random number between
0 and 1 up to 4 digits after decimal point

• rand(m)= generates a m by m size matrix of


random numbers b/w 0 and 1

• rand(r,c)= generates an r by c size matrix of


random numbers b/w 0 and 1
RAND command
• Generating numbers b/w 0 and U: rand()*U

• Generating numbers b/w L and U:


– rand()*(U-L)+L

• Generating whole numbers b/w L and U:


– round(rand()*(U-L)+L) / round(rand()*(U-L))+L

• In all cases, U>L.


WARM UP EXERCISES
1. Generate the following vector using for loop
A= [11 22 33 44 55 66]
Result: Predict it yourself before you try the question
2. Declare a matrix B consisting of 8 elements. Using for loop, find the sum
product of all the elements of the vector and store the result in a
variable ‘p’.
Result: Predict it yourself before you try the question
3. What will be the output of the following code
c=2;
if c==1
disp (‘c is 1’)
else if c==2
disp(‘c is 2’)
else
disp(‘c is neither 1 nor 2’)
end

You might also like