vectorized code
DESCRIPTION
Vectorized Code. Chapter 5. Vectorized Code: Speeding Up MATLAB. Loops are common in most programming languages Plus side: Are very fast (in other languages) & easy to understand Negative side: Require a lot of code Loops in MATLAB are not so fast - PowerPoint PPT PresentationTRANSCRIPT
![Page 1: Vectorized Code](https://reader034.vdocument.in/reader034/viewer/2022051118/56816530550346895dd7b64e/html5/thumbnails/1.jpg)
Vectorized Code Chapter 5
![Page 2: Vectorized Code](https://reader034.vdocument.in/reader034/viewer/2022051118/56816530550346895dd7b64e/html5/thumbnails/2.jpg)
Vectorized Code: Speeding Up MATLAB• Loops are common in most programming languages
• Plus side: Are very fast (in other languages) & easy to understand• Negative side: Require a lot of code
• Loops in MATLAB are not so fast• MATLAB provides a ton of ways to avoid using loops• For most problems (< 1e6 data, or so), loops are fast enough
• Vectorizing: The process of converting what would normally be done in a loop to use array operations, and/or built-in MATLAB functions
• The plus side: Code runs faster. Often easier to write• The negative side: All of this ONLY works in MATLAB
‘Snip!’
![Page 3: Vectorized Code](https://reader034.vdocument.in/reader034/viewer/2022051118/56816530550346895dd7b64e/html5/thumbnails/3.jpg)
Remember This One? Calculate a Sum• Lets mimic the
behavior of the MATLAB function “sum”
• Use a for loop
• Which is faster?• The loop• The built-in sum
function
• Why is the sum function provided?
![Page 4: Vectorized Code](https://reader034.vdocument.in/reader034/viewer/2022051118/56816530550346895dd7b64e/html5/thumbnails/4.jpg)
Race #1: Sum vs. For Loop• Use tic toc
to time each method for summing numbers• Both give
the same answer
![Page 5: Vectorized Code](https://reader034.vdocument.in/reader034/viewer/2022051118/56816530550346895dd7b64e/html5/thumbnails/5.jpg)
Race #1: Sum vs. For Loop• Use tic toc to time each
method for summing numbers• Both give the same answer
I pity the fool that doesn’t vectorize MATLAB code
![Page 6: Vectorized Code](https://reader034.vdocument.in/reader034/viewer/2022051118/56816530550346895dd7b64e/html5/thumbnails/6.jpg)
Refresher: Matrix Math RulesMatrix Addition/Subtraction• Both must have same
dimensions• Add each pair of corresponding
elements• Result has same dimensions• Can add a scalar to a matrix
• Would require a loop in most programming languages
• Is automatic in MATLAB• Called a “scalar operation”• Adds scalar to each element
![Page 7: Vectorized Code](https://reader034.vdocument.in/reader034/viewer/2022051118/56816530550346895dd7b64e/html5/thumbnails/7.jpg)
Refresher: Matrix Math RulesMatrix Multiplication/Division• If A is 3x2 and B is 2x3
[ 3 x 2 ] * [ 2 x 3 ]
• Red box: must be equal• Blue box: Result is 3x3
• Can multiply a matrix by a scalar• Would require a loop in most
programming languages• Is automatic in MATLAB• Called a “scalar operation”• Multiplies each element by the
scalar
![Page 8: Vectorized Code](https://reader034.vdocument.in/reader034/viewer/2022051118/56816530550346895dd7b64e/html5/thumbnails/8.jpg)
Array Operations• Say you wanted to multiply
each entry in one matrix by a corresponding value in another matrix
• In most programming languages, this would require a loop
• In MATLAB, you can use an array operation
• * = matrix multiplication• .* = array multiplication
![Page 9: Vectorized Code](https://reader034.vdocument.in/reader034/viewer/2022051118/56816530550346895dd7b64e/html5/thumbnails/9.jpg)
Race #2: Array Operation vs. For Loop• 100 million temp
measurements in °F• Convert to °F• Plots results to see
if results are the same
![Page 10: Vectorized Code](https://reader034.vdocument.in/reader034/viewer/2022051118/56816530550346895dd7b64e/html5/thumbnails/10.jpg)
Race #2: Array Operation vs. For Loop• 100 million temp
measurements in °F• Convert to °F• Plots results to see
if results are the same
I pity the fool that doesn’t pre-allocate matrices!!
![Page 11: Vectorized Code](https://reader034.vdocument.in/reader034/viewer/2022051118/56816530550346895dd7b64e/html5/thumbnails/11.jpg)
Race #2: Array Operation vs. For Loop• 100 million temp
measurements in °F• Convert to °F• Plots results to see
if results are the same
I pity the fool that doesn’t vectorize MATLAB code!!
![Page 12: Vectorized Code](https://reader034.vdocument.in/reader034/viewer/2022051118/56816530550346895dd7b64e/html5/thumbnails/12.jpg)
Vectorize This: Add Entries in a Matrix• Add 4 to all
entries in a vector• How could we
re-write this without using the loop?• Scalar operation!
![Page 13: Vectorized Code](https://reader034.vdocument.in/reader034/viewer/2022051118/56816530550346895dd7b64e/html5/thumbnails/13.jpg)
Vectorize This: Multiply Matrix Entries
• Multiply entries of two matrices together
• How could we re-write this without using the loop?• Use an array
operation!
![Page 14: Vectorized Code](https://reader034.vdocument.in/reader034/viewer/2022051118/56816530550346895dd7b64e/html5/thumbnails/14.jpg)
Vectorize This: Grab Column of Matrix
• Grab and store a column from a matrix
• How could we re-write this without using the loop?
![Page 15: Vectorized Code](https://reader034.vdocument.in/reader034/viewer/2022051118/56816530550346895dd7b64e/html5/thumbnails/15.jpg)
Vectorize This: Function Arguments
• What about function arguments?• Functions should be written
to handle either scalars or matrices/vectors
![Page 16: Vectorized Code](https://reader034.vdocument.in/reader034/viewer/2022051118/56816530550346895dd7b64e/html5/thumbnails/16.jpg)
Vectorize This: Function Arguments
• What about function arguments?• Functions should be written
to handle either scalars or matrices/vectors• How can we get rid of the
for loop?
![Page 17: Vectorized Code](https://reader034.vdocument.in/reader034/viewer/2022051118/56816530550346895dd7b64e/html5/thumbnails/17.jpg)
Vectorize This: Function Arguments
• What about function arguments?• Functions should be written to
handle either scalars or matrices/vectors
• Now it is vectorized!• Built-in MATLAB functions work
just like this• sin, cos, tan, sum, max, min,
etc…
![Page 18: Vectorized Code](https://reader034.vdocument.in/reader034/viewer/2022051118/56816530550346895dd7b64e/html5/thumbnails/18.jpg)
Logical VariablesCan we vectorize conditional statements? • Yes!
• Recall that MATLAB offers a variable type called “logical”
• Can only have two values
• 0 = False• 1 = True
![Page 19: Vectorized Code](https://reader034.vdocument.in/reader034/viewer/2022051118/56816530550346895dd7b64e/html5/thumbnails/19.jpg)
Logical Variables: A Quick Review
When converted to logical…• Any non-zero number
• 1 (true)
• Any zero number• 0 (false)
• You can perform mathematical operations on logical values, but they are automatically converted to doubles
![Page 20: Vectorized Code](https://reader034.vdocument.in/reader034/viewer/2022051118/56816530550346895dd7b64e/html5/thumbnails/20.jpg)
Logical Vectors: A Quick Overview
We can convert vectors of any numeric type to logical vectors• Any non-zero entry
• 1 (true)
• Any zero entry• 0 (false)
• You can index a vector by using a logical vector
• Only entries with non-zero entries are kept
![Page 21: Vectorized Code](https://reader034.vdocument.in/reader034/viewer/2022051118/56816530550346895dd7b64e/html5/thumbnails/21.jpg)
Logical MatricesWe can convert matrices of any numeric type to logical matrices• Any non-zero entry
• 1 (true)• Any zero entry
• 0 (false)
• You can index a matrix by using a logical vector
• Only entries with non-zero entries are kept
• Matrix is unwrapped and returned as a vector
• Why?
![Page 22: Vectorized Code](https://reader034.vdocument.in/reader034/viewer/2022051118/56816530550346895dd7b64e/html5/thumbnails/22.jpg)
Logical Vectors: A Word of Caution
Why does C not do what you expect?
The variable doing the indexing must of class=logical
![Page 23: Vectorized Code](https://reader034.vdocument.in/reader034/viewer/2022051118/56816530550346895dd7b64e/html5/thumbnails/23.jpg)
Vectorizing Conditional Statements• Using logical vectors,
we can vectorize conditional statements that would normally require a loop
![Page 24: Vectorized Code](https://reader034.vdocument.in/reader034/viewer/2022051118/56816530550346895dd7b64e/html5/thumbnails/24.jpg)
Vectorizing Conditional Statements• How can I test only
one column?
![Page 25: Vectorized Code](https://reader034.vdocument.in/reader034/viewer/2022051118/56816530550346895dd7b64e/html5/thumbnails/25.jpg)
Vectorize This: Conditional Statement• Loops through “dat”• Stores all values > 3
in “newDat”
![Page 26: Vectorized Code](https://reader034.vdocument.in/reader034/viewer/2022051118/56816530550346895dd7b64e/html5/thumbnails/26.jpg)
Vectorize This: Conditional Statement• Loops through “dat”• Stores all values > 3
in “newDat”
![Page 27: Vectorized Code](https://reader034.vdocument.in/reader034/viewer/2022051118/56816530550346895dd7b64e/html5/thumbnails/27.jpg)
Race #3: Logical Indexing vs. Loop + If
Finds:• Vals > 5 in col 1• Vals < 5 in col 2Prints times
![Page 28: Vectorized Code](https://reader034.vdocument.in/reader034/viewer/2022051118/56816530550346895dd7b64e/html5/thumbnails/28.jpg)
Race #3: Logical Indexing vs. Loop + If• Vectorized Code wins again
![Page 29: Vectorized Code](https://reader034.vdocument.in/reader034/viewer/2022051118/56816530550346895dd7b64e/html5/thumbnails/29.jpg)
Built-in Logical Functions• MATLAB provides
several built-in functions that perform logical tests
all, any, find, sign• You can read the
documentation for “all”, “any”, and “sign”
• Lets look at what find does
• Returns the linear index of all values that satisfy some condition
![Page 30: Vectorized Code](https://reader034.vdocument.in/reader034/viewer/2022051118/56816530550346895dd7b64e/html5/thumbnails/30.jpg)
Built-in Function: Diff• MATLAB provides a clever
function that calculates differences in adjacent data entries.
• “diff”
• Is VERY useful for calculating approximate derivatives
• Input matrix length=n• Output matrix length=n-1
![Page 31: Vectorized Code](https://reader034.vdocument.in/reader034/viewer/2022051118/56816530550346895dd7b64e/html5/thumbnails/31.jpg)
Built-in Function: Diff• Diff can also accept
matrices as arguments
• Returns the differences of successive rows
![Page 32: Vectorized Code](https://reader034.vdocument.in/reader034/viewer/2022051118/56816530550346895dd7b64e/html5/thumbnails/32.jpg)
Calculating Approximate Derivatives• Recall that a derivative is
just a slope• Exact analytical derivatives
are only possible for algebraic equations• For data, we cannot
calculate exact analytical derivatives
• We can calculate slopes!• Same is true for integrals.
We calculate areas under datasets.
• Why do I not need to calculate diff(x) in this case?
![Page 33: Vectorized Code](https://reader034.vdocument.in/reader034/viewer/2022051118/56816530550346895dd7b64e/html5/thumbnails/33.jpg)
Calculating Approximate Derivatives• What if data spacing ≠ 1?• Must calculate • Where should y’ data be
plotted?
![Page 34: Vectorized Code](https://reader034.vdocument.in/reader034/viewer/2022051118/56816530550346895dd7b64e/html5/thumbnails/34.jpg)
Calculating Approximate Derivatives“diff” can also calculate 2nd, 3rd, or nth derivatives• Lose one data point
per derivative
![Page 35: Vectorized Code](https://reader034.vdocument.in/reader034/viewer/2022051118/56816530550346895dd7b64e/html5/thumbnails/35.jpg)
1st Derivative Example• y’ = dy/dx = Slope = rise/run = diff(x) ./ diff(y)
• Note that the y’ values should be plotted at the midpoints of x
![Page 36: Vectorized Code](https://reader034.vdocument.in/reader034/viewer/2022051118/56816530550346895dd7b64e/html5/thumbnails/36.jpg)
1st Derivative Example• y’ = dy/dx = Slope = rise/run = diff(x) ./ diff(y)
• Note that the y’ values should be plotted at the midpoints of x
![Page 37: Vectorized Code](https://reader034.vdocument.in/reader034/viewer/2022051118/56816530550346895dd7b64e/html5/thumbnails/37.jpg)
2nd Derivative Example
• “diff” can also calculate approximate second or nth derivatives
• Note that each time you use diff, you lose one data point
• Where (at what x location) should second derivatives be plotted?
![Page 38: Vectorized Code](https://reader034.vdocument.in/reader034/viewer/2022051118/56816530550346895dd7b64e/html5/thumbnails/38.jpg)
2nd Derivative Example• y’ = dy/dx = Slope = rise/run = diff(x) ./ diff(y)
• y’ values should be plotted at the midpoints of x• y’’ plotted at x locations (excluding first and last point)
![Page 39: Vectorized Code](https://reader034.vdocument.in/reader034/viewer/2022051118/56816530550346895dd7b64e/html5/thumbnails/39.jpg)
Built-in Function: meshgrid• A common task in quantitative science is to evaluate 2D
or 3D spatial equations.• To do this, you need a 2D or 3D grid of (x,y,z) data points
• In most programming languages: nested for loops• In MATLAB: nested for loops, or the built-in function “meshgrid”
• Lets flashback to the Loops lecture notes…3D image of a carbonate reef
http://www.georgedreher.2e.com/3D_Seismic.html
3D image of Yucca Mountain unsaturated zonehttps://meshing.lanl.gov/
![Page 40: Vectorized Code](https://reader034.vdocument.in/reader034/viewer/2022051118/56816530550346895dd7b64e/html5/thumbnails/40.jpg)
Grid of XY Points?• This is NOT the way to do it!
• Lets try a nested for loop
![Page 41: Vectorized Code](https://reader034.vdocument.in/reader034/viewer/2022051118/56816530550346895dd7b64e/html5/thumbnails/41.jpg)
Grid of XY Points: Nested For Loops• To make a 2D grid we need a nested for loop
• Outer loop: x-range; Inner loop: y-range
• Could even make spherical grids
• (r, θ, ϕ)
![Page 42: Vectorized Code](https://reader034.vdocument.in/reader034/viewer/2022051118/56816530550346895dd7b64e/html5/thumbnails/42.jpg)
Grid of XY Points: meshgrid• To make a 2D grid we can also use the efficient built-in
function “meshgrid”
meshgrid will return:3 matrices for 3D 2 matrices for 2DYou must specify where to store all matrices, or you only get one!
![Page 43: Vectorized Code](https://reader034.vdocument.in/reader034/viewer/2022051118/56816530550346895dd7b64e/html5/thumbnails/43.jpg)
Grid of XY Points: meshgrid• meshgrid returns rectangular
matrices• Often, we want data in columns
• Col 1: X-Values; Col 2: Y-Values
• Use “reshape” to get in cols
![Page 44: Vectorized Code](https://reader034.vdocument.in/reader034/viewer/2022051118/56816530550346895dd7b64e/html5/thumbnails/44.jpg)
Grid of XYZ Points: meshgrid• meshgrid can also make 3D grids!
• Returns 3D matrices (refer to CH1 in Attaway & lecture notes)
meshgrid will also accept x,y,z ranges using the “linspace” function
![Page 45: Vectorized Code](https://reader034.vdocument.in/reader034/viewer/2022051118/56816530550346895dd7b64e/html5/thumbnails/45.jpg)
Grid of XY Points: meshgrid• Meshgrid will also accept ranges using the “linspace”
function
![Page 46: Vectorized Code](https://reader034.vdocument.in/reader034/viewer/2022051118/56816530550346895dd7b64e/html5/thumbnails/46.jpg)
Final ThoughtsMATLAB is a bit of an unusual programming language• Most languages rely heavily on loops
• So, anyone that knows how to code, knows loops well
• In MATLAB:• You can use loops (a little on the slow side)• You can avoid loops using vectorized code
What is best?• If data set is small (less than millions of points)
• Do whichever you prefer, or is easier to write/understand
• If data set is large or computation time is an issue• Use vectorized code, and built-in functions (when possible)