Presentation is loading. Please wait.

Presentation is loading. Please wait.

MATLAB Practice 1 Introducing MATLAB Lecture Notes on Video Search & Mining, Spring 2012 Presented by Jun Hee Yoo Biointelligence Laboratory School of.

Similar presentations


Presentation on theme: "MATLAB Practice 1 Introducing MATLAB Lecture Notes on Video Search & Mining, Spring 2012 Presented by Jun Hee Yoo Biointelligence Laboratory School of."— Presentation transcript:

1 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 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.kr2

3 What is MATLAB? MATLAB Practice 1 - Introducing MATLAB © 2012, SNU CSE Biointelligence Lab., http://bi.snu.ac.kr3

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!!! © 2012, SNU CSE Biointelligence Lab., http://bi.snu.ac.kr4 www.mathworks.com

5 Environment Base window © 2012, SNU CSE Biointelligence Lab., http://bi.snu.ac.kr5 Browser Workspace Command Window Command History Detail (Preview) Start Button

6 Environment Figure window © 2012, SNU CSE Biointelligence Lab., http://bi.snu.ac.kr6 Plot Tool logo.m

7 Environment Text Editor Window © 2012, SNU CSE Biointelligence Lab., http://bi.snu.ac.kr7

8 Basics MATLAB Practice 1 - Introducing MATLAB © 2012, SNU CSE Biointelligence Lab., http://bi.snu.ac.kr8

9 Basics Variables Basic classes(type) Integer( int8, int16, int32, int64, uint8, uint16, uint32, uint64 ) Real Single Doublex = 10 Complexx = 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;’? © 2012, SNU CSE Biointelligence Lab., http://bi.snu.ac.kr9 variable = expression (Double is default type)

10 Basics Assign matrix 1 Dim 2 Dim 3 Dim © 2012, SNU CSE Biointelligence Lab., http://bi.snu.ac.kr10 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];

11 Basics Assign matrix DIY - Create Dim 4 and Dim 5 matrix. © 2012, SNU CSE Biointelligence Lab., http://bi.snu.ac.kr11 x = [1 2 3; 4 5 6; 7 8 9];- 2D x( :, :, 2) = x( :, : );- 3D x( :, :, :, 2) = x( :, :, :);- 4D x( :, :, :, :, 2) = x( :, :, :, : );- 5D

12 Basics © 2012, SNU CSE Biointelligence Lab., http://bi.snu.ac.kr12

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.kr13

14 369 121518 212427 Basics Usage of ‘:’ in subscript DIY © 2012, SNU CSE Biointelligence Lab., http://bi.snu.ac.kr14 246 81012 141618 123 456 789 B = Try other cases!

15 Basics Dimension and index © 2012, SNU CSE Biointelligence Lab., http://bi.snu.ac.kr15 2D 3D

16 Basics Basic operators © 2012, SNU CSE Biointelligence Lab., http://bi.snu.ac.kr16 OperationAlgebraic FormMATLAB form Additiona + b Subtractiona – b Multiplicationa * b Divisiona / b Exponentiationa ^ b

17 Basics Basic operators © 2012, SNU CSE Biointelligence Lab., http://bi.snu.ac.kr17 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?

18 Basics Basic operators DIY >> A = [ 1 2 3 ]; >> B = [ 2 3 4 ]; >> A / B ans = ? © 2012, SNU CSE Biointelligence Lab., http://bi.snu.ac.kr18 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’.

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.kr19

20 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., http://bi.snu.ac.kr20

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.kr21

22 Basics Transpose operator DIY >> A = [ 1 2 3 ]; >> B = A’ >> C = A’’ © 2012, SNU CSE Biointelligence Lab., http://bi.snu.ac.kr22

23 Basics Relational & Logical Operators © 2012, SNU CSE Biointelligence Lab., http://bi.snu.ac.kr23 OperatorOperationOperatorOperation ==Equal to&AND ~=Not Equal to|OR >Greater than~NOT >=Greater than or equal to <Less than <=Less than or equal to

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.kr24

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.kr25

26 Basics *.m file Using command windows is inefficient when process long code. So, MATLAB supports a script editor © 2012, SNU CSE Biointelligence Lab., http://bi.snu.ac.kr26 Click or CTRL+N

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. © 2012, SNU CSE Biointelligence Lab., http://bi.snu.ac.kr27

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.kr28

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.kr29

30 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., http://bi.snu.ac.kr30

31 Practice I MATLAB Practice 1 - Introducing MATLAB © 2012, SNU CSE Biointelligence Lab., http://bi.snu.ac.kr31

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.kr32

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()) © 2012, SNU CSE Biointelligence Lab., http://bi.snu.ac.kr33


Download ppt "MATLAB Practice 1 Introducing MATLAB Lecture Notes on Video Search & Mining, Spring 2012 Presented by Jun Hee Yoo Biointelligence Laboratory School of."

Similar presentations


Ads by Google