I don’t know how to make them.. Function name = filenameCan have additional hidden functions... Files: Scripts and... THE INTERFACECurrent Directory / Workspace Command Window Interactiv
Trang 1Matlab Tutorial
Joseph E Gonzalez
Trang 3Why we use it?
Trang 5Matlab Language
Nap Time
Z
Trang 7Logic and Assignment
% Assignment with equality
ans: 1 % 1 is true in Matlab
not( a == 6 ) also works
Trang 11More Making Matrices
% Creating all ones, zeros, or identity matrices
>> zeros( rows, cols )
>> ones( rows, cols )
>> eye( rows )
% Creating Random matrices
>> rand( rows, cols ) % Unif[0,1]
>> randn( rows, cols) % N(0, 1)
% Make 3x5 with N(1, 4) entries
>> 1 + 2 * randn(3,5)
% Get the size
>> [rows, cols] = size( matrix );
Trang 12Array and Matrix Indices
Start at 1 not 0.
(Fortran)
Trang 14-1 5 -1
7 -1 9
Trang 15>> A / A
ans: 1 1 1
1 1 1
1 1 1
Trang 16Matrix Math 2
% Make a matrix
>> A = [1 2 3; 4 5 6; 7 8 9]ans: 1 2 3
Trang 1716 25 36
49 64 81
Trang 18% Solving Systems
>> (A + eye(3)) \ [1;2;3] % inv(A + eye(3)) * [1; 2; 3]ans: -1.0000
-0.0000 1.0000
Trang 21% Provide a convenient tool to organize variables
% Create Structs on the fly
Trang 22You can make objects but
you won’t need them.
I don’t know how to make them most people don’t use them
Trang 23If statements
% If Statements
>> c = rand();
>> if (c > 5) %% conditional disp(‘Greater than’);
Trang 24% Avoid using for loops
>> count = sum( data(:,1) == 4 & data(:,3) == 2 )
% How would you compute the outer product of a row vector?
>> repmat(x, length(x), 1) * repmat(x’, 1,length(x))
Outer Product of row vector x
Trang 25Function name = filename
Can have additional (hidden) functions
Trang 26Files: Scripts and
Trang 27>> yans: 4
>> xpans: 5
Trang 29THE INTERFACE
Current Directory
/ Workspace
Command Window Interactive Shell Recent
Commands
Trang 30>> pwd ans = /Users/jegonzal/tutorial
>> cd
>> pwd ans = /Users/jegonzal
ls : List Directory Contents
pwd : View Current directory
cd : Change Directory
Trang 31Other Commands
% Get help on a function
>> help <function name>
% List names of variables in the environment
Trang 32Help organize your programs
Can only call functions and scripts in:
The present working directory (pwd)
The Matlab path (path)
Call functions and scripts by typing name
>> my_script
>> y = my_function(x)
Trang 33GO PLAY WITH THE COMMAND WINDOW
Trang 34EDITOR
Trang 35Insert break points
Click to the left of the
line (Red Circle)
Use interactive shell
K>> K>> beta beta =
1 -5 6
Trang 36Walk Through Interface