User:Eml4500.f08.wiki1.schaet/hw1

=Matlab Tutorial Section 10-12=

Quick Summary
This section discusses the ease of editing commands using the arrow keys on the keyboard and recalling command lines in Matlab without having to write M-files (section 12).

Details
Editing commands is easy in Matlab. The cursor can be placed anywhere in the command line using the mouse, left/right arrow keys, and backspace. This simple method of editing does not exist in other programming languages such as Fortran. On the PC's that most of us use, the Home, End, and Delete keys can also be used to place the cursor to make revisions. Using the up/down arrow keys allows the user to scroll through past command lines. This allows the user to simply copy and paste a command they want to use again. Once pasted, the user can also alter the command line. This is very useful if you were using a long linear regression equation and only one constant or variable needed to be replaced.

Quick Summary
This section covers the use of colon notation for matricies. It also discusses subscripting by integral vectors. Both of these methods are used to easily manipulate complicated vectors and submatricies. Creative use of the colon notation discussed here can often eliminate the need for loops thus making the execution of MATLAB code faster.

Details
Vectors typically seen as [a b c d e f....n] can take on the notation of [a:n] in Matlab. Using this colon notation makes it easier and simpler to read and execute. One example of the use of this expression 1:5. This expands out to the row matrix [1 2 3 4 5]. When using this notation, the vector does not need to increment by just one either. Adding a middle term defines what level the vector should increment. For example, the expression 0.2:0.2:1.2 is the vector [0.2 0.4 0.6 0.8 1.0 1.2]. Another example is the expression 5:-1:1 gives [5 4 3 2 1].

The colon notation makes it easy to access submatricies as well as alter a given matrix. Below are some examples.

If A is a predefined matrix of arbitrary size n by n, the notation A(1:4,3) is the column vector consisting of the first four entries of the third column of A.

Using the colon by itself takes an entire row or column from a given matrix, in this case the matrix A. For example, the expression A(:,3) is the entire third column of A. The expression A(1:4,:) is the first four rows of A.

Creating new matricies using arbitrary integral vectors also proves useful in Matlab. The expression A(:,[2 4]) is a matrix whose two columns consist of column 2 and 4 of A. This method of manipulation can also be used in an assignment statement. One example of this is the expression A(:,[2 4 5]) = B(:,1:3) replaces columns 2, 4, and 5 of A with the first three columnds of B. Running this command prints and assigns the entire altered matrix A.

This notation also makes multiplying matricies easier. The expression A(:,[2,4]) = A(:,[2,4])*[1 2;3 4] multiplies the columns 2 and 4 in matrix A by the 2x2 matrix [1 2;3 4]. Running this command once again prints the entire altered matrix.

By comparison to other coding languages, using this notation makes adds to Matlab's usefulness when dealing with vectors and matricies.

Section 12 - M-files
M-files are MATLAB files with the ".m" file extension. These are usually created with a local editor, and contain a sequence of statements that can be executed. There are two types of M-files, script files and function files.

Script Files
A script file consists of a sequence of normal MATLAB statements. For example, if you create a script file called, then typing   into the command window will execute the statements that are in the script.

Note: Variables used in scripts are global and will interfere with any variables of the same name within the current MATLAB session.

Function Files
Function files allow you to create your own functions specific to what you are trying to do, and these will have the status as any other function within MATLAB. Function file variables are local by default, but can be set as global.

Example: function a = randint(m,n) %RANDINT Randomly generated integral matrix. %randint(m,n) returns an n-by-m matrix with integers between 0 and 9 a = floor(10*rand(m,n));

You would save this code as a diskfile with the name. Notice that the first line of code gives the name of the function, the input arguments, and output arguments; this line is the difference between a script file and a function file.

You would execute this function file by entering. This sends  and   to be sent as   and   respectively with the output result set as. Again, these variable are, by default, local variables.

The function are flexible in their use of inputs and outputs. The use of, ("number of input arguments") allows you to set a default value of an omitted input variable. A function may also have multiple output arguments.

To make comments, use '%' and the rest of the line is ignored by MATLAB. It is considered correct to include some beginning comment lines to describe the use of the function, and can displayed for example if  is entered. You should also try to write in efficient code, using vectorized operations and avoiding loops. If  loops must be used, you can make them faster by pre-allocating any vectors or matrices to which the output is stored.

Some MATLAB functions are built in while others are distributed as M-files. A listing of non-built-in functions can be seen with the MATLAB command  functionname.