DOC PREVIEW
MIT 9 29 - Study Guide

This preview shows page 1-2-3 out of 8 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 8 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 8 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 8 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 8 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

MATLAB, Statistics, and Linear RegressionJustin Werfel9.29 Optional Lecture #1, 2/09/041 MATLABMATLAB is a powerful software package for matrix manipulation. It’s a very usefullanguage not only for this class, but for a variety of scientific applications, and is usedwidely thoughout industry. Just as when you have a hammer, everything looks like anail, so when you have MATLAB, everything looks like a matrix. You may learn toapproach this Zen-like state by the end of the class.1.1 Free your mindThe basic MATLAB data type is a matrix, an array of values (by default, double-precision floating point numbers), typically arranged as a two-dimensional1grid (thougharrangements of more or fewer dimensions are possible).The simplest matrix consists of a single element. We canassign a value to a variablewith a statement like2i = 4Notice that when you enter this command, MATLAB echoes the result back to you.You can suppress that echo by ending the statement with a semicolon:i = 4;A vector (one-dimensional matrix) can be created with the colon operator. To makea row vector of successive integers, use a statement like x=2:9. The default incrementbetween successive entries is 1; you can specify a different increment by putting it be-tween two colons, e.g., x=9:-2:3 creates a row vector with the elements (9, 7, 5, 3).The transpose operator in MATLAB is the single-quote; the easiest way to createa column vector, then, is to transpose a row vector, e.g., x=(1:8)’. Note that paren-theses in MATLAB, like in C and other languages, can be used to specify the order in1There are at least two ways we can use the word ‘dimension’ when talking about matrices. One refers tothe layout, in the sense that a line is one-dimensional, a square two-dimensional, etc. The other is in the senseof dimensionality of a matrix, e.g., (x1, x2, x3, x4) is four-dimensional (although as a vector, its elementsare arranged in a line when it’s written; that is, a vector is one-dimensional in the first sense). MATLAB’shelp system tends to use the first sense, Sebastian tends to use the second. Context should hopefully alwaysmake it clear which sense is meant. If you have questions about any specific situation, ask.2I’m not going to display the results of every operation here; try them out for yourself and see whathappens.1which you want operations performed; if you type x=1:8’, MATLAB first transposesthe element 3 (with no effect), then applies the colon operator and gives you a rowvector as before.To enter a two-dimensional matrix, use commas or spaces to separate entries onthe same row, and semicolons or carriage returns to separate rows. For example, thestatementA = [1, 5,3 62 7 4,4; 3, 8 3 8]specifies the matrix1 5 3 62 7 4 43 8 3 8Parentheses are used to index matrices, and commas to separate dimensions; so forinstance, with the last definition we used for x, x(5) is 5, and A(1,4) is 6.3You canspecify ranges of indices with the colon operator; a colon by itself means all entriesin that dimension; the keyword end specifies the last entry in a dimension. See whatthese statements give you:A(2:3,1:3)A(:,2)A(3,2:end)A(1:end-1,3)Some operators have special meanings when applied to matrices (notably multi-plication, *, and raising to a power, ˆ); if you want to apply an operation to ev-ery element of a matrix separately, put a period before the operator. For instance,(1:8)*(1:8) gives an error because the inner dimensions of the matrices don’tmatch; (1:8)*(1:8)’ is an inner product, returning a single value; (1:8).*(1:8)gives you a row vector of the squares of the first eight counting numbers; so does(1:8).ˆ2.1.2 M-files, data sets, looping and why you should never do itSuppose we’ve got some set of N sensors from which we can read measurements (tem-perature, voltage, whatever) all at the same time; and we sample from them repeatedlyM times, so we have M sets of N values. Here’s our first look at MATLAB as ham-mer: one natural way to store these data points is in a matrix A, say M rows by Ncolumns. Then each row is a snapshot of the sensor readings at a given time; A(i, j) isthe value the jth sensor had at time i. Let’s further suppose that we want to calculatesome weighted sum of the sensor readings at each time step; the weights are given byvalues stored in the column vector b.3MATLAB stores matrices in memory such that the order of entries in columns is preserved. Thus A(2)= 2, A(5) = 7, etc. You can drop the geometry and get all the entries of a matrix as a single column vectorwith the syntax A(:). Sometimes that knowledge or that operation comes in handy.2The loop-based way to think about doing this would be to loop over all times, andfor each time, loop over all sensor values, multiply each by its weight, and add up thoseproducts. Because it would be a pain to type in all the separate statements every timewe wanted to do this, let’s write a program. With your favorite text editor, create thefollowing file, and name it sensor.m:M = 5000; N = 500; % Let’s choose M and N reasonably large.A = rand(M,N); b = rand(N,1);C = zeros(5000,1);for i=1:5000for j=1:500C(i) = C(i) + A(i,j) * b(j);endendA few notes about this program:1. This is a script, a collection of MATLAB commands put into a file with the.m extension; typing the filename is then equivalent to typing in all those com-mands separately at the command line. The other type of M-file is a function.Creating those is slightly more complicated; you can read about it by typinghelp function at the command line. The use of functions is also differentfrom the use of scripts in various subtle ways that I’m not going to get into here,mostly having to do with the scope of variables in memory.2. In the first line, you’ll see that you can put more than one statement on the sameline; semicolons separate statements and suppress echoing, commas separatestatements and echo the result.3. Everything after the % character on a line is treated as a comment and ignoredby the interpreter.4. The rand(p,q) command creates a p × q matrix and fills it with random num-bers between 0 and 1 drawn from a uniform distribution (every value is equallylikely). randn has the same syntax, but draws its randomnumbers from a Gaus-sian distribution with mean 0 and standard deviation 1. Other commands in thesame family include zeros and ones, which fill the matrices they create withzeros and ones, respectively. MATLAB’s intuitive that way. Be warned: if


View Full Document

MIT 9 29 - Study Guide

Download Study Guide
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Study Guide and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Study Guide 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?