- Most of the basic concepts we learned in C/C++ apply in Matlab. Only some of the implementation details differ.
- Relational Operators - >,<,>=,<=,= =, ~ =
- Note that "not equals" uses a tilde, ~, in place of the ! in C++
- Applied to matrices produces matrices of zeros and ones
- These zeros and ones can then be used as indices to refer to positions within matrices.
- Example:
- m = magic( 5 ); % Five by five magic square, values 1 to 25
- big = ( m >= 20 ); % Ones indicate positions where m is >= 20
- m( big ) = 0; % Changes values of only those positoins to zero.
- m( 5 < m & m < 10 ) = 42.0; % Without intermediary array. Note single &, not double &&.
- twoTone = 255 * big; % Array of all 255 or zero, based on whether m >= 20 ( originally ).
- Logical Operators - &, |, ~ for AND, OR, NOT
- Single &, | does elementwise comparison, continuing for all elements.
- Double &&, || does short-circuiting, similar to C++, only works on scalars.
- if - elseif - else - end
- No curly braces, but the "end" command is required.
- elseif and else are optional.
- No parentheses required, but they don't hurt either.
- while loops
- Condition controlled loops, just as in C/C++
- No curly braces, but a closing "end" is required.
- No parentheses required, but they don't hurt either.
- demonstrated in epsilonExample.m
- Matlab does not have a do-while loop.
- for loops
- NOT a counter-controled loop as in C, though that behaviour can be mimiced.
- The for loop in Matlab is a list-controlled loop
- Syntax:
for index = list
statements
end
- index takes on one value from the list for each iteration.
- If the "list" is a 2-D matrix,then index is a column vector inside the loop.
- for loops and while loops support break and continue
- Matlab does not have the conditional operator, ?: . ( Also no ++, --, +=, etc. )
- find - Generates indices of non-zero elements.
- Example: find( x > 0 ) returns indices for which x is positive.
- Normally treats 2-D matrices as one long column
- [ rows, columns ] = find( ... ) returns separate row and column information.
- ( However results of relational operators can be used directly, as shown above. )
- Matlab has a switch construct, not covered in our current book.
- Syntax:
switch switch_expression case case_expression statements case case_expression statements : otherwise statements end
- Automatically breaks at the end of each case.
- Cases can be combined by using a list for the case expression
- No braces, but a closing "end" is required
- No colon after the case expression
- Parentheses not required around the switch expression, but they don't hurt either
- Matlab uses "otherwise" instead of "default"
- Syntax:
- menus also not covered in our current book - combined with an infinite loop and switch in menuExample.m
The blog provides study material for Computer Science(CS) aspirants. Mostly says "material nahi milta, padhun kahan se.", I think If you can not find content on the Internet, then you are not a CS student. Dedicated to (Prof. Rakesh Kumar, DCSA, K.U.Kurukshetra, HARYANA, INDIA)- "Ek teacher ka bahut jyada padhna, bahut jyada jaroori hota hai."
Wednesday, 2 October 2013
Control Structures
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment