22
TU-CS474 : Pattern Recognition. Prof. Mostafa Gadal-Haqq slide - 1 Lab1: Overview of Matlab System Lab materials : Matlab’s Getting Started pdf file. Lecturer: Prof. Dr. Mostafa Gadal-Haqq CS474 : Pattern Recognition

Labmaterials : Matlab’s Getting Started .noufcs.weebly.com/.../cs474_lab1_intromatlab.pdf · TU-CS474 : Pattern Recognition.Prof. Mostafa Gadal-Haqq slide - 4 Matlab IDE • Command

  • Upload
    others

  • View
    4

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Labmaterials : Matlab’s Getting Started .noufcs.weebly.com/.../cs474_lab1_intromatlab.pdf · TU-CS474 : Pattern Recognition.Prof. Mostafa Gadal-Haqq slide - 4 Matlab IDE • Command

TU-CS474 : Pattern Recognition. Prof. Mostafa Gadal-Haqq slide - 1

Lab1: Overview of Matlab System

Lab materials : Matlab’s Getting Started pdf file.

Lecturer: Prof. Dr. Mostafa Gadal-Haqq

CS474 : Pattern Recognition

Page 2: Labmaterials : Matlab’s Getting Started .noufcs.weebly.com/.../cs474_lab1_intromatlab.pdf · TU-CS474 : Pattern Recognition.Prof. Mostafa Gadal-Haqq slide - 4 Matlab IDE • Command

TU-CS474 : Pattern Recognition. Prof. Mostafa Gadal-Haqq slide - 2

The MATLAB System1. Development Environment (IDE): A set of tools and facilities

that help you use MATLAB functions and files.

2. The MATLAB Mathematical Function Library: A vast

collection of computational algorithms ranging from elementary functions

like sum, …, to more sophisticated functions like matrix inverse, ….

3. The MATLAB Language: This is a high-level matrix/array

language with control flow statements, functions, data structures,

input/output, and object-oriented programming features.

4. Handle Graphics. This is the MATLAB graphics system. It includes

high-level commands for two-dimensional and three-dimensional data

visualization, image processing, animation, and presentation graphics..

5. The MATLAB Application Program Interface (API). This

is a library that allows you to write C and Fortran programs that interact

with MATLAB.

Page 3: Labmaterials : Matlab’s Getting Started .noufcs.weebly.com/.../cs474_lab1_intromatlab.pdf · TU-CS474 : Pattern Recognition.Prof. Mostafa Gadal-Haqq slide - 4 Matlab IDE • Command

TU-CS474 : Pattern Recognition. Prof. Mostafa Gadal-Haqq slide - 3

Matlab IDE

Command

History

Workspace

Browser

Current

Directory

Command

window

Start

Button

Page 4: Labmaterials : Matlab’s Getting Started .noufcs.weebly.com/.../cs474_lab1_intromatlab.pdf · TU-CS474 : Pattern Recognition.Prof. Mostafa Gadal-Haqq slide - 4 Matlab IDE • Command

TU-CS474 : Pattern Recognition. Prof. Mostafa Gadal-Haqq slide - 4

Matlab IDE

• Command Window:

– From which one can enter variables and run functions and M-files.

• Command History

– In which Lines entered in the Command Window are logged.

• Current Directory Browser

– Any file you want to run must either be in the current directory or

on the search path.

• Workspace Browser

– Displays the set of variables (named arrays) built up during a

MATLAB session and stored in memory.

• Start Button

– provides easy access to tools, demos, and documentation.

Page 5: Labmaterials : Matlab’s Getting Started .noufcs.weebly.com/.../cs474_lab1_intromatlab.pdf · TU-CS474 : Pattern Recognition.Prof. Mostafa Gadal-Haqq slide - 4 Matlab IDE • Command

TU-CS474 : Pattern Recognition. Prof. Mostafa Gadal-Haqq slide - 5

Matlab IDE

• Editor/Debugger

– Is used to create and debug M-files, which are programs you write to run MATLAB functions.

Page 6: Labmaterials : Matlab’s Getting Started .noufcs.weebly.com/.../cs474_lab1_intromatlab.pdf · TU-CS474 : Pattern Recognition.Prof. Mostafa Gadal-Haqq slide - 4 Matlab IDE • Command

TU-CS474 : Pattern Recognition. Prof. Mostafa Gadal-Haqq slide - 6

Manipulating Matrices

• Any variable in Matlab is considered as a

matrix.

• Scalar:

– A scalar is a 1x1 matrix.

• Just a number: A = 5;

• Vector:

– Column vector: a = [1; 2; 3; 4].

– Row vector: b = [1 2 3 4].

– Transpose: a = b’;

Page 7: Labmaterials : Matlab’s Getting Started .noufcs.weebly.com/.../cs474_lab1_intromatlab.pdf · TU-CS474 : Pattern Recognition.Prof. Mostafa Gadal-Haqq slide - 4 Matlab IDE • Command

