40
Introduction To MATLAB Chapter 1

Chapter 1. Earth Scientists Deal with Large Datasets Processing and Visualization Should be Automated Can make your own tools Research is new, so no tools

Embed Size (px)

Citation preview

Page 1: Chapter 1. Earth Scientists Deal with Large Datasets Processing and Visualization Should be Automated Can make your own tools Research is new, so no tools

Introduction To MATLAB

Chapter 1

Page 2: Chapter 1. Earth Scientists Deal with Large Datasets Processing and Visualization Should be Automated Can make your own tools Research is new, so no tools

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

Page 3: Chapter 1. Earth Scientists Deal with Large Datasets Processing and Visualization Should be Automated Can make your own tools Research is new, so no tools

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

Page 4: Chapter 1. Earth Scientists Deal with Large Datasets Processing and Visualization Should be Automated Can make your own tools Research is new, so no tools

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: Mathematical Inversion of GPS data for strain in SoCal Above: Fault slip rates on the Sierra Madre fault in SoCal

Page 5: Chapter 1. Earth Scientists Deal with Large Datasets Processing and Visualization Should be Automated Can make your own tools Research is new, so no tools

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*

*In this instructor’s opinion.

Slip rate vectors on the Hollywood fault, CA

Page 6: Chapter 1. Earth Scientists Deal with Large Datasets Processing and Visualization Should be Automated Can make your own tools Research is new, so no tools

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

Page 7: Chapter 1. Earth Scientists Deal with Large Datasets Processing and Visualization Should be Automated Can make your own tools Research is new, so no tools

Starting MATLAB

The Command Window

Page 8: Chapter 1. Earth Scientists Deal with Large Datasets Processing and Visualization Should be Automated Can make your own tools Research is new, so no tools

Starting MATLAB

Command History

Page 9: Chapter 1. Earth Scientists Deal with Large Datasets Processing and Visualization Should be Automated Can make your own tools Research is new, so no tools

Starting MATLAB

Workspace(List/Info of defined variables)

Page 10: Chapter 1. Earth Scientists Deal with Large Datasets Processing and Visualization Should be Automated Can make your own tools Research is new, so no tools

Starting MATLAB

Current Folder& File Info

Page 11: Chapter 1. Earth Scientists Deal with Large Datasets Processing and Visualization Should be Automated Can make your own tools Research is new, so no tools

Starting MATLAB

Current Folder (clickable)

The current folder is important to pay attention to!!!1. When you save something, this is where it is

saved (unless specified)2. 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

Page 12: Chapter 1. Earth Scientists Deal with Large Datasets Processing and Visualization Should be Automated Can make your own tools Research is new, so no tools

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

Page 13: Chapter 1. Earth Scientists Deal with Large Datasets Processing and Visualization Should be Automated Can make your own tools Research is new, so no tools

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 “/”

Page 14: Chapter 1. Earth Scientists Deal with Large Datasets Processing and Visualization Should be Automated Can make your own tools Research is new, so no tools

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’)

Page 15: Chapter 1. Earth Scientists Deal with Large Datasets Processing and Visualization Should be Automated Can make your own tools Research is new, so no tools

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’)

Page 16: Chapter 1. Earth Scientists Deal with Large Datasets Processing and Visualization Should be Automated Can make your own tools Research is new, so no tools

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

Page 17: Chapter 1. Earth Scientists Deal with Large Datasets Processing and Visualization Should be Automated Can make your own tools Research is new, so no tools

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 ../

Page 18: Chapter 1. Earth Scientists Deal with Large Datasets Processing and Visualization Should be Automated Can make your own tools Research is new, so no tools

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

Page 19: Chapter 1. Earth Scientists Deal with Large Datasets Processing and Visualization Should be Automated Can make your own tools Research is new, so no tools

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

Page 20: Chapter 1. Earth Scientists Deal with Large Datasets Processing and Visualization Should be Automated Can make your own tools Research is new, so no tools

MATLAB Time• Let’s try it out!

>> cd>> pwd>> ls>> movefile>> copyfile>> mkdir>> delete>> rmdir>> %comments

Page 21: Chapter 1. Earth Scientists Deal with Large Datasets Processing and Visualization Should be Automated Can make your own tools Research is new, so no tools

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

Page 22: Chapter 1. Earth Scientists Deal with Large Datasets Processing and Visualization Should be Automated Can make your own tools Research is new, so no tools

Variable Name RulesVariable names are very flexible, but must follow these rules:

1. 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

2. There is a limit to the length of a variable name. Typically ≤ 64 characters>> namelengthmax

