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

Post on 21-Jan-2016

226 views 0 download

transcript

A simple classification problem

Extract attributes

Pattern

Pattern recognition

decision

x

C1

C2

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

MATLAB fundamentals

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

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)

Variables (cont.)Variables (cont.)

• variable_name = expression;– addition a + b a +

b– subtraction a - b a - b– multiplication a x b a * b– division a / b a /

b– exponent ab a ^ b

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

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)

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)

Matlab Functions

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

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

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.

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.

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)

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

Flow Control Constructs

• Logic Control:– IF / ELSEIF / ELSE

• Iterative Loops:–FOR–WHILE

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 trueelseif logical expression 2% some commands executed if logical_expression_1 is

false% but logical_expression_2 is trueelse% executed in all other casesend

Example:if no1>no2disp([num2str(no1) '>' num2str(no2)])elseif no1<no2disp([num2str(no1) '<' num2str(no2)])elsedisp([num2str(no1) '=' num2str(no2)])end

The if, elseif and else statements

• 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

The while loop

i=0;while i<10disp(i);i=i+1;end

i=0;while i<10disp(i);i=i+1;end

• The syntax of the while loop is the following:

while logical_expression % some commands end

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)

Plot function

>> a=[ 3 4 5;3 6 7;6 7 8]

a =

3 4 5 3 6 7 6 7 8

>> plot(a)

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')

Multiple Graphs

t = 0:pi/100:2*pi;

y1=sin(t);

y2=sin(t+pi/2);

plot(t,y1,t,y2)

grid on

Notes

• The up-arrow key " will display previous commands.

• clc used to clears the command window

Yahoo group

• http://groups.yahoo.com/group/Pattern_Recognition_MUST

• Pattern_Recognition_MUST@yahoogroups.com