TU-CS474 : Pattern Recognition. Prof. Mostafa Gadal-Haqq slide - 7

Manipulating Matrices

• Matrix:

– A = [16 3 2 13; 5 10 11 8; 9 6 7 12; 4 15 14 1]

– Matlab displays:

• Access a single element.

– A( row index, colum index ),

– Entering >> A(1,3) returns 2;

• sum, transpose, and diag.

– sum(A) = 34 34 34 34

– Transpose of A: A’ produce

– diag(A)

Page 8: Labmaterials : Matlab’s Getting Started .noufcs.weebly.com/.../cs474_lab1_intromatlab.pdf · TU-CS474 : Pattern Recognition.Prof. Mostafa Gadal-Haqq slide - 4 Matlab IDE • Command

TU-CS474 : Pattern Recognition. Prof. Mostafa Gadal-Haqq slide - 8

Manipulating Matrices

– diag(A)

– sum(diag(A))

• ans = 34

• Subscripts:

– The element in row i and column j of A is denoted by A(i,j).

– A(1,4) + A(2,4) + A(3,4) + A(4,4)

• ans = 34

• Error on bounds:

– t = A(4,5)

– Returns: Index exceeds matrix dimensions.

Page 9: Labmaterials : Matlab’s Getting Started .noufcs.weebly.com/.../cs474_lab1_intromatlab.pdf · TU-CS474 : Pattern Recognition.Prof. Mostafa Gadal-Haqq slide - 4 Matlab IDE • Command

TU-CS474 : Pattern Recognition. Prof. Mostafa Gadal-Haqq slide - 9

Manipulating Matrices

• You get error when you use an

element outside the matrix:

– t = A(4,5)

– Returns: Index exceeds matrix dimensions.

• On the other hand, if you store a value in an

element outside of the matrix, the size increases to

accommodate the newcomer.– X = A;

– X(4,5) = 17

Page 10: Labmaterials : Matlab’s Getting Started .noufcs.weebly.com/.../cs474_lab1_intromatlab.pdf · TU-CS474 : Pattern Recognition.Prof. Mostafa Gadal-Haqq slide - 4 Matlab IDE • Command

TU-CS474 : Pattern Recognition. Prof. Mostafa Gadal-Haqq slide - 10

Manipulating Matrices

• The Colon Operator

– 1:10

– is a row vector containing the integers from 1 to 10

– 1 2 3 4 5 6 7 8 9 10

• Subscript expressions involving colons refer to portions of a matrix.

– A(1:k,j)

– is the first k elements of the jth column of A.

Page 11: Labmaterials : Matlab’s Getting Started .noufcs.weebly.com/.../cs474_lab1_intromatlab.pdf · TU-CS474 : Pattern Recognition.Prof. Mostafa Gadal-Haqq slide - 4 Matlab IDE • Command

TU-CS474 : Pattern Recognition. Prof. Mostafa Gadal-Haqq slide - 11

Manipulating Expressions

• The building blocks of expressions are:

– Variables

– Numbers

– Operators

– Functions

Page 12: Labmaterials : Matlab’s Getting Started .noufcs.weebly.com/.../cs474_lab1_intromatlab.pdf · TU-CS474 : Pattern Recognition.Prof. Mostafa Gadal-Haqq slide - 4 Matlab IDE • Command

TU-CS474 : Pattern Recognition. Prof. Mostafa Gadal-Haqq slide - 12

Manipulating Expressions

• Variables:

– MATLAB does not require any type

declarations or dimension statements.

– num = 25

– creates a 1-by-1 matrix named num and stores the value 25 in its single element.

– MATLAB is case sensitive;

Page 13: Labmaterials : Matlab’s Getting Started .noufcs.weebly.com/.../cs474_lab1_intromatlab.pdf · TU-CS474 : Pattern Recognition.Prof. Mostafa Gadal-Haqq slide - 4 Matlab IDE • Command

TU-CS474 : Pattern Recognition. Prof. Mostafa Gadal-Haqq slide - 13

Manipulating Expressions

• Numbers :

– MATLAB uses conventional decimal notation, with an optional decimal point and leading plus or minus sign, for numbers. Scientific notation uses the letter e to specify a power-of-ten scale factor.

– Imaginary numbers use either i or j as a suffix.

– Some examples of legal numbers are:

Page 14: Labmaterials : Matlab’s Getting Started .noufcs.weebly.com/.../cs474_lab1_intromatlab.pdf · TU-CS474 : Pattern Recognition.Prof. Mostafa Gadal-Haqq slide - 4 Matlab IDE • Command

TU-CS474 : Pattern Recognition. Prof. Mostafa Gadal-Haqq slide - 14

Manipulating Expressions

• Operators :

Addition+

Subtraction-

Multiplication*

Division/

Left division\

Power^

Complex conjugate transpose'

Specify evaluation order( )

Page 15: Labmaterials : Matlab’s Getting Started .noufcs.weebly.com/.../cs474_lab1_intromatlab.pdf · TU-CS474 : Pattern Recognition.Prof. Mostafa Gadal-Haqq slide - 4 Matlab IDE • Command

