MATLAB Practice 1 Introducing 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
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.,
What is MATLAB? MATLAB Practice 1 - Introducing MATLAB © 2012, SNU CSE Biointelligence Lab.,
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!!! © 2012, SNU CSE Biointelligence Lab.,
Environment Base window © 2012, SNU CSE Biointelligence Lab., Browser Workspace Command Window Command History Detail (Preview) Start Button
Environment Figure window © 2012, SNU CSE Biointelligence Lab., Plot Tool logo.m
Environment Text Editor Window © 2012, SNU CSE Biointelligence Lab.,
Basics MATLAB Practice 1 - Introducing MATLAB © 2012, SNU CSE Biointelligence Lab.,
Basics Variables Basic classes(type) Integer( int8, int16, int32, int64, uint8, uint16, uint32, uint64 ) Real Single Doublex = 10 Complexx = i 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;’? © 2012, SNU CSE Biointelligence Lab., variable = expression (Double is default type)
Basics Assign matrix 1 Dim 2 Dim 3 Dim © 2012, SNU CSE Biointelligence Lab., 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) = [ ; ; ];
Basics Assign matrix DIY - Create Dim 4 and Dim 5 matrix. © 2012, SNU CSE Biointelligence Lab., x = [1 2 3; 4 5 6; 7 8 9];- 2D x( :, :, 2) = x( :, : );- 3D x( :, :, :, 2) = x( :, :, :);- 4D x( :, :, :, :, 2) = x( :, :, :, : );- 5D
Basics © 2012, SNU CSE Biointelligence Lab.,
Basics Usage of ‘:’ in subscript DIY >> A = [ 1 2 3; 4 5 6; ]; >> A( :, 2 ); >> A( 1, : ); >> B = A; >> B( :, :, 2 ) = 2*A; >> B( 1, 1, : ); >> B( :, :, 3) = 3*A; © 2012, SNU CSE Biointelligence Lab.,
Basics Usage of ‘:’ in subscript DIY © 2012, SNU CSE Biointelligence Lab., B = Try other cases!
Basics Dimension and index © 2012, SNU CSE Biointelligence Lab., 2D 3D
Basics Basic operators © 2012, SNU CSE Biointelligence Lab., OperationAlgebraic FormMATLAB form Additiona + b Subtractiona – b Multiplicationa * b Divisiona / b Exponentiationa ^ b
Basics Basic operators © 2012, SNU CSE Biointelligence Lab., OperationMATLAB formComments Matrix AdditionA + BA and B must be same dimension. Matrix SubtractionA – BA and B must be same dimension. Matrix MultiplicationA * Bcols of A and rows of B must be same. Matrix Right DivisionA / B? Matrix Left DivisionA \ B?
Basics Basic operators DIY >> A = [ ]; >> B = [ ]; >> A / B ans = ? © 2012, SNU CSE Biointelligence Lab., 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’.
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 = [ ]; >> B = [ ]; >> A.* B% in this case, A and B must be same % shape © 2012, SNU CSE Biointelligence Lab.,
Basics dot operator A.* B Element-by-element multiplication of a and b. Both arrays 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.,
Basics colon operator DIY A = 1:10; B = 1:2:10; C = 5:0.5:7; D = 0:-1:5; A = [ ] B = [ ] C = [ ] D = [ ] © 2012, SNU CSE Biointelligence Lab.,
Basics Transpose operator DIY >> A = [ ]; >> B = A’ >> C = A’’ © 2012, SNU CSE Biointelligence Lab.,
Basics Relational & Logical Operators © 2012, SNU CSE Biointelligence Lab., OperatorOperationOperatorOperation ==Equal to&AND ~=Not Equal to|OR >Greater than~NOT >=Greater than or equal to <Less than <=Less than or equal to
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.,
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.,
Basics *.m file Using command windows is inefficient when process long code. So, MATLAB supports a script editor © 2012, SNU CSE Biointelligence Lab., Click or CTRL+N
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. © 2012, SNU CSE Biointelligence Lab.,
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.,
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.,
Basics Loop and Branch statements can be nested Example for 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 end © 2012, SNU CSE Biointelligence Lab.,
Practice I MATLAB Practice 1 - Introducing MATLAB © 2012, SNU CSE Biointelligence Lab.,
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.,
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()) © 2012, SNU CSE Biointelligence Lab.,