![Page 1: StatLab Workshop Yale University Maximiliano Appendino, Economics October 18 th, 2013](https://reader036.vdocuments.mx/reader036/viewer/2022062511/551c3ae2550346ea388b4941/html5/thumbnails/1.jpg)
Introduction to MatlabStatLab Workshop
Yale University
Maximiliano Appendino, Economics
October 18th, 2013
![Page 2: StatLab Workshop Yale University Maximiliano Appendino, Economics October 18 th, 2013](https://reader036.vdocuments.mx/reader036/viewer/2022062511/551c3ae2550346ea388b4941/html5/thumbnails/2.jpg)
What is Matlab?Matlab is a matrix-based tool for numerical
computationsPowerful Easy to use
Programming language Interactive environmentLots of available toolboxes
![Page 3: StatLab Workshop Yale University Maximiliano Appendino, Economics October 18 th, 2013](https://reader036.vdocuments.mx/reader036/viewer/2022062511/551c3ae2550346ea388b4941/html5/thumbnails/3.jpg)
Getting HelpUseful links:
http://statlab.stat.yale.edu/help/FAQ/matlab_FAQ.jsp
Mathworks' Getting Started Matlab CenterKermit Sigmon’s Matlab PrimerMany others
Google your questionMatlab’s help
OnlineStatlab Consultants: Max Perez Leon,
Zhentao Shi
![Page 4: StatLab Workshop Yale University Maximiliano Appendino, Economics October 18 th, 2013](https://reader036.vdocuments.mx/reader036/viewer/2022062511/551c3ae2550346ea388b4941/html5/thumbnails/4.jpg)
Acquiring MatlabITS Software Library & Resources:
http://www.yale.edu/its/software/Free for students: Matlab R2013aAvailable in Statlab locations
The Center for Science and Social Science Information 219 Prospect St, Basement Kline Biology Tower
Rosenkranz Hall 115 Prospect St, Room 01
![Page 5: StatLab Workshop Yale University Maximiliano Appendino, Economics October 18 th, 2013](https://reader036.vdocuments.mx/reader036/viewer/2022062511/551c3ae2550346ea388b4941/html5/thumbnails/5.jpg)
Launching MatlabDouble-click the “MATLAB R2013a” icon on
the desktopOr click the start bottom, type MATLAB and
enterUsual Interface:
Command WindowWorkplace
.MAT filesCommand historyCurrent Folder
![Page 6: StatLab Workshop Yale University Maximiliano Appendino, Economics October 18 th, 2013](https://reader036.vdocuments.mx/reader036/viewer/2022062511/551c3ae2550346ea388b4941/html5/thumbnails/6.jpg)
InterfaceCan be used as a calculator“help” gives you a list of all topics
“help topic” informs you about the particular topic
Example:>> help>> help stats>> help normcdf
![Page 7: StatLab Workshop Yale University Maximiliano Appendino, Economics October 18 th, 2013](https://reader036.vdocuments.mx/reader036/viewer/2022062511/551c3ae2550346ea388b4941/html5/thumbnails/7.jpg)
Entering MatricesEntered manually:
>> A = [1 2 3 4; 5 6 7 8; 9 10 11 12]
Generate it by built-in functions
Loaded from a file .MAT or Menu File Import Data…
![Page 8: StatLab Workshop Yale University Maximiliano Appendino, Economics October 18 th, 2013](https://reader036.vdocuments.mx/reader036/viewer/2022062511/551c3ae2550346ea388b4941/html5/thumbnails/8.jpg)
Matrix Operation+ addition- subtraction* multiplication^ power‘ transposeElement-by-element: preceded the operators
by .Subscripts:
>> B = A(2,3)
![Page 9: StatLab Workshop Yale University Maximiliano Appendino, Economics October 18 th, 2013](https://reader036.vdocuments.mx/reader036/viewer/2022062511/551c3ae2550346ea388b4941/html5/thumbnails/9.jpg)
Matrix OperationMatrix Multiplication
>> [1 2; 3 4]*[1 2; 3 4] [7 10; 15 22]
Element-by-element Multiplication>> [1 2; 3 4].*[1 2; 3 4]
[1 4;9 16]
![Page 10: StatLab Workshop Yale University Maximiliano Appendino, Economics October 18 th, 2013](https://reader036.vdocuments.mx/reader036/viewer/2022062511/551c3ae2550346ea388b4941/html5/thumbnails/10.jpg)
The Colon Operator : One of Matlab’s most important operators:
Portions of a matrix:>> C = A(:,3)>> D = A(1,:)>> E = A(1:2,1:3)
Create matrices using increments:>> F = [1:0.1:1.5]’ >> G = [1:0.1:1.5; 1:0.5:3.5]
![Page 11: StatLab Workshop Yale University Maximiliano Appendino, Economics October 18 th, 2013](https://reader036.vdocuments.mx/reader036/viewer/2022062511/551c3ae2550346ea388b4941/html5/thumbnails/11.jpg)
Matrix Generation FunctionsZeros:
>> zeros(3,3)Ones:
>> ones(3,3)Identity:
>> eye(3)More on matrices and linear algebra:
>> help elmat>> help matfun
![Page 12: StatLab Workshop Yale University Maximiliano Appendino, Economics October 18 th, 2013](https://reader036.vdocuments.mx/reader036/viewer/2022062511/551c3ae2550346ea388b4941/html5/thumbnails/12.jpg)
Random MatricesPseudo-Random numbers:
Change the seed:>> rng('shuffle')>> rng(15)
U[0,1] random variable:>> RU = rand(3,4)
Normal random variable:>> RN = randn(4,3)
![Page 13: StatLab Workshop Yale University Maximiliano Appendino, Economics October 18 th, 2013](https://reader036.vdocuments.mx/reader036/viewer/2022062511/551c3ae2550346ea388b4941/html5/thumbnails/13.jpg)
Matrix manipulationConcatenation:
>> A2 = [A A.^2; A./2 A]Deleting rows and columns:
>> A2(:,7:8) = []Adding rows and columns:
>> A2 = [1:2:11;A2]Knowing the size:
>> sizeA2 = size(A2)
![Page 14: StatLab Workshop Yale University Maximiliano Appendino, Economics October 18 th, 2013](https://reader036.vdocuments.mx/reader036/viewer/2022062511/551c3ae2550346ea388b4941/html5/thumbnails/14.jpg)
Suppressing OutputIf you simply type a statement and press
Enter Matlab automatically displays the results on the Command Window
If you end the line with a semicolon ; Matlab performs the computation but does not display any result>> H = rand(2,2)>> H = rand(2,2);>> H
![Page 15: StatLab Workshop Yale University Maximiliano Appendino, Economics October 18 th, 2013](https://reader036.vdocuments.mx/reader036/viewer/2022062511/551c3ae2550346ea388b4941/html5/thumbnails/15.jpg)
FunctionsMatlab provides a large number of standard
elementary mathematical functions:>> abs(-10)>> sqrt(9)>> x = [0:0.1:2*pi];>> y = sin(y);
Look for the ones you need: Google>> help
![Page 16: StatLab Workshop Yale University Maximiliano Appendino, Economics October 18 th, 2013](https://reader036.vdocuments.mx/reader036/viewer/2022062511/551c3ae2550346ea388b4941/html5/thumbnails/16.jpg)
GraphicsMatlab generate 2-dimensional plots easily:
>> plot(x,y)
>> y2 = y + 0.25;>> y3 = y + 0.50;>> plot(x,y,x,y2,x,y3)
With a friendly interface to edit them
![Page 17: StatLab Workshop Yale University Maximiliano Appendino, Economics October 18 th, 2013](https://reader036.vdocuments.mx/reader036/viewer/2022062511/551c3ae2550346ea388b4941/html5/thumbnails/17.jpg)
GraphicsAlso 3-dimensional ones:
First we need to generate a grid:>> [X,Y] = meshgrid([-2:.2:2]);
Second we can calculate the function to graph:>> Z = X.*exp(-X.^2-Y.^2);
Finally the graph:>> surf(X,Y,Z)
![Page 18: StatLab Workshop Yale University Maximiliano Appendino, Economics October 18 th, 2013](https://reader036.vdocuments.mx/reader036/viewer/2022062511/551c3ae2550346ea388b4941/html5/thumbnails/18.jpg)
ProgrammingM-files contain Matlab code.m is their extensionMatlab editorCan be used as any command or function as
long as they are in the “Current Folder”Two types:
ScriptsFunctions
![Page 19: StatLab Workshop Yale University Maximiliano Appendino, Economics October 18 th, 2013](https://reader036.vdocuments.mx/reader036/viewer/2022062511/551c3ae2550346ea388b4941/html5/thumbnails/19.jpg)
ScriptsA list of code grouped together
It does not accept argument or return outputUse them to register your workExample:
File New Scriptdisp(‘Hello’)File Save test.m>> test
![Page 20: StatLab Workshop Yale University Maximiliano Appendino, Economics October 18 th, 2013](https://reader036.vdocuments.mx/reader036/viewer/2022062511/551c3ae2550346ea388b4941/html5/thumbnails/20.jpg)
FunctionsFunctions are M-files that can accept input
arguments and return output arguments. The M-file and the function should have the
same nameExample:
function ar = area(radius)ar = pi*radius.^2;File Save area.m>> area(2)
![Page 21: StatLab Workshop Yale University Maximiliano Appendino, Economics October 18 th, 2013](https://reader036.vdocuments.mx/reader036/viewer/2022062511/551c3ae2550346ea388b4941/html5/thumbnails/21.jpg)
Flow ControlMatlab has the standard flow controls
If statements
For loops
While loops
![Page 22: StatLab Workshop Yale University Maximiliano Appendino, Economics October 18 th, 2013](https://reader036.vdocuments.mx/reader036/viewer/2022062511/551c3ae2550346ea388b4941/html5/thumbnails/22.jpg)
If statementa = 10;b = 11;
if a > bdisp('greater‘)
elseif a < bdisp('less‘)
elsedisp('equal‘)
end
>> [a==b]
![Page 23: StatLab Workshop Yale University Maximiliano Appendino, Economics October 18 th, 2013](https://reader036.vdocuments.mx/reader036/viewer/2022062511/551c3ae2550346ea388b4941/html5/thumbnails/23.jpg)
For loopsbetavec = zeros(100,1);beta = 0.925;a = [1:100];for i = 1:100
betavec(i)=beta^a(i);endplot(betavec)But you should avoid for loops if possible:
>> newbetavec=beta.^a
![Page 24: StatLab Workshop Yale University Maximiliano Appendino, Economics October 18 th, 2013](https://reader036.vdocuments.mx/reader036/viewer/2022062511/551c3ae2550346ea388b4941/html5/thumbnails/24.jpg)
While loopsThe previous example would be:betavec = zeros(100,1);beta = 0.925;a = [1:100];i = 1;while i < 101
betavec(i)=beta^a(i);i = i + 1;
endplot(betavec)
![Page 25: StatLab Workshop Yale University Maximiliano Appendino, Economics October 18 th, 2013](https://reader036.vdocuments.mx/reader036/viewer/2022062511/551c3ae2550346ea388b4941/html5/thumbnails/25.jpg)
Solving problemsCheck if somebody else has already solved it
Matlab itselfAnybody using Matlab
Solve it by yourselfUse Matlab’s matrix processing capacity as
much as you canBe organized with your code
![Page 26: StatLab Workshop Yale University Maximiliano Appendino, Economics October 18 th, 2013](https://reader036.vdocuments.mx/reader036/viewer/2022062511/551c3ae2550346ea388b4941/html5/thumbnails/26.jpg)
Thank you!
Questions, Comments,
Problems to solve?