GS 2006/Math - Collins

Lab 2
Manipulating Matrices & Programming in MATLAB


Start MATLAB (from Start Menu). Arrange your windows so you can see MATLAB and this webpage.

Matrix Manipulations

We can pull parts of matrices by specifying a row, list of rows and all row, and a column, list of columns or all the columns. Try these:
M = magic(5); 
M(1,[2 3]) 
The colon can be used to specify a range (1:5) of rows or columns or all (:) the rows or columns.
MATLAB performs matrix addition, subtraction, multiplication and multiplication by a scalar using the usual format: +, -, * and *. If the dimensions don't allow the operation, then there will be an error message. Try these:
A = rand(3); B = rand(3,4); C = rand(3); D = rand(4); 
You can also do powers of matrices using ^, e.g. D^2 = D*D, D^3 = D*D*D. Try D^10
MATLAB also allows functions that act on matrices. Some act on the matrix as a matrix, others as a collection of individual elements. For example sum(A) sums each column of A, max(A) finds the maximum of each column of A, while abs(A) takes the absolute value of each element of A, and cos(A) computes the cosine of each element of A.

Scripts and Functions

Programming in MATLAB is pretty straight-forward as all you do is put the commands you want to use together in a file. There are two types of programs that MATLAB uses: Scripts and Functions. Both are called "M-Files" because the file name is whatever.m

To create a M-File, go under the File menu and choose New M-File. This will bring up a new window which you can type in. You might want to move/resize windows so you can see the M-file editor, the Command Window and your web brouser all at once.

First Script
A script is just a list of MATLAB commands and when it runs, it acts just like you had typed the commands. You can control the way it acts either by changing values before you run it, or have it ask for values, like in this example:

% SCRIPT 1  myname.m  by Dr. Collins for GSS:Math 

name = input('Enter your name: ','s'); 
N = input('How many times do you want to see it? '); 
for i = 1:N 

Either type this in, or copy and paste it into the M-file editor window and save as "myname.m". To run it, go into the Command window and type myname

Second Script
Create a new script that does the following: Save the result as "mymatrix.m" and run it. If you get an error message, try to figure out what's wrong and fix it. If you have trouble, try to think about what commands you would type to get the result.
If you get really lost, I've put a version of this at the end of this file.
First Function
The main two differences between a script and a function are that A function starts with a line like
function output = nameoffunction(input)
You can have several inputs and several outputs, but we'll start with one of each. Here's a function that takes as input a value for the size of a matrix and produces a matrix of that size so that A(i,j) = i + j:

function M = myfun(N) 
% function to compute N x N matrix with entries M(i,j) = i + j 

for i = 1:N 
     for j = 1:N 
         M(i,j) = i + j; 
Type or copy & paste this into the M-file editor and save it as "myfun.m". The file and the name of the function should be the same.

To test it, type myfun(4) in the command window.

Here are some additional program ideas you might try. You can do all the changes in the "myfun.m" M-file.
SCRIPT 2 mymatrix.m
% SCRIPT 2  mymatrix.m  by Dr. Collins for GSS:Math 

N = input('Enter the size of the matrix: '); 
for i = 1:N 
      for j = 1:N 
            A(i,j) = i/j;