
n=length(x) mean=sum(x)/n stdev=sqrt(sum((x-mean).^2)/n) Defines a new function called STAT that calculates the mean and standard deviation of a vector. Internal variables are local to the function Computer Vision Lecture Notes 03 20Ģ1 Functions in MATLAB (cont.) Example: A file called STAT.M: function =stat(x) %STAT Interesting statistics. They operate on data in the workspace Functions, which can accept input arguments and return output arguments. Element-wise operators defined with a preceding dot.*./.^ size(a) size vector sum(a) columns sums vector sum(sum(a)) sum of all the elements Computer Vision Lecture Notes 03 10ġ1 Variable Name in Matlab Variable naming rules must be unique in the first 63 characters must begin with a letter may not contain blank spaces or other types of punctuation may contain any combination of letters, digits, and underscores are case-sensitive should not use Matlab keyword Pre-defined variable names pi Computer Vision Lecture Notes 03 11ġ2 Logical Operators =,, (not equal) ~=, (not) ~ find( condition ) Returns indexes of A s elements that satisfy the condition Computer Vision Lecture Notes 03 12ġ3 Logical Operators (cont.) Example: >A=, Idx=find(A<4) A= Idx= Computer Vision Lecture Notes 03 13ġ4 Flow Control MATLAB has five flow control constructs: if statement switch statement for loop while loop break statement Computer Vision Lecture Notes 03 14ġ5 if IF statement condition The general form of the IF statement is IF expression statements ELSEIF expression ELSE END statements statements Computer Vision Lecture Notes 03 15ġ6 switch SWITCH Switch among several cases based on expression The general form of SWITCH statement is: SWITCH switch_expr CASE case_expr, statement, statement CASE statement, statement OTHERWISE statement, statement END Computer Vision Lecture Notes 03 16ġ7 switch (cont.) Note: Only the statements between the matching CASE and the next CASE, OTHERWISE, or END are executed Unlike C, the SWITCH statement does not fall through (so BREAKs are unnecessary) Computer Vision Lecture Notes 03 17ġ8 for FOR repeats statements a specific number of times The general form of a FOR statement is: FOR variable=expr END statements Computer Vision Lecture Notes 03 18ġ9 while WHILE repeats statements an indefinite number of times The general form of a WHILE statement is: WHILE expression END statements Computer Vision Lecture Notes 03 19Ģ0 Scripts and Functions There are two kinds of M-files: Scripts, which do not accept input arguments or return output arguments.
#If statement matlab how to#
6 MATLAB Help (cont.) Any command description can be found by typing the command in the search field As shown above, the command to take square root (sqrt) is searched We can also utilize MATLAB Help from the command window as shown Computer Vision Lecture Notes 03 6Ĩ More about the Workspace who, whos current variables in the workspace save save workspace variables to *.mat file load load variables from *.mat file clear clear workspace variables Computer Vision Lecture Notes 03 8ĩ Matrices in MATLAB Matrix is the main MATLAB data type How to build a matrix? A= Creates matrix A of size 3 x 3 Special matrices: zeros(n,m), ones(n,m), eye(n,m), rand(), randn() Computer Vision Lecture Notes 03 9ġ0 Basic Operations on Matrices All operators in MATLAB are defined on matrices: +, -, *, /, ^, sqrt, sin, cos, etc.
