Presentation is loading. Please wait.

Presentation is loading. Please wait.

A simple classification problem Extract attributes Pattern Pattern recognition decision x C1 C2.

Similar presentations


Presentation on theme: "A simple classification problem Extract attributes Pattern Pattern recognition decision x C1 C2."— Presentation transcript:

1

2 A simple classification problem Extract attributes Pattern Pattern recognition decision x C1 C2

3

4

5

6

7

8

9 Clearing theWorkspace Matlab keeps all the variables you declare in memory until you tell it to erase them. InMatlab lingo, the place where user-defined variables are stored is referred to as the workspace. To erase a single variable from the workspace, type clear and then the variable name. clear x If you want to erase all variables in the workspace, simply type clear

10 MATLAB fundamentals

11 MATLAB as a Calculator You can use Matlab as a calculator by typing commands at >>, like these. Try them out. 1+1 2*3 5/6 Sin(5) to enter numbers like 1.23e15

12

13

14

15

16 Variables Variables are not declared before they are used. The assignment command in Matlab is simply the equal sign. Note that variable names in Matlab are case sensitive. All Variables are arrays(A collection of data values organized into rows and columns)

17 Variables (cont.) variable_name = expression; –addition a + b  a + b –subtractiona - b  a - b –multiplicationa x b  a * b –divisiona / b  a / b –exponenta b  a ^ b

18

19 Changing the data format >> value = 12.345678901234567; – format short  12.3457 (default format) – format long  12.34567890123457 – format short e  1.2346e+001 – format long e  1.234567890123457e+001

20 String Variables String variables contain a sequence of characters, like this s='This is a string' If you need an apostrophe in your string, repeat a single quote, like this: t='Don''t worry‘ And if you justwant to access part of a string, like the first 7 characters of s (defined above) use s(1:7)

21

22

23

24

25

26

27

28

29 Matlab Functions Matlab knows all of the standard functions found on scientific calculators and even many of the special functions. – cos(x) – sin(x) – tan(x) – acos(x) – asin(x) – atan(x) – atan2(y,x)

30 Matlab Functions The disp() function – >> disp( 'Hello' ) – Hello – >> disp(5) – 5 – >> disp( [ 'Bilkent ' 'University' ] ) – Bilkent University – >> name = 'Alper'; – >> disp( [ 'Hello ' name ] ) – Hello Alper

31 Matlab Functions The num2str() and int2str() functions – >> d = [ num2str(16) '-Feb-' num2str(2004) ]; – >> disp(d) – 16-Feb-2004 – >> x = 23.11; – >> disp( [ 'answer = ' num2str(x) ] ) – answer = 23.11 – >> disp( [ 'answer = ' int2str(x) ] ) – answer = 23

32 Scripts Typing in commands in the command window is just the tip of the iceberg of what Matlab can do for you. Most of the work you will do in Matlab will be stored in files called scripts, or m-files, containing sequences of Matlab commands to be executed over and over again.

33 Scripts To make a script, create a new text file in Matlab by clicking on the empty document on the tool bar. Then save this file with a.m extensions (automatically added in Windows) in the directory where you want to store your Matlab scripts. Script file names cannot start with numbers, like 430lab1a.m You execute scripts by typing their name, and when Matlab receives the start of a number in a command it thinks a calculation is coming. Since something like 430lab1a is not a valid calculation, Matlab will give you an error if you try and name your scripts like this.

34 Running a Script Before you can execute a script, you need to point Matlab’s current folder to the place where your script is saved. Remember to save changes to your script before executing it (Ctrl-s is a quick way) You should nearly always begin your scripts with the following two commands: clear; close all; The clear command clears all variables fromMatlab’s memory and makes sure that you don’t have leftover junk active in Matlab that will interfere with your code. The close all command closes any figure windows that are open. Any line in a script that ends with a semicolon will execute without printing to the screen. For example, add these two lines of code to your test script – a=sin(5); – b=cos(5)

35 Input and Output To have a script request and assign a value to the variable N from the keyboard, put the command N=input(' Enter a value for N - ') To display printed results you can use the fprintf command fprintf(' N =%g ',N) Note that the stuff inside the single quotes is a string which will print on the screen; % is where the number you are printing goes; and the stuff immediately after % is a format code. A g means use the “best” format; if the number is really big or really small

36 Flow Control Constructs Logic Control: – IF / ELSEIF / ELSE Iterative Loops: – FOR – WHILE

37 The if, elseif and else statements The general syntax for the if structure is:  if logical expression 1 % some commands executed if logical_expression_1 is true elseif logical expression 2 % some commands executed if logical_expression_1 is false % but logical_expression_2 is true else % executed in all other cases end

38 Example: if no1>no2 disp([num2str(no1) '>' num2str(no2)]) elseif no1<no2 disp([num2str(no1) '<' num2str(no2)]) else disp([num2str(no1) '=' num2str(no2)]) end The if, elseif and else statements

39 The for loop syntax is useful to repeat execution of a certain block of code The for loop for variable=startvalue :stepsize : endvalue % some commands end Example: for i=1:1:5 disp(i) end for variable=startvalue :stepsize : endvalue % some commands end Example: for i=1:1:5 disp(i) end

40 The while loop i=0; while i<10 disp(i); i=i+1; end i=0; while i<10 disp(i); i=i+1; end The syntax of the while loop is the following: while logical_expression % some commands end

41 Creating MATLAB functions By creating a function m-file, you can add functions to MATLAB. The name ‘m-file’ is derived from the extension of such files, which must be.m. Using m-files, you can collect your own commands and execute them with a single command (which is the m-file name). Using input and output variables, the m-file can also communicate with MATLAB and other functions. An m-file has the following form  function [output1,output2,...]=myfunction(input1,input2,...)  Example: function [sum,avg]=sumavg(no1,no2) sum=no1+no2; avg=sum/2;  To access this function: [x,y]=sumavg(3,5)

42 Plot function >> a=[ 3 4 5;3 6 7;6 7 8] a = 3 4 5 3 6 7 6 7 8 >> plot(a)

43 Matlab Graphs x = 0:pi/100:2*pi; y = sin(x); plot(x,y) xlabel('x = 0:2\pi') ylabel('Sine of x') title('Plot of the Sine Function')

44 Multiple Graphs t = 0:pi/100:2*pi; y1=sin(t); y2=sin(t+pi/2); plot(t,y1,t,y2) grid on

45 Notes The up-arrow key " will display previous commands. clc used to clears the command window

46 Yahoo group http://groups.yahoo.com/group/Pattern_Reco gnition_MUST http://groups.yahoo.com/group/Pattern_Reco gnition_MUST Pattern_Recognition_MUST@yahoogroups.co m

47


Download ppt "A simple classification problem Extract attributes Pattern Pattern recognition decision x C1 C2."

Similar presentations


Ads by Google