MATLAB Practice 1Introducing MATLAB
Lecture Notes on Video Search & Mining, Spring 2012
Presented by Jun Hee Yoo
Biointelligence Laboratory
School of Computer Science and Engineering
Seoul National Univertisy
http://bi.snu.ac.kr
© 2012, SNU CSE Biointelligence Lab., http://bi.snu.ac.kr 2
Contents
What is the MATLAB? Pros & cons. Environment
Basics Variables Operators Editor Loop, Branch Practice I
To be Beginner (maybe next time) Functions Error Handling Useful Tips
© 2012, SNU CSE Biointelligence Lab., http://bi.snu.ac.kr 3
What is MATLAB?
MATLAB Practice 1 - Introducing MATLAB
© 2012, SNU CSE Biointelligence Lab., http://bi.snu.ac.kr 4
What is MATLAB?
Short for MATrix LABoratory Pros
Ease to Use A lots of built-in useful functions. Script language
Platform Independence Can use with C/C++ and JAVA
Cons Slower than native programs It’s commercial software
GNU Octave is free!!!
www.mathworks.com
© 2012, SNU CSE Biointelligence Lab., http://bi.snu.ac.kr 5
Environment
Base window
Browser
Workspace
CommandWindow
CommandHistory
Detail(Preview)
StartButton
© 2012, SNU CSE Biointelligence Lab., http://bi.snu.ac.kr 6
Environment
Figure window Plot Tool
logo.m
© 2012, SNU CSE Biointelligence Lab., http://bi.snu.ac.kr 7
Environment
Text Editor Window
© 2012, SNU CSE Biointelligence Lab., http://bi.snu.ac.kr 8
Basics
MATLAB Practice 1 - Introducing MATLAB
© 2012, SNU CSE Biointelligence Lab., http://bi.snu.ac.kr 9
Basics
Variables
Basic classes(type) Integer(int8, int16, int32, int64, uint8, uint16, uint32, uint64) Real
Single Double x = 10 Complex x = 10 + 9i
Character x = ‘a’ ( to assign string, ‘abc’ or [‘a’ ‘b’ ‘c’] )
And Etc. (cell, struct, object, boolean) What is different between ‘x = 10’ and ‘x = 10;’?
variable = expression(Double is default type)
© 2012, SNU CSE Biointelligence Lab., http://bi.snu.ac.kr 10
Basics
Assign matrix 1 Dim
2 Dim
3 Dim
x1 = [1 2 3];x2 = [1, 2, 3];x3 = [1;2;3];
x = [[1 2 3]; [4 5 6]; [7 8 9]]; or x = [1 2 3; 4 5 6; 7 8 9];
x = [[1 2 3]; or x = [ 1 2 3; [4 5 6]; 4 5 6; [7 8 9]]; 7 8 9];
x = [[[1 2 3]; [4 5 6]; [7 8 9]];]; ??
x = [1 2 3; 4 5 6; 7 8 9];x( :, :, 2) = [10 11 12; 13 14 15; 16 17 18];
© 2012, SNU CSE Biointelligence Lab., http://bi.snu.ac.kr 11
Basics
Assign matrix DIY
- Create Dim 4 and Dim 5 matrix.
x = [1 2 3; 4 5 6; 7 8 9]; - 2Dx( :, :, 2) = x( :, : ); - 3Dx( :, :, :, 2) = x( :, :, :); - 4Dx( :, :, :, :, 2) = x( :, :, :, : ); - 5D
© 2012, SNU CSE Biointelligence Lab., http://bi.snu.ac.kr 12
Basics
Assign Matrix Elements A = [ 1 2 3; 4 5 6 ]; How to change matrix A to ?
Using subscript To Access , A(1, 3);
© 2012, SNU CSE Biointelligence Lab., http://bi.snu.ac.kr 13
Basics
Usage of ‘:’ in subscript DIY
>> A = [ 1 2 3; 4 5 6; 7 8 9 ]; >> A( :, 2 ); >> A( 1, : ); >> B = A; >> B( :, :, 2 ) = 2*A; >> B( 1, 1, : ); >> B( :, :, 3) = 3*A;
© 2012, SNU CSE Biointelligence Lab., http://bi.snu.ac.kr 14
3 6 9
12 15 18
21 24 27
Basics
Usage of ‘:’ in subscript DIY
2 4 6
8 10 12
14 16 18
1 2 3
4 5 6
7 8 9
𝐵 (1 ,: ,: )=[1 2 32 4 63 6 9]
𝐵 (2 ,: ,: )=[ 4 5 68 10 1212 15 18]
B =
Try other cases!
© 2012, SNU CSE Biointelligence Lab., http://bi.snu.ac.kr 15
Basics
Dimension and index
2D
3D
© 2012, SNU CSE Biointelligence Lab., http://bi.snu.ac.kr 16
Basics
Basic operators
Operation Algebraic Form MATLAB form
Addition a + b
Subtraction a – b
Multiplication a * b
Division a / b
Exponentiation a ^ b
< Arithmetic Operations between two scalars >
© 2012, SNU CSE Biointelligence Lab., http://bi.snu.ac.kr 17
Basics
Basic operators
Operation MATLAB form Comments
Matrix Addition A + B A and B must be same dimension.
Matrix Subtraction A – B A and B must be same dimension.
Matrix Multiplication A * B cols of A and rows of B must be same.
Matrix Right Division A / B ?
Matrix Left Division A \ B ?
< Common Matrix Operations >
© 2012, SNU CSE Biointelligence Lab., http://bi.snu.ac.kr 18
Basics
Basic operators DIY
>> A = [ 1 2 3 ]; >> B = [ 2 3 4 ]; >> A / B ans = ?
DIY >> A = magic(3); >> B = [ 1; 2; 3 ]; >> x = A \ B >> A*x ans = ?
In MATLAB, if matrix is not square shape or doesn’t have inverse matrix,it uses ‘pseudo inverse matrix’.
© 2012, SNU CSE Biointelligence Lab., http://bi.snu.ac.kr 19
Basics
dot operator DIY
>> A = pascal(3); >> B = A ^ 2; % this is same as B = A * A; >> C = A .^ 2;
B and C are same?
DIY >> A = [ 1 2 3 ]; >> B = [ 2 4 8 ]; >> A .* B % in this case, A and B must be same
% shape
© 2012, SNU CSE Biointelligence Lab., http://bi.snu.ac.kr 20
Basics
dot operator A .* B
Element-by-element multiplication of a and b. Both ar-rays must be the same shape, or one of them must be a scalar
A ./ B Element-by-element right division of a and b : a(i,j) /
b(i,j). Both arrays must be the same shape, or one of them must be a scalar.
A .\ B Same constraints as A ./ B.
© 2012, SNU CSE Biointelligence Lab., http://bi.snu.ac.kr 21
Basics
colon operator DIY
A = 1:10; B = 1:2:10; C = 5:0.5:7; D = 0:-1:5;
A = [ 1 2 3 4 5 6 7 8 9 ] B = [ 1 3 5 7 9 ] C = [ 5.0000 5.5000 6.0000 6.5000 7.0000 ] D = [ 0 -1 -2 -3 -4 -5 ]
© 2012, SNU CSE Biointelligence Lab., http://bi.snu.ac.kr 22
Basics
Transpose operator DIY
>> A = [ 1 2 3 ]; >> B = A’ >> C = A’’
© 2012, SNU CSE Biointelligence Lab., http://bi.snu.ac.kr 23
Basics
Relational & Logical Operators
Operator Operation Operator Operation
== Equal to & AND
~= Not Equal to | OR
> Greater than ~ NOT
>= Greater than or equal to
< Less than
<= Less than or equal to
© 2012, SNU CSE Biointelligence Lab., http://bi.snu.ac.kr 24
Basics
Relational & Logical Operators DIY
>> x = [ 0 : 3 : 15 ]; >> y = [ 0 : 2 : 10 ]; >> x > y >> x == y >> x < y >> x ~= y >> u = randint(1, 6); >> v = randint(1, 6); >> u & v >> u | v >> ~u
© 2012, SNU CSE Biointelligence Lab., http://bi.snu.ac.kr 25
Basics
Tips clear variable_name
remove variable from workspace ‘clear all’ will remove all variables in workspace
clc clear command window
© 2012, SNU CSE Biointelligence Lab., http://bi.snu.ac.kr 26
Basics
*.m file Using command windows is inefficient when process
long code. So, MATLAB supports a script editor
Click or CTRL+N
© 2012, SNU CSE Biointelligence Lab., http://bi.snu.ac.kr 27
Basics
Editor
1. new document 2. execute current m-file
Do not use any white space character in file name. Please, write your file name in English.
Also, the path should be written in English.
1 2
© 2012, SNU CSE Biointelligence Lab., http://bi.snu.ac.kr 28
Basics
Loop ‘for’ statement
for iteration = initial_value : step : final_value
end
DIY ( write in the script editor )
for i = 1:10
disp(i);
end
*while is loop statements too.
© 2012, SNU CSE Biointelligence Lab., http://bi.snu.ac.kr 29
Basics
Branch ‘if’ statement
if relation or logical expression
statements;
elseif relation or logical expression % optional
statements;
else % optional
statements;
end
* keyword ‘swicth’ is branch statement too.
© 2012, SNU CSE Biointelligence Lab., http://bi.snu.ac.kr 30
Basics Loop and Branch statements can be nested
Examplefor i = 1:2:10 if i <= 5 if i == 1 disp(i); end elseif i > 5 && i < 10 for j = i:10 disp(j); end else endend
© 2012, SNU CSE Biointelligence Lab., http://bi.snu.ac.kr 31
Practice I
MATLAB Practice 1 - Introducing MATLAB
© 2012, SNU CSE Biointelligence Lab., http://bi.snu.ac.kr 32
Practice I
Function call MATLAB has many powerful Built-in functions. DIY
>> A = rand(1); >> B = ceil(A);
To see the explanation of functions >> help function_name
DIY >> help rand
© 2012, SNU CSE Biointelligence Lab., http://bi.snu.ac.kr 33
Practice I
Automated Lottery Program Generate 6 number between 1~45 for n times (n = 100) Generated numbers must not contain same number.
You can use… ceil() rand() sum() Google!!
You can not use… Any other random functions. (such as randperm())