Matlab Reference Guide - Scientific Computing and Problem Solving - Spring 2007 | CSCI 0040, Study notes of Computer Science

Material Type: Notes; Class: Introduction to Scientific Computing and Problem Solving; Subject: Computer Science; University: Brown University; Term: Spring 2007;

Typology: Study notes

Pre 2010

Uploaded on 08/18/2009

koofers-user-iht
koofers-user-iht 🇺🇸

10 documents

1 / 6

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
CS004 Introduction to Scientific Computing Hughes
Matlab Reference Guide
Spring, 2007
Getting Started
•Most of the shortcuts from MS Word have the same function in the
Editor and Command Windows (cut and paste, copy, save, select all,
undo, find and replace, etc). If you’re working in the Sunlab, set up
the shortcuts by going to File →Preferences →Keyboard, and
changing the key bindings to ‘Windows’.
•Typing edit myfunction.m in the Command Window will open up
the M-file for that function in the Editor, or create an M-file for it if
it doesn’t already exist.
•Typing help functionname in the Command Window will display
the help documentation for that function (if it is a standard Matlab
function) or the comments for that function (if it is a function you
wrote) in the Command Window.
•To create a function that returns nothing, type
function functionname(arguments) into the Editor, where
functionname is the name you want your function to have (and the
name of the M-file you’re typing in!) and arguments are the parame-
ters to your function.
•To view the help documentation for a particular function, or to search
for a keyword in the help browser, type doc keyword into the Com-
mand Window.
•When you execute a program and it “freezes” or runs forever, press
Ctrl+c in the Command Window to force it to quit. This command
will always work in Linux, but is not as consistent in Windows and
Mac OS. Another way to force Matlab to stop executing some func-
tion is to type quit force into the Command Window, but this com-
mand will also close your current session.
pf3
pf4
pf5

Partial preview of the text

Download Matlab Reference Guide - Scientific Computing and Problem Solving - Spring 2007 | CSCI 0040 and more Study notes Computer Science in PDF only on Docsity!

CS004 Introduction to Scientific Computing Hughes

Matlab Reference Guide

Spring, 2007

Getting Started

  • Most of the shortcuts from MS Word have the same function in the Editor and Command Windows (cut and paste, copy, save, select all, undo, find and replace, etc). If you’re working in the Sunlab, set up the shortcuts by going to File → Preferences → Keyboard, and changing the key bindings to ‘Windows’.
  • Typing edit myfunction.m in the Command Window will open up the M-file for that function in the Editor, or create an M-file for it if it doesn’t already exist.
  • Typing help functionname in the Command Window will display the help documentation for that function (if it is a standard Matlab function) or the comments for that function (if it is a function you wrote) in the Command Window.
  • To create a function that returns nothing, type function functionname(arguments) into the Editor, where functionname is the name you want your function to have (and the name of the M-file you’re typing in!) and arguments are the parame- ters to your function.
  • To view the help documentation for a particular function, or to search for a keyword in the help browser, type doc keyword into the Com- mand Window.
  • When you execute a program and it “freezes” or runs forever, press Ctrl+c in the Command Window to force it to quit. This command will always work in Linux, but is not as consistent in Windows and Mac OS. Another way to force Matlab to stop executing some func- tion is to type quit force into the Command Window, but this com- mand will also close your current session.

Debugging, Commenting, Editing, and Running Code

  • Ctrl+/ comments out an entire bolt of text, while Ctrl+t uncomments it.
  • To intelligently indent your code, select the code block (or select all with Ctrl+a) and hit Ctrl+i.
  • To run your code, either click on the Run icon (a sheet of paper with a downward pointing arrow next to it), or press F5.
  • To debug a piece of code line-by-line, click on the little dash that appears to the right of the line number. This will turn it into a red dot, which will be used as a breakpoint. When execution reaches a breakpoint, it stops and Matlab allows you to examine the variables and output of your program.
  • If you define multiple functions within another function, which occurs often when working with GUIDE, clicking on the blue f icon on the toolbar will show you a list of all the functions contained within the current M-file, making it easy to navigate and edit them.
  • When you run code with a bug in it (this will happen often!), Matlab will give you an error and a line number. Clicking on the line number in the Command Window will automatically take you to corresponding line in the Editor and place the cursor at the beginning so you can fix the bug.

Tricks To Make Your Life Easier

  • You can dock the Editor so that it appears within the main Matlab Window (the one that contains the Command Window) by clicking the arrow in the top right corner of the Editor.
  • In the upper left panel, there is a little window that can show you either your Current Directory or the Workspace. Using Workspace rather than Current Directory will probably be ideal most of the time, because Workspace allows you to see all the variables currently defined
    • very useful for debugging. Double clicking on a variable will display a spreadsheet-like editor that will allow you to change the values of your variables.

Useful Matlab Commands

Creating and Manipulating Matrices

Syntax Description reshape(A, m, n) Reshapes A from a k × p matrix into a m × n one. Note: kp = mn must hold. mod(a, b) Returns the remainder of a when divided by b. mod(a, 0) == a by convention. Useful for indexing some arrays. abs(A) Returns the absolute values or complex magnitudes of the elements of A. round(A) Rounds the entries of A to the nearest integer. floor(A) Rounds each entry of A to the nearest integer less than or equal to that entry. ceil(A) Rounds each entry of A to the nearest integer greater than or equal to that entry. A .* B Multiplies each element of A by the corresponding element of B. A and B must have the same dimensions. A ./ B Divides each element of A by the corresponding element of B. A and B must have the same dimensions. A .^ n Raises each entry of A to the nth power. zeros(n) (^) Returns a n × n or n × m matrix of zeros zeros(n, m) ones(n) Returns a n × n or n × m matrix of ones ones(n, m)

Working With Matrices

whos Returns the name, number of elements, size in bytes, and type of all variables currently active in the workspace; specifying the variable name limits the output to that variable.

whos(‘variablename′)

numel(A) Returns the number of elements in matrix A. [r, c] = size(A) Returns the vector: [# of rows, # of columns]. size (A, 1) Returns the number of rows of A. size (A, 2) Returns the number of columns of A. find(A) Returns the indices of the nonzero elements of A. find(A == n) Returns the indices of the elements of A equal to n. sum(A) For a n × m matrix A, returns a 1 × m vector, containing the sums of each column. sum(A, dim) Returns a vector of sums along the specified dimensions, where dim = 1 sums across the rows and dim = 2 sums across columns. cumsum(A) Returns a matrix of the same dimensions as A whose elements are the cumulative sum of each column. [m, n] = ind2sub(size, index) Returns the (m, n) index corresponding to the linear index index. index = sub2ind(size, m, n) Returns the linear index corresponding to the indices (m, n).

Generating Random Numbers

rand(n) Returns either a n × n or n × m matrix of numbers chosen rand(n, m) randomly between 0 and 1 (inclusive). randn(n) Similar to rand, but instead draws the values from a normal randn(n, m) distribution with mean 0 and standard deviation 1. randint(n) Creates a n × n or n × m matrix whose entries are either 0 randint(n, m) or 1 with equal probability. randint(n,m,k) Creates a n × m matrix whose entries are random integers between 0 and k − 1.