TU-CS474 : Pattern Recognition. Prof. Mostafa Gadal-Haqq slide - 15

Matlab standard functions

• Functions:

• MATLAB provides a large number of standard

elementary mathematical functions, including abs,

sqrt, exp, and sin, as well as many more advanced

mathematical functions. Most of these functions

accept complex arguments.

– For a list of the elementary mathematical

functions, type:

• help elfun

Page 16: Labmaterials : Matlab’s Getting Started .noufcs.weebly.com/.../cs474_lab1_intromatlab.pdf · TU-CS474 : Pattern Recognition.Prof. Mostafa Gadal-Haqq slide - 4 Matlab IDE • Command

TU-CS474 : Pattern Recognition. Prof. Mostafa Gadal-Haqq slide - 16

Generating Matrices

Matlab provides many internal and external functions

that generate special matrices, for example:

zeros all zeros

ones all ones

rand uniformly distributed random numbers

randn normally distributed random numbers

For Example: Z = zeros(2,4) produces 2x4 zero matrix:

Page 17: Labmaterials : Matlab’s Getting Started .noufcs.weebly.com/.../cs474_lab1_intromatlab.pdf · TU-CS474 : Pattern Recognition.Prof. Mostafa Gadal-Haqq slide - 4 Matlab IDE • Command

TU-CS474 : Pattern Recognition. Prof. Mostafa Gadal-Haqq slide - 17

Graphics

Creating plot :

The plot function has different forms , depending on input

arguments. If you have two vectors y and x,

the command >>plot (x,y)

produces a graph of y versus x.

: For example

Page 18: Labmaterials : Matlab’s Getting Started .noufcs.weebly.com/.../cs474_lab1_intromatlab.pdf · TU-CS474 : Pattern Recognition.Prof. Mostafa Gadal-Haqq slide - 4 Matlab IDE • Command

TU-CS474 : Pattern Recognition. Prof. Mostafa Gadal-Haqq slide - 18

M-Files

There are two kinds of M-files:

• Scripts, which do not accept input arguments

or return output arguments. They operate on

data in the workspace.

• Functions, which can accept input arguments

and return output arguments. Internal

variables are local to the function.

Page 19: Labmaterials : Matlab’s Getting Started .noufcs.weebly.com/.../cs474_lab1_intromatlab.pdf · TU-CS474 : Pattern Recognition.Prof. Mostafa Gadal-Haqq slide - 4 Matlab IDE • Command

TU-CS474 : Pattern Recognition. Prof. Mostafa Gadal-Haqq slide - 19

M-Files: 1- ScriptsInstead of writing data (or commands) each time you start matlab, you can save them in a file with extension .m (hence the name m-files). Such a file is called a script.

Example 1, create a file containing these five lines:

Store the file under the name magik.m,

then the command >>magic

Reads the file and creates a variable, A , containing the saved matrix.

Page 20: Labmaterials : Matlab’s Getting Started .noufcs.weebly.com/.../cs474_lab1_intromatlab.pdf · TU-CS474 : Pattern Recognition.Prof. Mostafa Gadal-Haqq slide - 4 Matlab IDE • Command

TU-CS474 : Pattern Recognition. Prof. Mostafa Gadal-Haqq slide - 20

M-Files: 1- ScriptsExample 2, create a file containing these three lines:

Store the file under the name sinplot.m,

then the command >>sinplot

Reads the file and plot

the same graph as before.

Page 21: Labmaterials : Matlab’s Getting Started .noufcs.weebly.com/.../cs474_lab1_intromatlab.pdf · TU-CS474 : Pattern Recognition.Prof. Mostafa Gadal-Haqq slide - 4 Matlab IDE • Command

TU-CS474 : Pattern Recognition. Prof. Mostafa Gadal-Haqq slide - 21

M-Files: 2- Functions

• Functions are M-files that can accept input

arguments and return output arguments

• The name of the M-file and of the function should

be the same.

• Functions variables are local, i.e. they are defined

only within the function.

For example: The commands, zeros, ones, rand, …

are external function, to see their content enter:

>> type ‘function Name’

Page 22: Labmaterials : Matlab’s Getting Started .noufcs.weebly.com/.../cs474_lab1_intromatlab.pdf · TU-CS474 : Pattern Recognition.Prof. Mostafa Gadal-Haqq slide - 4 Matlab IDE • Command

TU-CS474 : Pattern Recognition. Prof. Mostafa Gadal-Haqq slide - 22

M-Files: 2- FunctionsExample 1, the following is a function that returns the sum of

a vector:

then enter the commands

>> B = [ 1 2 3 4 5 6 7 8 9 10 ];

>> vectorsum(B)

return the sum of B ( =55 )

function s = VectorSum( a)

n = length(a);

sum =0;

for i = 1: n

sum = sum + a(i);

end

s = sum;

Store the file under the name

vectorsum.m

Important: Functions must be in the path or in the

current directory