Professional Documents
Culture Documents
Introduction to MATLAB:
2021
Outline
2 MATLAB Online
3 Overview
4 Variables
5 Matrix
6 Miscellaneous
2/29
How to install MATLAB?
1 Go to https://la.mathworks.com/
2 Click on icon
3/29
MATLAB Online
4/29
What & Why
• Matrix Laboratory
• Dynamically typed language
• Variables require no declaration
• Creation by initialization (x = 10; )
• All variables are treated as matrices
• Scalar: 1 × 1 matrix; Vector: N × 1 or 1 × N matrix
• Calculations are much faster
• Advantages
• Fast implementation and debugging
• Natural matrix operation
• Powerful image processing toolbox
5/29
MATLAB Environment
Current Directory
View Folders and m-�iles
Workspace
View variables
Double click on a variable
to see it in the Array Editor
Editor
Type commands in a Script
Command Window
Type commands
6/29
Defining Variables
C/C++ MATLAB
int a;
a = 1; >> a = 1;
double b; >> b = 2 + 4;
b = 2 + 4;
7/29
Variables
>> B = [1 2 3; 4 5 6]
B=
>> b = false; 1 2 3
4 5 6
8/29
Variables
>> C = [1 ; 2 ; 3] text
C=
1
2
3
>> D = [1 2 3]0
D=
1
2
3
9/29
Variables
>> A = 1 : 10
A=
1 2 3 4 5 6 7 8 9 10
>> B = 0 : 2 : 10
B=
0 2 4 6 8 10
>> 1 : 0.5 : 5
ans =
1.0000 1.5000 2.0000 2.5000 3.0000 3.5000 4.0000 4.5000 5.0000
C = 0 Hello World!0 ;
>> C =0 Hello World!0
C=
Hello World!
10/29
Variables
• A = zeros(3);
• B = ones(5);
• C = rand(100, 2);
• D = eye(20);
• E = sprintf(0 %d\n0 , 9)
11/29
Matrix Index
12/29
Matrix Index
13/29
Matrix Index
Accessing Elements
• A = rand(4);
• A(2, 3)
• A(:, 2)
• A(end, :)
• A([1, 2], [1, 3])
• A(1 : 2, 3 : end)
14/29
Matrix Operations
• + addition
• − subtraction
• ∗ multiplication
• ˆpower
• 0 complex conjugate transpose
15/29
Matrix Operations
Given A and B: A= B=
1 2 3 3 5 2
4 5 6 5 2 8
7 8 9 3 6 9
4 7 5 −2 −3 1 22 27 45 1 4 7
9 7 14 −1 3 −2 55 66 102 2 5 8
10 14 18 4 2 0 88 105 159 1 4 9
16/29
Matrix Operations
• .∗ element-wise multiplication
• ./ element-wise division
• .ˆelement-wise power
17/29
Matrix Operations
18/29
Matrix Operations
19/29
Matrix Concatenation
X = [1 2], Y = [3 4]
>> A = [X Y] >> A = [X ; Y]
A= A=
1 2 3 4 1 2
3 4
20/29
Strings
21/29
Cell and Structure
• Cells
• a = {}; a = cell(1)
• b = {1, 2, 3}
• c = {{1, 2}, 2, {3}}
• d = {’cat’, ’dog’, ’sheep’, ’cow’}
• {’cat’, 4}
• Structures
• A = struct(’name’, ’1.jpg’, ’height’, 640, ’width’, 480);
• b.name = ’1.jpg’
22/29
Operators
• == Equal to
• ∼= Not equal to
• < Strictly smaller
• > Strictly greater
• <= Smaller than or equal to
• >= Greater than or equal to
• & And operator
• | Or operator
23/29
Flow Control
24/29
Implementing Functions
How?
• Create a M-file with the function name
• Use the function definition at the beginning
function out1 = functionname(int1)
Some Matlab Commands;
function out1 = functionname(int1, int2, int3)
Some Matlab Commands;
function [out1, out2] = functionname(int1, int2)
Some Matlab Commands;
Hands-on!
Implement your own function that computes the sum of
the square of each element in a N × N matrix.
25/29
Plotting
Plotting functions
plot, plot3d, bar, area, hist, contour, mesh
>> x = −pi : 0.1 : pi; text
1
>> y = sin(x); 0.8
0.4
0.2
-0.2
-0.4
-0.6
-0.8
-1
-4 -3 -2 -1 0 1 2 3 4
26/29
Help & Doc
27/29