Matlab Programming
EE121 COMPUTING FOR ENGINEERS B. ENG. IN ELECTRONIC ENGINEERING YEAR 1 SEMESTER 2
Function Handling
function [A,M,D] = Arith(a,b) A = a+b; M = a*b; D = a/b; end To take the output arguments [A,M,D] = Arith(10,5) Else [A,M,D] = Arith(a,b) a & b should be assigned with values
Relational Operators
Compares two numbers Returns 1 if the statement is true, 0 otherwise If the operands are matrices they should be in same
size. A new matrix of same size will be created and 1 or 0 will be assigned depending on the expression evaluated on each elements Eg . 5< 8, returns 1 Can be used in flow control
Relational Operators
< > <= >= == ~=
Less than Greater than Less than or equal Greater than or equal Equal Not eqaul
Logical Operators
Logical operators have two types of operators True or False Nonzero numbers are true and zero is false These operators can also be used in flow control & | ~
And OR NOT
Bit-wise functions
Operates on binary representation of the number bitand(A,B) Ex. Bitand(2,1) = and(10,01) = 00 = 0 bitor(A,B) Ex. Bitor(3,1) = or(11,01) = 11 = 3 Bitxor(A,B) Ex. Bitor(3,1) = or(11,01) = 11 = 3
Similarly bitcmp, bitshift etc
Conditional Statements
if-else if conditional expression ....... . . end
Conditional Statements
if-else-end if conditional expression . . . else . . end
Conditional Statements
if-elseif-else-end
if conditional expression .. .. elseif conditional expression . . esleif conditional expression . . else . end
Conditional Statements
switch-case
switch expression case value1 . case value2 . otherwise . end
Loops
For loop
for i = 1:.1:10 end
While loop while conditional expression
. . end
All these loops and conditional statements can be
nested For each if, case, for and while statements there should be corresponding end statements.
for i = 1:n for j = 1:m x(i,j); end end
Break & Continue
break will terminate the execution of a loop Only the loop containing the break statement will terminate continue will stop the next statements in the loop
and start the new iteration