Introduction To Matlab
Introduction To Matlab
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: Mathematical Inversion of GPS data for strain in SoCal Above: Fault slip rates on the Sierra Madre fault in SoCal
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
Command History
Starting MATLAB
Workspace
(List/Info of defined variables)
Starting MATLAB
Current Folder
& File Info
Starting MATLAB
• int8, int16, int32, int64 (integer): Can save memory (in some
cases. See Ch1 of Attaway)
or
-Wind directions,
-Stream flow direction
-Plate Motions
-Fault slip
or -Glacier motion
Etc…
[
σ = σ21
σ 31
σ 22
σ 32
σ 23
σ 33 ] Also:
-Strain,
-Hydrologic conductivity
-Earthquake Energy Release
The stress matrix Etc…
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 =
• 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]
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
• 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?
Matrix Size and Dimensions
•
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)
[ 𝟔
𝟗
𝟏𝟐
𝟐
𝟒
𝟕
𝟓
𝟖
𝟏
]