Professional Documents
Culture Documents
Introduction To MATLAB: Mechatronics Design Lab
Introduction To MATLAB: Mechatronics Design Lab
Introduction To MATLAB: Mechatronics Design Lab
Visualization
DIP Toolbox
Outline
(1) Getting Started
(2) Making Variables
(3) Manipulating Variables
(4) Basic Plotting
Getting Started
Most variables you’ll deal with will be arrays or matrices of doubles or chars
Images are stored and manipulated as 2-D arrays of real numbers
Command window:
Workspace:
Column Vectors
Column vector: semicolon separated values between
brackets
column = [4;2;7;4];
Command window:
Workspace:
Matrices
Make matrices like vectors
1 2
Element by element a
3 4
a= [1 2;3 4];
d = [a;b];
e = [d c];
f = [[e e];[a b a]];
Outline
(1) Getting Started
(2) Making Variables
(3) Manipulating Variables
(4) Basic Plotting
Basic Scalar Operations
Arithmetic operations (+,-,*,/)
7/45
(1+i)*(2+i)
1.1 – 0.1
Exponentiation (^)
4^2
(3+4*j)^2
>> x = pi/3;
>> a = exp(i*x);
>> b = cos(x) + i*sin(x)
>> a - b
size() & length()
You can tell the difference between a row and a column
vector by:
Looking in the workspace
Displaying the variable in the command window
Using the size() function –
shows Try this
>> column = [1; 2; 3; 4]
[(no. of rows) (no. of cols)] >> row = [5 6 7 8]
>> size(column)
>> size(row)
Try this
>> mat1 = [1+j 2+3*j]
>> mat1’
>> mat1.’
For vectors of real numbers .' and ' give same result
Addition and Subtraction
Addition and subtraction are element-wise; sizes must match (unless one is
a scalar):
12 3 32 11 12 3 9
1 1 2
2 11 30 32
10 13 23
14 14 2 21
0 33
33
Try this
>> row = [1 2 3 4]
>> column = [4; 5; 6; 7]
>> c = row + column
Error! Incompatible sizes. Now try this
>> c = row + column’
>> c = row’ + column
Try this
>> our_matrix = [1 2;3 4]
>> result = our_matrix + 5
Element-Wise Functions
All the functions that work on scalars also work on
vectors
t = [1 2 3];
f = exp(t);
is the same as
f = [exp(1) exp(2) exp(3)];
4 1 1 1 1 2 3 1 2 3
2 2 2 .* 1 2 3 2 4 6
1 2 3 .* 2 ERROR
1 3 3 3 1 2 3 3 6 9
1 4 4 3 3.* 3 3 3 3
2 .* 2 4
3 1 3
3 1.* 3 1 3 1 Try this
>> a = [1 3 3]; b = [4;2;1];
>> a.*b
1 2 12 22 >> a./b
3 4 .^ 2 2 2
>> a.^b
3 4 All Errors!
>> a.*b’
Can be any dimension >> a./b’
>> a.^(b’)
All Valid
Operators: standard
Multiplication can be done in a standard way or element-wise
Standard multiplication (*) is either a dot-product or an outer-product
Remember from linear algebra: inner dimensions must MATCH! (for matrix multiplication AxB,
number of columns of A = number of rows of B)
>> c = sin(b)
Automatic Initialization
Initialize a vector of ones, zeros, or random numbers
o=ones(1,10)
row vector with 10 elements, all with the value 1
z=zeros(23,1)
column vector with 23 elements, all 0
r=rand(1,45)
row vector with 45 elements with random values between interval [0,1]
n=nan(1,69)
row vector of NaNs (useful for representing uninitialized variables)
id = eye(5,5)
identity vector of 5x5
>> x = linspace(0,10,10000);
>> f = exp(-x).*cos(x);
Vector Indexing
Matlab indexing starts with 1, not 0
The index argument can be a vector. In this case, each element is looked up
individually, and returned as a vector of the same size as the index vector.
x = [12 13 5 8];
a = x(2:4); a = [13 5 8];
b = x(1:end-1); b= [12 13 5];
a=[-1 10 3 -2];
b=a([1 2 4;3 4 2]); 1 10 2 Try it
b
3 2 10
To select rows or columns of a matrix, use the colon :
12 5
c
2 13
d=c(1,:); d=[12 5];
e=c(:,2); e=[5;13]; And these!
c(2,:)=[3 6]; replaces second row of c
Advanced Indexing 2
MATLAB contains functions to help you find desired values within a vector or
matrix
Try this:
To get the minimum value and its index >> vec = [1 5 3 9 7]
use min() >> [minVal, minInd] = min(vec)
>> [maxVal, maxInd] = max(vec)
To get the maximum value and its index:
use max()
To find any the indices of specific values or ranges
use find()
Try this
>> ind = find(vec == 9);
>> ind = find(vec > 2 & vec < 6);
>> x = linspace(0,2*pi,1000);
>> y=sin(x);
>> y(55)
>> y(100:110)
>> [minVal,minInd]=min(y)
>> [maxVal,maxInd]=max(y)
>> inds=find(y>-0.001 & y<0.001)
Outline
(1) Getting Started
(2) Making Variables
(3) Manipulating Variables
(4) Basic Plotting
Plotting Vectors
Example
x=linspace(0,4*pi,10);
y=sin(x);
1 1
10 x values: 0.8
0.6
1000 x values:
0.8
0.6
0.4 0.4
0.2 0.2
0 0
-0.2 -0.2
-0.4 -0.4
-0.6 -0.6
-0.8 -0.8
-1 -1
0 2 4 6 8 10 12 14 0 2 4 6 8 10 12 14
Plot Options
Can change the line color, marker style, and line style by
adding a string argument
plot(x,y,’k.-’); Try this
>> x = linspace(0,4*pi,1000)
>> plot(x,sin(x),’b*-’);
color line-style
marker
Can plot without connecting the dots by omitting line
style argument
plot(x,y,’.’)
>> x=0:0.01:10;
>> plot(x,exp(x).*cos(x),’r’);