david s. touretzky september 2019 day 1: essentials
TRANSCRIPT
![Page 1: David S. Touretzky September 2019 Day 1: Essentials](https://reader031.vdocument.in/reader031/viewer/2022020916/61addd24721fb55d5b341706/html5/thumbnails/1.jpg)
1
CNBC Matlab Mini-Course
David S. TouretzkyOctober 2021
Day 1: Essentials
![Page 2: David S. Touretzky September 2019 Day 1: Essentials](https://reader031.vdocument.in/reader031/viewer/2022020916/61addd24721fb55d5b341706/html5/thumbnails/2.jpg)
2
What Is Matlab?
● Product of The Mathworks, Inc.
http://www.mathworks.com
● Runs on Linux, Windows, and Macs.● Student version just $99 (plus toolboxes).● Latest release is Matlab R2021b.● “Interactive” interface like BASIC, Python, Lisp,
etc. Type in expressions and see the result.
![Page 3: David S. Touretzky September 2019 Day 1: Essentials](https://reader031.vdocument.in/reader031/viewer/2022020916/61addd24721fb55d5b341706/html5/thumbnails/3.jpg)
3
What Is Matlab? (cont.)
● Full programming language.● Strong on matrix manipulation and graphics.● Optional toolboxes for statistics, image
processing, signal processing, etc.● Interfaces with C, Fortran, and Java.● Can create stand-alone executable files.
– HHsim, a Hodgkin-Huxley simulator developed by Dave Touretzky with help from Jon Johnson, is distributed as a stand-alone executable. (Source is also available.)
![Page 4: David S. Touretzky September 2019 Day 1: Essentials](https://reader031.vdocument.in/reader031/viewer/2022020916/61addd24721fb55d5b341706/html5/thumbnails/4.jpg)
4
Why Should You Learn Matlab?
● Data analysis:● Much more versatile than a spreadsheet.● Extensive statistics toolbox.● SPM uses Matlab.
● Graphics:● Many ways to visualize your data – even animations!● Produce great figures for your papers.
● Modeling and simulation:● Best choice for neural net simulations.
![Page 5: David S. Touretzky September 2019 Day 1: Essentials](https://reader031.vdocument.in/reader031/viewer/2022020916/61addd24721fb55d5b341706/html5/thumbnails/5.jpg)
5
Getting Started
● Log in to a workstation.● Go to the menu bar at the top of your screen
and select:
Applications > Education > MATLAB
![Page 6: David S. Touretzky September 2019 Day 1: Essentials](https://reader031.vdocument.in/reader031/viewer/2022020916/61addd24721fb55d5b341706/html5/thumbnails/6.jpg)
6
Variable Creation
a = 5
a = 6 ;
b = 'penguins love herring'
who
whos
single quote
Click on the Workspace tab for a graphical
version of whos.
![Page 7: David S. Touretzky September 2019 Day 1: Essentials](https://reader031.vdocument.in/reader031/viewer/2022020916/61addd24721fb55d5b341706/html5/thumbnails/7.jpg)
7
Matrix Creation
x = [1 2 3 ; 9 8 7]
zeros(3, 5)zeros(5)zeros(5, 1) column vectorzeros(1, 5) row vector
ones, rand, randn, eye
What does eye do?
![Page 8: David S. Touretzky September 2019 Day 1: Essentials](https://reader031.vdocument.in/reader031/viewer/2022020916/61addd24721fb55d5b341706/html5/thumbnails/8.jpg)
8
Colon Creates Row Vectors
1 : 5
1 : 3 : 15
10 : -1 : 0
pts = 0 : pi/20 : 4*pi;
![Page 9: David S. Touretzky September 2019 Day 1: Essentials](https://reader031.vdocument.in/reader031/viewer/2022020916/61addd24721fb55d5b341706/html5/thumbnails/9.jpg)
9
Size of a Matrix
whos pts
size(pts)
length(pts)
![Page 10: David S. Touretzky September 2019 Day 1: Essentials](https://reader031.vdocument.in/reader031/viewer/2022020916/61addd24721fb55d5b341706/html5/thumbnails/10.jpg)
10
Subscripting
V = [10 20 30 40 50];
V(3) index from 1, not 0
M = [1 2 3; 4 5 6; 7 8 9]
M(2,2)
M(2) access in column-major order
M(6)
M = 1 2 3 4 5 6 7 8 9
![Page 11: David S. Touretzky September 2019 Day 1: Essentials](https://reader031.vdocument.in/reader031/viewer/2022020916/61addd24721fb55d5b341706/html5/thumbnails/11.jpg)
11
Matrix Slices
V(2:4)
V(2:end)
M(1:2, 2:3)
M( : )
M(: , :)
![Page 12: David S. Touretzky September 2019 Day 1: Essentials](https://reader031.vdocument.in/reader031/viewer/2022020916/61addd24721fb55d5b341706/html5/thumbnails/12.jpg)
12
Expanding a Matrix
a = [1 2 3]
a = [a 4]
a(7) = 5
a(end+1) = 6
b = [a ; a.^2]
Efficiency tip:
Use ZEROS(rows,cols) to preallocate large arrays instead of growing them dynamically.
![Page 13: David S. Touretzky September 2019 Day 1: Essentials](https://reader031.vdocument.in/reader031/viewer/2022020916/61addd24721fb55d5b341706/html5/thumbnails/13.jpg)
13
Reshaping a Matrix
M = reshape(1:15, 5, 3)
M'
M' ' or (M')'
![Page 14: David S. Touretzky September 2019 Day 1: Essentials](https://reader031.vdocument.in/reader031/viewer/2022020916/61addd24721fb55d5b341706/html5/thumbnails/14.jpg)
14
Exercise
● Create the following matrix using only the colon, reshape, and transpose operators.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
![Page 15: David S. Touretzky September 2019 Day 1: Essentials](https://reader031.vdocument.in/reader031/viewer/2022020916/61addd24721fb55d5b341706/html5/thumbnails/15.jpg)
15
Adding Rows vs. Columns
M = [1 2 ; 3 4]
M = [M ; 5 6]
V = [10 20 30] '
M = [M V]
M = [M [99; 98; 97] ]
![Page 16: David S. Touretzky September 2019 Day 1: Essentials](https://reader031.vdocument.in/reader031/viewer/2022020916/61addd24721fb55d5b341706/html5/thumbnails/16.jpg)
16
Deleting Rows or Columns
M(: , 3) = [ ]
M(2, :) = [ ]
size([ ])
![Page 17: David S. Touretzky September 2019 Day 1: Essentials](https://reader031.vdocument.in/reader031/viewer/2022020916/61addd24721fb55d5b341706/html5/thumbnails/17.jpg)
17
Command Line Editing
● Arrow keys work like you expect● Basic Emacs commands also work:
Forward/back char ^F / ^BLeft/right word alt-F / alt-BBeginning/end of line ^A / ^EDelete forward/back char ^D / backspaceClear line ^UKill to end of line ^KUndo ^_
● Environ. > Preferences > Keyboard > Shortcuts for a list, or to switch to Windows conventions.
![Page 18: David S. Touretzky September 2019 Day 1: Essentials](https://reader031.vdocument.in/reader031/viewer/2022020916/61addd24721fb55d5b341706/html5/thumbnails/18.jpg)
18
Command Line History
● Scrolling through the command history:Move to previous command Move to next command ¯
● Can also double click (or click and drag) on an item in the Command History window
● Command/function completion: cle<tab>
● Interrupt execution: ^C
![Page 19: David S. Touretzky September 2019 Day 1: Essentials](https://reader031.vdocument.in/reader031/viewer/2022020916/61addd24721fb55d5b341706/html5/thumbnails/19.jpg)
19
Editing Files in Matlab
New > Script
Put 3+5 on the first line
Put m = magic(5) on the second line
Save the file as foo.m in the current directory.
Type foo in the Command Window
![Page 20: David S. Touretzky September 2019 Day 1: Essentials](https://reader031.vdocument.in/reader031/viewer/2022020916/61addd24721fb55d5b341706/html5/thumbnails/20.jpg)
20
Basic Plottingpts = 0 : pi/20 : 4*pi ;
plot(sin(pts))
plot(pts, sin(pts))
axis off / on
grid on / off
box off / on
whitebg(gcf, [0 0 0])
clf
clf reset
![Page 21: David S. Touretzky September 2019 Day 1: Essentials](https://reader031.vdocument.in/reader031/viewer/2022020916/61addd24721fb55d5b341706/html5/thumbnails/21.jpg)
21
Plot Labeling
pl^P
xlabel('Angle \theta')
ylabel('y = sin(\theta)')
title('The Sine Function')
![Page 22: David S. Touretzky September 2019 Day 1: Essentials](https://reader031.vdocument.in/reader031/viewer/2022020916/61addd24721fb55d5b341706/html5/thumbnails/22.jpg)
22
Multiple Plots
clf
hold on
plot(pts, sin(pts))
plot(pts, cos(pts), 'm')
plot(pts, cos(pts), 'go')
legend('sin', 'cos', 'pts')
Click and drag to position the legend.
![Page 23: David S. Touretzky September 2019 Day 1: Essentials](https://reader031.vdocument.in/reader031/viewer/2022020916/61addd24721fb55d5b341706/html5/thumbnails/23.jpg)
23
Summary of Plot Options
● Colors: r,g,b,w c,m,y,k● Symbols: . o x + * s(quare) d(iamond) etc.● Line type: - (solid), -- (dashed), : (dotted),
-. (dash-dot)
help plot
![Page 24: David S. Touretzky September 2019 Day 1: Essentials](https://reader031.vdocument.in/reader031/viewer/2022020916/61addd24721fb55d5b341706/html5/thumbnails/24.jpg)
24
Printing
● On the File pulldown menu, select Print.● Or type ^P in the figure window.
● Printing to a file:print -djpeg myfig.jpgprint -depsc -r300 myfig.psprint -dtiff myfig.tiff
● To learn more: help print
![Page 25: David S. Touretzky September 2019 Day 1: Essentials](https://reader031.vdocument.in/reader031/viewer/2022020916/61addd24721fb55d5b341706/html5/thumbnails/25.jpg)
25
Plotting With Error Bars
clf
y = sin(pts);
e = rand(1, length(y)) * 0.4;
errorbar(pts, y, e)
![Page 26: David S. Touretzky September 2019 Day 1: Essentials](https://reader031.vdocument.in/reader031/viewer/2022020916/61addd24721fb55d5b341706/html5/thumbnails/26.jpg)
26
Multiple Figures
figure
bar3(abs(peaks(7)))
figure(5)
delete(2)
Or type ^W in a figure window to close it.
![Page 27: David S. Touretzky September 2019 Day 1: Essentials](https://reader031.vdocument.in/reader031/viewer/2022020916/61addd24721fb55d5b341706/html5/thumbnails/27.jpg)
27
Histograms
dat = randn(10000, 1);
hist(dat)
hist(dat, 50)
b = hist(dat, 6)
bar(b)
![Page 28: David S. Touretzky September 2019 Day 1: Essentials](https://reader031.vdocument.in/reader031/viewer/2022020916/61addd24721fb55d5b341706/html5/thumbnails/28.jpg)
28
Writing Your Own Functions
New > Function
Save as parabola.m
Try: parabola(5)help parabolaclf, plot(parabola(-10 : 10),'r--s')
parabola Gives an error message. Why?
function [ y ] = parabola( x )% PARABOLA Computes a quadratic.% Y = parabola(X) May be called with a vector.y = x .^ 2;
![Page 29: David S. Touretzky September 2019 Day 1: Essentials](https://reader031.vdocument.in/reader031/viewer/2022020916/61addd24721fb55d5b341706/html5/thumbnails/29.jpg)
29
Scripts vs. Functions
● Scripts take no input arguments and produce no return values.
● Scripts operate in the workspace of their caller.
● If called from the command line, scripts operate in the base workspace.
● If called from within a function, scripts operate in the function's local workspace and can see and modify its local variables.
![Page 30: David S. Touretzky September 2019 Day 1: Essentials](https://reader031.vdocument.in/reader031/viewer/2022020916/61addd24721fb55d5b341706/html5/thumbnails/30.jpg)
30
Scripts vs. Functions
● Functions can take zero or more arguments and return zero or more values.
● Functions operate in their own local workspace.
● Variables created inside a function are local to that function.
● Local variables disappear when the function returns.
![Page 31: David S. Touretzky September 2019 Day 1: Essentials](https://reader031.vdocument.in/reader031/viewer/2022020916/61addd24721fb55d5b341706/html5/thumbnails/31.jpg)
31
Logical Operations
Operators: == ~= < > <= >=
Logical values: 0 means “false” 1 (or any non-zero number) means “true”
a = (3 >= 1 : 5) What are the type and size of a?
Can't use != as in Java or C
![Page 32: David S. Touretzky September 2019 Day 1: Essentials](https://reader031.vdocument.in/reader031/viewer/2022020916/61addd24721fb55d5b341706/html5/thumbnails/32.jpg)
32
Boolean Subscripting
V = [1 2 3 4 5];
V(logical([1 0 1 1 0]))
V( V >= 3 )
V( V >= 3) = 0
S = 'banana cabana'
S( S == 'a') = [ ]
![Page 33: David S. Touretzky September 2019 Day 1: Essentials](https://reader031.vdocument.in/reader031/viewer/2022020916/61addd24721fb55d5b341706/html5/thumbnails/33.jpg)
33
The IF Statement
if x >= 3 y = x;else y = x + 3; hadHelp = true;end
Short form – use commas or semicolons: if x>3, y=x; else y=x+3; hadHelp=true; end
Differences from C/C++/Java:
No ( ) parens around the condition expression.
No { } braces around thethen/else clauses.
Requires end keyword.
![Page 34: David S. Touretzky September 2019 Day 1: Essentials](https://reader031.vdocument.in/reader031/viewer/2022020916/61addd24721fb55d5b341706/html5/thumbnails/34.jpg)
34
Control Structure: FOR Loops
for i = 1 : 5 [ i i^2 ]end
clf, hold onfor x = pts plot(x, cos(x), 'kd') pause(1)end
(you can use ^C to terminate the loop)
![Page 35: David S. Touretzky September 2019 Day 1: Essentials](https://reader031.vdocument.in/reader031/viewer/2022020916/61addd24721fb55d5b341706/html5/thumbnails/35.jpg)
35
Control Structure: WHILE Loops
How quickly can a random accumulator reach 5?
accum = 0; steps = 0;
while accum < 5 steps = steps + 1; accum = accum + rand(1);end
steps, accum
![Page 36: David S. Touretzky September 2019 Day 1: Essentials](https://reader031.vdocument.in/reader031/viewer/2022020916/61addd24721fb55d5b341706/html5/thumbnails/36.jpg)
36
Element-Wise Arithmetic
Element-wise operators: + − .* ./ .^
M = rand(5,3)
M + 100
M .* 5 same as M * 5
M .* M not same as M * M
M ./ M
M .^ 2
Dot means“element-wise”
![Page 37: David S. Touretzky September 2019 Day 1: Essentials](https://reader031.vdocument.in/reader031/viewer/2022020916/61addd24721fb55d5b341706/html5/thumbnails/37.jpg)
37
Matrix Arithmeticm1 = rand(5,3)
m2 = rand(3, 5)
m1 * m2 (5×3) * (3×5) (5×5)m2 * m1 (3×5) * (5×3) (3×3)m1 * m1 Error! Shapes don't fit.m1 / m2 Error! Shapes don't fit.
m1' / m2
pinv(m1) (5×3) (3×5)
![Page 38: David S. Touretzky September 2019 Day 1: Essentials](https://reader031.vdocument.in/reader031/viewer/2022020916/61addd24721fb55d5b341706/html5/thumbnails/38.jpg)
38
Exercise: Data Plotting Script
x = 0 : pi/20 : 5*pi ;y = sin(x) + x/3 + randn(1,length(x))/4;z = smooth(y,20)' ;
clf, hold on
plot(x, y, 'bo--')
plot(x, z, 'm', 'LineWidth', 3)
Save as mydata.m and run it several times.
![Page 39: David S. Touretzky September 2019 Day 1: Essentials](https://reader031.vdocument.in/reader031/viewer/2022020916/61addd24721fb55d5b341706/html5/thumbnails/39.jpg)
39
Exercise (cont.)
Now add these additional lines:
maxL = [1, z(2:end) > z(1:end-1)] ;maxR = [z(1:end-1) > z(2:end), 1];localMax = maxL & maxR; % true if point is local maximum
px = x(localMax); px(2,:)=0; px(3,:)=NaN;pz = z(localMax); pz(2,:)=z(localMax); pz(3,:)=NaN;
plot(px, pz, 'r')
For homework: figure out how it works.
![Page 40: David S. Touretzky September 2019 Day 1: Essentials](https://reader031.vdocument.in/reader031/viewer/2022020916/61addd24721fb55d5b341706/html5/thumbnails/40.jpg)
40
Reduction Operators
M = rand(5, 3)
sum(M)
sum(M, 2) sum along 2nd dimension
sum, prod, min, max, mean, var
min(min(M))
min( M(:) )
![Page 41: David S. Touretzky September 2019 Day 1: Essentials](https://reader031.vdocument.in/reader031/viewer/2022020916/61addd24721fb55d5b341706/html5/thumbnails/41.jpg)
41
Expanding with REPMAT
● REPMAT is often used to expand a vector to fit the shape of a matrix.
● Example: adjusting a dataset to have zero mean.
M = rand(5, 3)
avgs = mean(M)
Mavgs = repmat(avgs, 5, 1)
Mzero = M – Mavgs
sum(Mzero)
![Page 42: David S. Touretzky September 2019 Day 1: Essentials](https://reader031.vdocument.in/reader031/viewer/2022020916/61addd24721fb55d5b341706/html5/thumbnails/42.jpg)
42
Exercise
● Suppose we want the rows of M to sum to zero, instead of the columns.
● How would you do this, without using the transpose operator?
![Page 43: David S. Touretzky September 2019 Day 1: Essentials](https://reader031.vdocument.in/reader031/viewer/2022020916/61addd24721fb55d5b341706/html5/thumbnails/43.jpg)
43
Matlab Documentation
help cos
doc cos
clf, peaks
click on rotate3D icon
which peaks
edit peaks
lookfor rotate
Yes!You CAN see
oursource code!
![Page 44: David S. Touretzky September 2019 Day 1: Essentials](https://reader031.vdocument.in/reader031/viewer/2022020916/61addd24721fb55d5b341706/html5/thumbnails/44.jpg)
44
Browsing Online Documentation
● Press F1 to bring up the Documentation Browser
● In the documentation browser: > Statistics and Machine Learning Toolbox > Probability Distributions > Continuous Distributions > Beta Distribution > (Concepts) Beta Distribution
![Page 45: David S. Touretzky September 2019 Day 1: Essentials](https://reader031.vdocument.in/reader031/viewer/2022020916/61addd24721fb55d5b341706/html5/thumbnails/45.jpg)
45
MATLAB Primer, 8th ed.
Timothy A. Davis
CRC Press
$29.95 paper at Amazon
$20.30 ebook
Handy pocket reference.
![Page 46: David S. Touretzky September 2019 Day 1: Essentials](https://reader031.vdocument.in/reader031/viewer/2022020916/61addd24721fb55d5b341706/html5/thumbnails/46.jpg)
46
Introductory Text
Examines a variety of neuroscience applications, with examples.
![Page 47: David S. Touretzky September 2019 Day 1: Essentials](https://reader031.vdocument.in/reader031/viewer/2022020916/61addd24721fb55d5b341706/html5/thumbnails/47.jpg)
47
Ways To Learn Matlab
● Three more days of this mini-course.● Tutorial videos at mathworks.com● Built-in demos:
doc demo● Browse the online documentation● Dozens of books:
Amazon.com reports 7,900 search results!● Matlab Central: user community site
http://www.mathworks.com/matlabcentral● Questions to [email protected]