Introduction To MATLAB - Appalachian State University
Download
Report
Transcript Introduction To MATLAB - Appalachian State University
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 may exist
Above: > 1,000,000 Persistent-Scatterer InSAR Data Points in SoCal
• 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
• 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
• 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 nonnumeric data*
*In this instructor’s opinion.
Slip rate vectors on the Hollywood fault, CA
• 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
The Command Window
Command History
Workspace
(List/Info of defined variables)
Current Folder
& File Info
Current Folder (clickable)
The current folder is important to pay
attention to!!!
1.
2.
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
• 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
• 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 “/”
• 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’)
• 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’)
• 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
• 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 ../
• 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
• 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
• Let’s try it out!
>> cd
>> pwd
>> ls
>> movefile
>> copyfile
>> mkdir
>> delete
>> rmdir
>> %comments
• 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
Variable 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
>> for = 4
6.
Names of built-in functions should not be used as variable names
>> sin = 2
7.
%gives a list of reserved words
%this is not allowed
%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
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!
• 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
• 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’)
• 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
• MATLAB is short for Matrix Laboratory
• It is specifically designed for vector and matrix operations
• Vectors and matrices are VERY useful in Earth sciences!
𝑨=
𝟏
𝟐
𝑩=
𝟏
−𝟑
σ11
σ = σ21
σ31
or
or
σ12
σ22
σ32
𝟏
𝟐
𝟏
−𝟑
σ13
σ23
σ33
The stress matrix
-Wind directions,
-Stream flow direction
-Plate Motions
-Fault slip
-Glacier motion
Etc…
The stress cube
(i.e. cool stuff!)
Also:
-Strain,
-Hydrologic conductivity
-Earthquake Energy Release
Etc…
• Defining vectors in MATLAB is straightforward
• Must use square brackets []
• Called “Arrays” in most other programming languages
• rowVect = 1 2 3
>> rowVect = [1 2 3]
>> rowVect = [1, 2, 3]
4
• columnVect = 7
9
>> columnVect = [4; 7; 9]
• Turn a row vector into a column vector using apostrophe ‘
• Mathematically, this is called the transpose
>> columnVect = rowVect’
Matrix construction follows specific
mathematical rules:
• Dimensions [m x n]
• m = rows (vertical)
• n = columns (horizontal)
All numerical quantities are technically
matrices!
1 = 1x1 matrix (scalar)
1
= 2x1 matrix (column vector)
2
1 2 3 = 1x3 matrix (row vector)
1 3
= 2x2 matrix (square)
2 4
1
2
3
4
5
6
= 4x2 matrix (rectangular)
7
8
The matrix has you…
• Defining matrices in MATLAB is also straightforward
• Again, use square brackets
• Mat2x2 =
1 3
2 4
>> mat2x2 = [1 3; 2 4]
>> mat2x2 = [1, 3; 2, 4]
• mat2x3 =
1
2
3 5
4 6
>> mat2x3 = [1 3 5; 2 4 6]
>> mat2x3 = [1, 3, 5; 2, 4, 6]
1
• mat3x2 = 2
3
4
5
6
>> mat3x2 = [1 4; 2 5; 3 6]
>> mat3x2 = [1, 4; 2, 5; 3, 6]
• 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
>> A .* B
>> A ./ B
>> A .^ B
%matrix multiplication
%array multiplication
%array division
%array to a power
• We’ll do some basic linear algebra later on…
• 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
1. 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?
𝟗
𝟑
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)
(1,1)
(2,1)
(row,col) notation:
(3,1)
(4,1)
(1,2)
(2,2)
(3,2)
(4,3)
(1,3)
(2,3)
(3,3)
(4,3)
(1)
(2)
Linear index:
(3)
(4)
(5)
(6)
(7)
(8)
(9)
(10)
(11)
(12)
𝟗
𝟑
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, :)
>> test(2, :)
%This returns what value?
• 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
𝟗
𝟑
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, :)
>> test(2, :)
%This returns what value?
• 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?
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?
𝟗
𝟑
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, :))
𝟗
𝟑
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.
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)’
• 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 =