1 / 40

Introduction To MATLAB

Introduction To MATLAB. Chapter 1. Why Learn To Code?. Earth Scientists Deal with Large Datasets Processing and Visualization Should be Automated Can make your own tools Research is new, so no tools may exist. Above: > 1,000,000 Persistent-Scatterer InSAR Data Points in SoCal.

kipling
Download Presentation

Introduction To MATLAB

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Introduction To MATLAB Chapter 1

  2. Why Learn To Code? • Earth Scientists Deal with Large Datasets • Processing and Visualization Should be Automated • Can make your own tools • Research is new, so no tools may exist Above: > 1,000,000 Persistent-Scatterer InSAR Data Points in SoCal

  3. What Language Should I Learn? • It doesn’t really matter • Once you know one language, you can learn new ones • Most languages are more similar than different • Commonly used programming languages in Earth sciences • MATLAB / Octave • Python • C / C++ • Fortran • Perl • Java • Mathematica/ Maple • R • Various Linux/UNIX shell scripting languages (sh, bash, csh, tcsh, ksh, etc…) • Lots of others, too… • All have strengths and weaknesses

  4. MATLAB’s Strengths • Scientists use MATLAB a lot. Why? • Easier than traditional programming • Runs on all major platforms (Win, Mac, Linux/UNIX) • It is a full programming language (unlike Excel), so tasks can be fully automated (saves time) • Has tons of built-in functions that do complex math • diff, inv, chol, svd, fft, eig, and lots more! • Has built-in plotting and visualization tools Above: Fault slip rates on the Sierra Madre fault in SoCal Above: Mathematical Inversion of GPS data for strain in SoCal

  5. MATLAB’s Weaknesses • MATLAB is not free • can become expensive if you use toolboxes • Can be slow for some operations • Launching MATLAB is very slow • Interpreted language (not formally compiled) • Language is converted into machine language on the fly • Good news: most commands are highly optimized • Awkward handling of non-numeric data* Slip rate vectors on the Hollywood fault, CA *In this instructor’s opinion.

  6. MATLAB is Well-Documented • Nearly anything that you need to know is in the MATLAB documentation • Online: http://www.mathworks.com/help/matlab/ • In MATLAB Command Window: “doc matlab” • Don’t be afraid to Google it! • Don’t copy code verbatim! • You must be able to explain what you did • Code in assignments cannot exceed what we have covered • The Attaway text is also a nice reference

  7. Starting MATLAB The Command Window

  8. Starting MATLAB Command History

  9. Starting MATLAB Workspace (List/Info of defined variables)

  10. Starting MATLAB Current Folder & File Info

  11. Starting MATLAB Current Folder (clickable) • The current folder is important to pay attention to!!! • When you save something, this is where it is saved (unless specified) • If you want to execute a script or function, it must be here or in your system path • To automate many tasks: must know how to navigate the file system with keyboard commands! • For these reasons, we must learn about paths and associated commands

  12. MATLAB: Common File Operations • MATLAB offers a set of commands that perform common file operations. • http://www.mathworks.com/help/matlab/file-operations.html • You should know how to use • dir, ls, pwd, cd, copyfile, delete, mkdir, movefile, rmdir, winopen,what, which

  13. Sample File System Directory Tree • Hard disks are subdivided into directories and subdirectories • Sometimes called “folders” • The top level directory: root directory • “C:\” on windows (sometimes D:\ or any other letter) • “/” on Mac and Linux/UNIX • MATLAB accepts either “\” or “/” to for paths, but I will use “/”

  14. Sample File System Directory Tree • To move a file, or your change current location, you must specify the path to where you want to go • Absolute Path: begins with the root directory • >> cd /Users/marshallst • >> cd(‘/Users/marshallst’) • >> cd C:\Users\marshallst • >> cd(‘C:\Users\marshallst’)

  15. Sample File System Directory Tree • To change directories, you can specify a relative or absolute path • Absolute Path: begins with the root directory • >> cd(‘/Users/marshallst/Documents’) • Relative Path: assumes you are starting from your pwd • >> cd(‘Documents’)

  16. Absolute vs. Relative Paths • To change directories, you can specify a relative or absolute path • Absolute Path: begins with the root directory • >> cd /Users/marshallst/Documents/MATLAB • Relative Path: assumes you are starting from your pwd • >> cd Documents/MATLAB

  17. Absolute vs. Relative Paths • To change directories, you can specify a relative or absolute path • Absolute Path: begins with the root directory • >> cd /Cygwin/home • Relative Path: assumes you are starting from your pwd • >> cd ../

  18. Absolute vs. Relative Paths • To change directories, you can specify a relative or absolute path • Absolute Path: begins with the root directory • >> cd /cygwin/usr/local/share • Relative Path: assumes you are starting from your pwd • >> cd ../../usr/local/share

  19. Listing Directory Contents & Wildcards • MATLAB provides two commands to display the contents of a directory. (They do the exact same thing) • >> dir • >> ls • “dir” comes from DOS • “ls” comes from Linux/UNIX • Sometimes you may only want to see certain files. • Use the “*” wildcard. • Only list files ending in “.txt” • >> ls *.txt • Only list files beginning with “data” • >> ls data* • Only list files that have “temp” anywhere in the file name • >> ls *temp* • What does this list? • >> ls file*412*.dat

  20. MATLAB Time • Let’s try it out! • >> cd • >> pwd • >> ls • >> movefile • >> copyfile • >> mkdir • >> delete • >> rmdir • >> %comments

  21. Assigning Variables • Values can be stored in variables using MATLAB • Variable goes on left, what you want to put goes on right • >> variablename = expression • >> a = 6 • >> 6 = a %This gives an error! • >> mass = 2.5e9 • MATLAB follows order of operations and parentheses () • Don’t mix (), [], {} • >> badIdea = ([2+3]*4)+6 • These [] {} often have special meanings

  22. Variable Name Rules Variable names are very flexible, but must follow these rules: • Must begin with letter of the alphabet. After the first character, it can contain special characters or numbers • >> myvar = 4 • >> 2num = 6 • >> rad23 = 2.3e3 • There is a limit to the length of a variable name. Typically ≤ 64 characters • >> namelengthmax • MATLAB is case-sensitive, so capitalization matters • >> mynum = 4 • >> MYNUM = 6 • >> myNum = 8 • Avoid underscores. Use mixed case • >> my_num = 5 %probably works, but could cause problems • >> myNum = 5 %much safer…still easy to read • Certain words are reserved and cannot be used as variable names • >> iskeyword %gives a list of reserved words • >> for = 4 %this is not allowed • Names of built-in functions should not be used as variable names • >> sin = 2 %technically allowed, but is bad programming style & may cause problems • Variable names should be mnemonic & short • >> aabcaded = 2 %what is aabcaded? • >> temperatureInTopekaKansasAug19RecordedByChrisSmithTannerTheSecond=2 %Yuk! Too long! • >> tempKS = 2 %much better

  23. Basic MATLAB Usage Things to remember… • Remember that all functions/commands in MATLAB use a similar basic syntax • functionCall(arguments) • >> sin(4*pi) • >> sqrt(64) • Non-numeric arguments: surrounded with single quotes • >> disp(‘Hello’) • Many functions return values that can be stored • >> angleRad=atan(0.5) • Let’s try it out!

  24. Variable Types and Casting • MATLAB allows for several different classes of variables • double (double precision): the default type for floating point numbers • int8, int16, int32, int64 (integer): Can save memory (in some cases. See Ch1 of Attaway) • char (character): single character (e.g. ‘x’ or ‘z’). A group of characters is called a string (e.g. ‘hello’ or ‘thisIsAString’) • logical: stores true/false values • Once a variable has been defined it can be changed into another type by type casting

  25. Random Numbers • MATLAB can generate pseudo-random numbers • Useful for creating synthetic data, or adding noise • Gives a random floating point number between 0-1 • >> rand • To get random integers use “round” • >> round(rand*10) %gives rand integers from 0-10 • WARNING: rand will always give the exact same random number when you first start. • Use the following command first to prevent this • >> rng(‘shuffle’)

  26. Encoding • Read the section on encoding in Attaway. • Basically, you need to know: • What ASCII is/means • Why a character casted into an int or double returns an integer from 0-127 • How to convert an ASCII value to numeric and vice versa

  27. Vectors and Matrices • MATLAB is short for Matrix Laboratory • It is specifically designed for vector and matrix operations • Vectors and matrices are VERY useful in Earth sciences! or -Wind directions, -Stream flow direction -Plate Motions -Fault slip -Glacier motion Etc… or The stress cube (i.e. cool stuff!) Also: -Strain, -Hydrologic conductivity -Earthquake Energy Release Etc… The stress matrix

  28. Defining Vectors • Defining vectors in MATLAB is straightforward • Must use square brackets [] • Called “Arrays” in most other programming languages • rowVect = • >> rowVect = [1 2 3] • >> rowVect = [1, 2, 3] • columnVect = • >> columnVect = [4; 7; 9] • Turn a row vector into a column vector using apostrophe ‘ • Mathematically, this is called the transpose • >> columnVect = rowVect’

  29. Matrix Construction: The Basics Matrix construction follows specific mathematical rules: • Dimensions [m x n] • m = rows (vertical) • n = columns (horizontal) All numerical quantities are technically matrices! = 1x1 matrix (scalar) = 2x1 matrix (column vector) = 1x3 matrix (row vector) = 2x2 matrix (square) = 4x2 matrix (rectangular) The matrix has you…

  30. Defining Matrices • Defining matrices in MATLAB is also straightforward • Again, use square brackets • Mat2x2 = • >> mat2x2 = [1 3; 2 4] • >> mat2x2 = [1, 3; 2, 4] • mat2x3 = • >> mat2x3 = [1 3 5; 2 4 6] • >> mat2x3 = [1, 3, 5; 2, 4, 6] • mat3x2 = • >> mat3x2 = [1 4; 25; 3 6] • >> mat3x2 = [1, 4; 2, 5; 3, 6]

  31. Matrix Operations: The Basics • Matrix Addition/Subtraction • Both must have same dimensions • Add each corresponding element • Result has same dimensions • + is Commutative A + B = A + B • - is not commutative A - B ≠ B - A • Matrix Multiplication: 2 x 3 * 3 x 4 --> 2 x 4 • Not commutative • MATLAB assumes all math operations are on matrices • For element by element operations use a dot before the symbol • >> A * B %matrix multiplication • >> A .* B %array multiplication • >> A ./ B %array division • >> A .^ B %array to a power • We’ll do some basic linear algebra later on…

  32. Linspace and the Colon Operator • Often, we want to make long lists of numbers • >> [1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20] %very tedious • >> [0 3 6 9 12 15 18 21 24 27 30 33 36 39 42 45 48 51 54 57 60] • >> [50 45 40 35 30 25 20 15 10 5 0 -5 -10 -15 -20 -25 -30] %boring! • We have two handy options in MATLAB to make lists • The colon operator • >> 1:20 %The increment is assumed to be 1 unless specified • >> 0:3:60 %begin:increment:end • >> 50:-5:-30 %note that the increment can be negative • “linspace” function: linspace(begin,end,n) • >> linspace(1,20,20) %Gives 20 evenly spaced values between 1-20 • >> linspace(0,60,21) %Gives 21 evenly spaced values between 0-60 • >> linspace(50,-30,17) %Gives 17 evenly spaced values between 50 & -30 • Don’t these do the same thing? Why have both?

  33. Referring to Matrix Elements test • To refer to a particular element of a matrix, use its row,col (subscripted) • >> test(3, 1) %This returns what value? • >> test(2, 3) • Can also refer to the linear index • For a matrix, can be quite confusing ---> generally a bad idea • Useful for vectors! • >> test(3) %This returns what value? • >> test(10) (row,col) notation: Linear index:

  34. Referring to Matrix Rows/Columns test • To refer to an entire column of a matrix, use a colon in the col entry • >> test(:, 1) %This returns what value? • >> test(:, 3) • Can also refer to an entire row • >> test(1, :) %This returns what value? • >> test(2, :) • This is a HUGE time saver. Use the colon! • Common Example: if your variable has XY coordinates in columns • >> XY(:, 1) %gives all x-vals • >> XY(:, 2) %gives all y-vals

  35. Replacing/Removing Matrix Elements test test2 = • To change the 2 to 16 in test and test2: • >> test(1, 3) = 16 • >> test2(3) = 16 • Can also refer to an entire row • >> test(1, :) %This returns what value? • >> test(2, :) • To remove an element, use an empty matrix “[]” • >> test2(3) = [] %what does this do? • >> test(2,3) = [] %why does this given an error? • >> test(:, 2) = [] %what does this do?

  36. Concatenating Matrices Sometimes we want to add elements to a matrix • Concatenate: to paste together two items • Concatenating strings is easy! • >> str = ‘This ’ • >> str2 = ‘is ’ • >> str3 = ‘easy!’ • >> finalStr = [str str2 str3] • >> newStr=[‘This’, ‘ ’, ‘is ’, ‘also’, ‘ easy!’] • Concatenating a matrix works the same way. (make sure dimensions are consistent) • Row vector • >> stuff = [2 3 4] • >> otherStuff = [5 6 7] • >> allStuff = [stuff, otherStuff] • Column vector • >> stuff = [1; 2; 3] • >> otherStuff = [4; 5; 6; 7] • >> allStuff = [stuff; otherStuff] • Matrix • >> mat = [1 3; 2 4] • >> mat2 = [5 7; 6 8] • >> newMat= [mat, mat2] %what will size of newMat be? • >> newMat2 = [mat; mat2] %what will size of newMat2 be?

  37. Matrix Size and Dimensions test test2 = test3 = Sometimes we want to know the length, dimensions, or number of elements in a matrix • To get the matrix dimensions (rows, cols) use “size” • Size returns a matrix, so you can store the rows/cols if you want. • >> size(test) • >> [m, n] = size(test3) • >> [row, col] = size(test2) • To get matrix length use “length” • >> length(test2) • >> a = length(test) • >> len = length(test3) • To get the total number of elements, us “numel” • >> numel(test) • >> sumTest = numel(test2) + numel(test3) • >> harderOne = numel(test(:, 1) * numel(test3(1, :))

  38. Reshaping, Rotating, and Flipping test test2 = test3 = Sometimes we want to change the shape (dimensions) of a matrix. • To reshape a matrix use “reshape” • Must have same total number of elements • Preserves the linear index of elements • >> reshape(test2, 2, 3) • >> newTest = reshape(test,2,6) • >> [row, col] = size(test2) • MATLAB also provides functions to flip horizontally/vertically or rotate matrices • flipud, fliplr, rot90 %In my experience, these are not as useful as reshape • (note that rot90, does not do a coordinate transformation of data!) • MATLAB also provides a function to repeat a matrix forming a new matrix • repmat %try this one on your own. Read the documentation for usage.

  39. Using Functions with Matrices Nearly all functions in MATLAB are designed to be performed on matrices, not just single values test test2 = test3 = • >> sin(test) %takes the sine of all of the entries in test individually • >> sqrt(test2) %takes the sqrt of all entires in test2 • >> log(test3) • Most functions that generate values, can generate matrices! • >> rand(3,4) • >> round(rand(6,2)*20) • What do you think these commands do? Try them out! • >> zeros(4,3) • >> ones(2,10) • >> (ones(3,4) .* 7)’

  40. 3D Matrices • In Math class, you probably only learned about 2D matrices • MATLAB allows for multi-dimensional matrices • E.g. color images are typically stored in 3D matrices • How to define a 3D matrix? One slice at a time • >> mat3D(:, :, 1) = [9 4 2; 3 7 5; 11 1 10; 8 6 12] • >> mat3D(:, :, 2) = [2 1 9; 10 5 8; 12 4 3; 5 7 6] • >> mat3D(:, :, 3) = [3 0 10; 6 2 5; 9 4 8; 12 7 1] • What is the output below? • >> mat3D(2, 3, 2) • >> B = mat3D(2, :, 2) • >> new = mat3D(:, :, 3) mat3D =

More Related