Beruflich Dokumente
Kultur Dokumente
Matlab Introduction
MAT(rix) LAB(oratory)
environment for algorithm development, data visualization, data analysis, and numeric computation
Matlab Interface
Editor
Workspace
Current Directory
Matlab Interface
Current Directory show the working directory and files
inside the directory Editor - text-editor where a user can create Matlab (*.m) m-files Command Window where a user can issue and call matlab commands at the prompt Workspace shows the current variables and data loaded into the matlab memory Command History shows a list of previously called or issued commands
Creating Variables
Matlab variables can accept any data at creation
Creating Matrices
1-dimensional matrix (row vector) A = [1 2 3 4 5] Use semicolon to separate data to rows B = [1 2 3; 4 5 6; 7 8 9] Other special matrices ones(m,n) Zeros(m, n) Rand(m, n)
m = number of rows n = number of columns
Matrix Operations
Try it! : Create a 3x3 Matrix C of ones! Adding matrices B+C Ans: 2 3 4 5 6 7 8 9 10 TIP: Make sure B and C has the same size Details the same with subtracting matrices (B C) To transpose matrices, use a quote ( ) Try it! Make Matrix A a column vector
Matrix Operations
Matrix Multiplication Inner products between row and columns ( * ) operator Example
B*
B
30 36 42 66 81 96 102 126 150
Matrix Operations
Element-wise multipication
( .* ) dot-asterisk operation
Example
B .*
B
1 16 49 4 9 25 36 64 81
Matrix Operations
Matrix Division
( / ) or matrix right division: B / A = B * inv(A) ( \ ) or matrix left division:
A \ B = inv(A) * B ( ./ ) or array right division: A./B = A/B (element-wise) ( .\ ) or array left division: A.\B = B/A (element-wise)
Matrix Operation
Concatenation [B, B]
1 4 7
2 5 8
3 6 9
1 4 7
2 5 8
3 6 9
1 4 7 1 4 7
2 5 8 2 5 8
3 6 9 3 6 9
Array Indexing
Used to select a part of an array, whether a smaller matrix or a
Array Indexing
Try it! Select and display only the 3rd row Select and display the 3rd and 4th column Select and display the smaller matrix in the 4th quadrant
of rows and columns Repmat: Replicates and tiles the input array Str2num / Num2str : converts strings to numbers /vice versa, respectively
Useful when copying numerical data from external sources
variable M NOTE: data should not have header names (else, error would occur)
M = importdata(filename, delimeter, number_header_lines)
Importdata more general and flexible compared to csvread
Need not be comma-separated Delimeter character that separates the data (space or comma) Number_header_lines which row data actually starts
Open/LibreOffice Calc) and copy the data to memory clipboard will paste these data into Matlab workspace Data would be separated to texts and numbers
Plotting Data
To visualize your data, we can use plots
Given a vector of values y, call plot(y) The x-axis would be the index of the value If you have values for the x-axis, call plot(x,y)
Lets try plotting data! X = 1:10 Y = rand(10,1) * 10; Plot the data
Plotting Data
3-dimensional data For plotting 3-dimensional data, we can use the following
Z = peaks; % lets create 2-dimensional dummy data surf (To create surface graphs ) surf(Z) surf(X, Y, Z) (if you have value for the X- and Y-axis) Contour contour(Z) contour(X, Y, Z) (if you have value for the X- and Y-axis)
surfc (create surface plot and contour plot at the bottom) surfc(Z) surf(X, Y, Z) (if you have value for the X- and Y-axis)
mesh (similar to surf but retains only the wireframe) mesh(Z) mesh(X, Y, Z) (if you have value for the X- and Y-axis)
Control Flow
Control flows are important to direct the execution of your
else
disp('a is odd)
end
Control Flow
ELSE, ELSEIF a = randi(100, 1); if a < 30
disp('small')
elseif a < 80
disp('medium')
else
disp('large')
end
Control Flow
SWITCH
NOTE: For both IF and SWITCH, once the code enters and executes a
section by satisfying an IF or a CASE, the program exits the code block Lets Try! Create code by typing a number and telling the user if its a negative number, a positive number or zero. a = input(Type number: )
Control Flow
For Loop Runs a part of the code in a predetermined number of time
H = rand(10); [m, n] = size(H);
for i = 1:m
for j = 1:n
Control Flow
While Loop Runs the code until a certain condition is met
Code to find the zero of f(x) = x3-2x -5 between 0 and 3
Control Flow
Continue Goes to the next iteration of the Loop, skipping all the commands after it Code below counts the number of command lines in a Matlab mfile. It skips counting when a line in the file is not considered a code (either a comment/ %, invalid character or is a whitespace)
Control Flow
Break Enables early exit in a loop execution Code below is similar to the While-Loop but break was utilized
Control Flow
Return Break the flow of the program and gives back the control to the main function, will skip all the succeeding parts of the code. Usually found at the end of a function to return control to the invoking function Can also be inserted in middle part of the code to cause premature exit of execution
Format:
Resources
Matlab Primer (www.mathworks.com)