3. MATLAB is case-sensitive, so capitalization matters>> mynum = 4>> MYNUM = 6>> myNum = 8

4. Avoid underscores. Use mixed case>> my_num = 5 %probably works, but could cause problems>> myNum = 5 %much safer…still easy to read

5. Certain words are reserved and cannot be used as variable names>> iskeyword %gives a list of reserved words>> for = 4 %this is not allowed

6. Names of built-in functions should not be used as variable names>> sin = 2 %technically allowed, but is bad programming style & may cause problems

7. Variable names should be mnemonic & short>> aabcaded = 2 %what is aabcaded?>> temperatureInTopekaKansasAug19RecordedByChrisSmithTannerTheSecond=2 %Yuk! Too long!>> tempKS = 2 %much better

Page 23: Chapter 1. Earth Scientists Deal with Large Datasets Processing and Visualization Should be Automated Can make your own tools Research is new, so no tools

Basic MATLAB UsageThings 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!

Page 24: Chapter 1. Earth Scientists Deal with Large Datasets Processing and Visualization Should be Automated Can make your own tools Research is new, so no tools

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

Page 25: Chapter 1. Earth Scientists Deal with Large Datasets Processing and Visualization Should be Automated Can make your own tools Research is new, so no tools

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’)

Page 26: Chapter 1. Earth Scientists Deal with Large Datasets Processing and Visualization Should be Automated Can make your own tools Research is new, so no tools

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

Page 27: Chapter 1. Earth Scientists Deal with Large Datasets Processing and Visualization Should be Automated Can make your own tools Research is new, so no tools

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

or

σ=[ σ11 σ 12 σ 13σ 21 σ 22 σ 23σ 31 σ 32 σ 33

]The stress matrix

The stress cube(i.e. cool stuff!)

Also: -Strain, -Hydrologic conductivity-Earthquake Energy ReleaseEtc…

-Wind directions,-Stream flow direction-Plate Motions-Fault slip-Glacier motionEtc…

Page 28: Chapter 1. Earth Scientists Deal with Large Datasets Processing and Visualization Should be Automated Can make your own tools Research is new, so no tools

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’

Page 29: Chapter 1. Earth Scientists Deal with Large Datasets Processing and Visualization Should be Automated Can make your own tools Research is new, so no tools

Matrix Construction: The BasicsMatrix 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…

Page 30: Chapter 1. Earth Scientists Deal with Large Datasets Processing and Visualization Should be Automated Can make your own tools Research is new, so no tools

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; 2 5; 3 6]>> mat3x2 = [1, 4; 2, 5; 3, 6]

Page 31: Chapter 1. Earth Scientists Deal with Large Datasets Processing and Visualization Should be Automated Can make your own tools Research is new, so no tools

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…

Page 32: Chapter 1. Earth Scientists Deal with Large Datasets Processing and Visualization Should be Automated Can make your own tools Research is new, so no tools

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 lists1. 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

2. “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?

Page 33: Chapter 1. Earth Scientists Deal with Large Datasets Processing and Visualization Should be Automated Can make your own tools Research is new, so no tools

Referring to Matrix Elementstest

• 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:

Page 34: Chapter 1. Earth Scientists Deal with Large Datasets Processing and Visualization Should be Automated Can make your own tools Research is new, so no tools

Referring to Matrix Rows/Columnstest

• 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

Page 35: Chapter 1. Earth Scientists Deal with Large Datasets Processing and Visualization Should be Automated Can make your own tools Research is new, so no tools

Replacing/Removing Matrix Elementstest 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?

Page 36: Chapter 1. Earth Scientists Deal with Large Datasets Processing and Visualization Should be Automated Can make your own tools Research is new, so no tools

Concatenating MatricesSometimes 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?

Page 37: Chapter 1. Earth Scientists Deal with Large Datasets Processing and Visualization Should be Automated Can make your own tools Research is new, so no tools

Matrix Size and Dimensionstest 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, :))

Page 38: Chapter 1. Earth Scientists Deal with Large Datasets Processing and Visualization Should be Automated Can make your own tools Research is new, so no tools

Reshaping, Rotating, and Flippingtest 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.

Page 39: Chapter 1. Earth Scientists Deal with Large Datasets Processing and Visualization Should be Automated Can make your own tools Research is new, so no tools

Using Functions with MatricesNearly 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)’

Page 40: Chapter 1. Earth Scientists Deal with Large Datasets Processing and Visualization Should be Automated Can make your own tools Research is new, so no tools

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 =