23
Matlab basics ,operators & branching statements

Matlab operators

Embed Size (px)

DESCRIPTION

Basic matlab operators

Citation preview

Page 1: Matlab operators

Matlab basics ,operators & branching statements

Page 2: Matlab operators

Basics commands

• Inputting a variableNum = input(‘ Enter a number ’);• Displaying output data– Use of semicolon– X=a+b;– X=a+b output displays automatically

• disp(‘Hello’)

Page 3: Matlab operators

Relational operators

• Equality ==• Not equal ~=• >• <• >=• <=

Page 4: Matlab operators

Equality operator ==

• Difference between ‘=‘ & ‘==‘• A=5; B=5; C=-35;• A==B 1• A==C 0• Q=[1 2 3]; W=[2 3 4] ; R=[1 9 3] ; T=[1 2 3];• Q==W ?• Q==T ?• Use of isequal()

Page 5: Matlab operators

Not equality ~=

• A=89;• B=79;• C=89;• A==B ??• A~=B ??• A~=C ??

Page 6: Matlab operators

> ,< , >= ,<=

• A=12 ; B=87 ; C=78;• A>B • A<B

• Q=[1 2 3 8 9 4] R=[2 3 1 8 9 2]• Q>R• Q<R

Page 7: Matlab operators

Logic operators

• Logical AND &• Logical AND with shortcut evaluation &&• Logical OR |• Logical OR with shortcut evaluation ||• Logical XOR xor(a,b)• Logical NOT ~

Page 8: Matlab operators

Logical AND &a b Y=a & b

0 0 ?

0 1 ?

1 0 ?

1 1 ?

Truth tablea b Y=a & b

0 0 0

0 1 0

1 0 0

1 1 1

A = 1;B=0;A & B ?

A=8;B=-9;

A & B ?

Page 9: Matlab operators

Logical AND with shortcut evaluation &&

• Support partial evaluations• A = 0 ;• B= 1;• A & B • A && B

Page 10: Matlab operators

Problem

• Test whether ratio of two variables a and b is greater than 10

• X = a/b >10 ; • What if b = 0?• Use shortcut evaluations• x = (b ~= 0) && (a/b > 10)

Page 11: Matlab operators

Logical OR |a b Y=a |b

0 0 ?

0 1 ?

1 0 ?

1 1 ?

Truth tablea b Y=a | b

0 0 1

0 1 1

1 0 1

1 1 0

A = 1;B=0;A | B ?

A=8;B=-9;

A | B ?

Page 12: Matlab operators

Logical XOR xor(a,b)a b Y= xor ( a ,b)

0 0 ?

0 1 ?

1 0 ?

1 1 ?

Truth table

a b Y=xor ( a , b)

0 0 0

0 1 1

1 0 1

1 1 0

A = 1;B=0;xor ( A , B) ?

A=8;B=-9;

xor ( A, B) ?

Page 13: Matlab operators

Logical NOT ~

• One operand only• A=1; ~A ??• B= false ~B ??• C=-89 ~C ??

Page 14: Matlab operators

Logical functions

• isequal(a,b) • ischar(a) • isempty(a)• isinf(a)• isnan(a)• isnumeric(a)• logical(a)

Page 15: Matlab operators

A=‘brainbitz’; C=-89; D=[1 2 -3 0 1 ];B=15; E =[ ]; F=0; H = [1 2 3 4]; J=[1 2 3] ; G =[ 0 0 0];

• ischar(A) • ischar(C)• isempty(F)• isempty(E )• isempty(G)• isinf(C)• isinf(C/F)• isnumeric(D) • logical (D) [1 1 1 0 1];

Page 16: Matlab operators

Branching statements & Loops

• If• Switch• While • For• Try catch branch

Page 17: Matlab operators

If statement

if (condition 1)-------------------------------

elseif (condition 2)------------------------------

else---------------------

end

Page 18: Matlab operators

Switchswitch (expression)case val1

--------------------

case val2---------------

otherwise-------------------

end

Page 19: Matlab operators

while

while (expression)------------------------------------end

Page 20: Matlab operators

For

for index=1:increment:m-----------------------------------end

Page 21: Matlab operators

Try/catch

try----------------------------

catch------------------------------

end

Page 22: Matlab operators

Problem

• Input 2 numbers and calculate factorial of their differences , ie (N1 – N2)!

N1=input(‘Enter number 1 ‘);N2=input(‘Enter number 2 ’ );Result = factorial(N1 – N2);OutputsN1=5 ;N2=2;Result =3!

=6

N1=2;N2=5;Result = -3 ! = ERROR !!!

Page 23: Matlab operators

Continued…

N1=input(‘Enter number 1’);N2=input(‘Enter Number 2’);try

Result = factorial( N1 - N2 );

catchResult = factorial( N2 - N1 );

end