0% found this document useful (0 votes)
62 views40 pages

Introduction To Matlab

This document introduces MATLAB and provides reasons for learning to code. It discusses commonly used programming languages in earth sciences, with a focus on MATLAB's strengths and weaknesses. MATLAB is well documented and its commands can be accessed through online or command window help. The document provides an overview of basic MATLAB usage, including starting MATLAB, file operations, assigning variables, and generating random numbers. It also introduces vectors and matrices as fundamental data types in MATLAB.

Uploaded by

Monica
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
62 views40 pages

Introduction To Matlab

This document introduces MATLAB and provides reasons for learning to code. It discusses commonly used programming languages in earth sciences, with a focus on MATLAB's strengths and weaknesses. MATLAB is well documented and its commands can be accessed through online or command window help. The document provides an overview of basic MATLAB usage, including starting MATLAB, file operations, assigning variables, and generating random numbers. It also introduces vectors and matrices as fundamental data types in MATLAB.

Uploaded by

Monica
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 40

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


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


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


Starting MATLAB

The Command Window


Starting MATLAB

Command History
Starting MATLAB

Workspace
(List/Info of defined variables)
Starting MATLAB

Current Folder
& File Info
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
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
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 “/”
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’)
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’)
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
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 ../
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
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
MATLAB Time
• Let’s try it out!
>> cd
>> pwd
>> ls
>> movefile
>> copyfile
>> mkdir
>> delete
>> rmdir
>> %comments
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
Variable Name Rules
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 %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
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!


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
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’)
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
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
 or -Glacier motion
Etc…

The stress cube


(i.e. cool stuff!)
  σ 11 σ 12 σ 13

[
σ = σ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 =

>> columnVect = [4; 7; 9]

• Turn a row vector into a column vector using apostrophe ‘


• Mathematically, this is called the transpose
>> columnVect = rowVect’
Matrix Construction: The Basics
•   construction follows specific
Matrix
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…


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]
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…
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
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?


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:


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
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?
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?
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, :))
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.
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)’
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)
mat3D =
[ 𝟏𝟎
𝟏𝟐
𝟓
  𝟑
𝟓
𝟒
𝟕
𝟎
𝟖
𝟑
𝟔
]
𝟏𝟎
>> new = mat3D(:, :, 3)

[ 𝟔
𝟗
𝟏𝟐
𝟐
𝟒
𝟕
𝟓
𝟖
𝟏
]

You might also like