Extra MATLAB Info The following Web Sites have tutorials on MATLAB: www.mathworks.com http://spicerack.sr.unh.edu/~mathadm/tutorial/software/matlab/ http://www.engin.umich.edu/group/ctm/ Starting MATLAB - On a PC, double click the MATLAB icon - On a UNIX machine, enter the command: matlab Exiting MATLAB - To exit MATLAB, enter the command: quit or exit Getting Help - The command: help xxx Will get help on topic xxx - The command: lookfor xxx Will look for functions whose first line of the help text contains the keyword xxx. - Use the command: lookfor -all xxx to search all help text lines for each function. - The command: helpdesk Give more detailed information on a function etc.. including some examples. Funny State? - If MATLAB goes into a funny state (e.g. infinite loop) try pressing ctrl-c or ctrl-break. Capturing MATLAB Commands/Output Sometimes a record of commands and output entered in a MATLAB session needs to be recorded. To do this, enter the command: diary xxx where xxx is a filename. Output saved is only text based and no graphics will be included. To shut off the diary, enter the command: diary off Variables - Must start with a letter - The first 31 characters are unique - Case sensitive - To see the contents of a variable, enter the variable name and press the enter key. - To erase a variable from memory, enter the command: clear xxx where xxx is the variable to remove. - The command: clear by itself will remove all variables. Types of variables - MATLAB's primary type is a matrix. - 1 x 1 matrices are interpreted as scalars - 1 x n matrices are interpreted as row vectors - n x 1 matrices are interpreted as column vectors Check out the help for more information on different data types. The MATLAB workspace The workspace is the area of memory accessible from the MATLAB command line. Variables are stored in the workspace. The command: whos Is used to see what is currently in the workspace. Creating Statements, Expressions and Variables in the Workspace MATLAB statements are usually of the form: variable = expression or expression Normally statements are placed one on a line. Several statements can be placed on one line as long as each statement is separated by a comma or semi-colon. Expressions are usually composed of operators, functions and variable names. If just the expression form is used, the result is stored in a variable called ans. MATLAB does not require any type declarations or dimension statements. When it encounters a new variable name, the variable is automatically created and the appropriate storage is allocated. If a variable already exists, it will be overwritten and the storage may be re-adjusted. Use the equal sign (=) to assign a value to a variable and create it. Scalars Scalars are used as in programming languages such as C or BASIC. Try entering the following statements (hit Enter key after each line): a = 5, b = 6.327e3 c = 7 d = 5.5 + 7i whos Notice after each line is entered and the Enter key is pressed, the variable shows up and then its value. MATLAB can handle integers, reals, scientific notation and complex numbers. If complex numbers are used, there can be no space between the i and the number in the imaginary part. The letter j can also be used instead of i for the imaginary part. To suppress the output of a variable, put a semi-colon at the end of each statement. This holds for any type of variable. Try it for the same example. Row vectors To create a row vector, enclose the values with square brackets []. Each value within the brackets must be separated by either a comma or blanks. Try: a = [1 2 3 4 5.5] b = [3 4e5] c = [1 2 3 4 5 6 7 8 9 ... 10 11 12 13 14 15] The last line is an example of continuing a statement if there is no more space on the current line. Add three periods at the end of the line and press the Enter key and continue on the next line. This method holds for any statement entered at the command line. Column Vectors To create a column vector, enclose the values with square brackets []. Each value within the brackets must be separated by semi-colon. The semi-colon starts a new row. Try: c = [2;4;5;6] d = [1;1;2;2;3;3] Matrices Creating a matrix is the combination of a row and column vector. Enclose the values in the square brackets and use the semi-colon to start a new row in the matrix. Try: ff = [1 2 3; 234 34 3; 3 4 5] gg = [4 5 6; 2 3 4] Saving a MATLAB workspace Sometimes the workspace contains several variables and it would be a pain to re-enter all of them. To save the current workspace enter the command: save xxx This will create a file called xxx.mat. This file is a binary file and is machine dependent. NOTE, if there is an existing workspace saved and the same name is used, the previous workspace will get overwritten. To load a workspace back into MATLAB, enter the command: load xxx This will load the file called xxx.mat into the workspace. Duplicate variables will get overwritten. Check the help for more details on these two commands. Make sure there are some variables in memory and try: whos save first clear whos load first whos Expressions Expressions can be composed of operators, numbers, functions or variables. All operations are evaluated as a matrix. Here are some matrix operators: + addition - subtraction * multiplication / right division ' transpose ^ power Note that matrices have to be the correct size. Otherwise an error message will occur. Scalars Scalars follow the normal mathematical rules with the operators. Try the following and examine the results: a = 5 b = 6 c = a + b d = a + b * c a = b*c+a^2 e = a/0 e-e f = 2e200/e The last three statements are an example that infinity and Not a Number (NaN) can be handled by MATLAB . This could be beneficial in some cases. Vectors and Matrices Vectors and matrices have to follow the matrix operator rules. Try the following and examine the results: clear a = [1;2] b = 3 c = a + b b * a d = b * a row = [1 2 3 4 5 6 7 8 9] row = row' col = [1 2 3 4 5]' r = [1 2 3 4 5] c = [6 7 8 9 0]' r*c c*r b2 = [1 2 3; 4 5 6] b3 = [3 3 3; 4 4 4] 5*b2 b2+b3 b3-b2 b2*b3' Building Matrices So far the elements in the matrices have been numbers. MATLAB allows expressions and even other matrices as elements to create larger matrices. Try the following and examine the results: a = 5 b = [5*a a] c = [b;b] d = [c c; c c] a=[1 2; 3 4] b=[5 6; 7 8] [a+b a-b; 2*a 3*b; b a] Functions MATLAB has several functions, here are some that will create matrices. Check the help for other functions. Try the following, use help to check out the functions: clear z = zeros(5,5) o = ones(3,3) ident = eye(7) five = 5*ones(4,4) mix = rand(2,5) normdist = randn(3,4) p = [zeros(5,6) ones(5,3)] Accessing Elements in a Matrix For row and column vectors a single subscript can be used. For example: k=3 a= [1 2 3] b= [4 5 6]' a(2) a(k) b(3) For matrices, two subscripts can be used. The first indicates the row, the second the column. A single subscript notation can be used as well. With the single subscript, the matrix is considered to be a single array. Each COLUMN of elements are placed one after each other to represent the array. For example: a=[1 2 3 4; 5 6 7 8; 9 10 11 12] a(1,2) a(2,2) a(3,1) a(1) a(5) a(10) a(6) = 99 a Colon Operator (:) This is one of MATLAB's most important operators. Proper use of this operator will improve efficiency. There are two forms of the colon operator, one is start : inc : end where start is the initial starting value, inc is an increment value and end is the ending value. If the inc expression is left out, an increment of 1 is used. Either form creates a row vector. Try the following and examine the results: 1:3 1:0.5:4.7 55:-3:22 1:-1:3 x= 3, y=2, z=9 x:y:z x = 1:10 y = [1:5;6:10] The colon operator can also be used to access parts of a matrix. If the colon is used by itself in a row or column subscript, it accesses the whole row or column. For example: a(:,3) - the third column a(1,:) - the first row a(1:4,3) - the first 4 rows of the third column a(1:3,:) - the first 3 rows Try the following out: a=[1:4;5:8;9:12;13:16] a(:,3) a(1,:) a(2:4,2:3) a(2,:) = [1 1 1 1] a(:,2) = [2 2 2 2]' Comparing Efficiency Two ways in MATLAB to compare the efficiency of algorithms is by count the number of floating point operations (flops) or elapsed time. flops(0) - Reset floating point operations to zero flops - Display current number of floating point operations tic - Starts clock (in seconds) toc - Stops clock Examples: a = rand(100,100); b = rand(100,200); tic, x=a*b; toc flops(0), x=a*b; flops Saving space If a matrix is very large, but the majority of the matrix is zero, then using the sparse command will save some space and possibly computational time. To convert a sparse matrix to a full matrix, use the full command. Try the following: a= floor(10*rand(1000)/9); b = sparse(a); whos tic, a*a; toc tic, b*b; toc Scripts Scripts (sometimes called M-files) in MATLAB are text files which contain MATLAB commands. When a script is invoked, MATLAB reads in the file and executes each command in it. If several commands need to be executed repeatedly, then creating a script would save time. Any variables created by the script remain in the workspace. The name of the script can be any valid filename with a .m extension (e.g. start.m). To invoke a script, enter the name of the file without the extension. Using a text editor, create the following script and name it script1.m : % Percent sign is used as a comment % Identify your script % % Author: Your name % a = [1 2 3; 4 5 6; 7 8 9] % create a 3x3 matrix sum(a) At the command line prompt in MATLAB, enter the command: script1 This should run the script.