Документ взят из кэша поисковой машины. Адрес оригинального документа : http://imaging.cs.msu.ru/files/courses/ipintro2012/Lec_Pt2_MATLAB_Workshop_SHORT.pdf
Дата изменения: Sat Mar 24 00:24:19 2012
Дата индексирования: Mon Oct 1 20:09:15 2012
Кодировка:
Introduction to MATLAB
1

Lecture Series by CEPSTRUM
Presented by Pratik Kotkar & Akash Baid


Topics..
2

What is MATLAB ??
Basic Matrix Operations Script Files and M-files Some more Operations and Functions

APPLICATIONS: Plotting functions .. Image Processing Basics .. Robotics Applications .. GUI Design and Programming


Topics..
3

What is MATLAB ??
Basic Matrix Operations Script Files and M-files Some more Operations and Functions

APPLICATIONS: Plotting functions .. Image Processing Basics .. Robotics Applications .. GUI Design and Programming


MATLAB
4

MATLAB is a program for doing numerical

computation. It was originally designed for solving linear algebra type problems using matrices. It's name is derived from MATrix LABoratory.

MATLAB has since been expanded and now has built-in functions for solving problems requiring data analysis, signal processing, optimization, and several other types of scientific computations. It also contains functions for 2-D and 3-D graphics and animation.


MATLAB
5

Everything in MATLAB is a matrix !


MATLAB
6

The MATLAB environment is command oriented

somewhat like UNIX. A prompt appears on the screen and a MATLAB statement can be entered. When the key is pressed, the statement is executed, and another prompt appears.
If a statement is terminated with a semicolon ( ; ), no

results will be displayed. Otherwise results will appear before the next prompt.


The MATLAB User Interface
7


MATLAB
8

To get started, type one of these commands: helpwin, helpdesk, or demo » a=5; » b=a/2

b=
2.5000

»


MATLAB Variable Names
9

Variable names ARE case sensitive
Variable names can contain up to 63 characters (as of

MATLAB 6.5 and newer)
Variable names must start with a letter followed by letters,

digits, and underscores.


MATLAB Special Variables
10

ans pi eps inf NaN i and j realmin realmax

Default variable name for results Value of Smallest incremental number Infinity Not a number e.g. 0/0 i = j = square root of -1 The smallest usable positive real number The largest usable positive real number


Topics..
11

What is MATLAB ??
Basic Matrix Operations Script Files and M-files Some more Operations and Functions

APPLICATIONS: Plotting functions .. Image Processing Basics .. Robotics Applications .. GUI Design and Programming


Math & Assignment Operators
12

Power Multiplication Division or NOTE:

^ or * or / or \ or 56/8

.^ .* ./ .\ = 8\

a^b a*b a/b b\a 56

or or or or

a.^b a.*b a./b b.\a

- (unary) + Addition Subtraction Assignment

(unary) + =

a+b a-b a=b (assign b to a)


Other MATLAB symbols
13

>> ... , % ; :

prompt continue statement on next line separate statements and data start comment which ends at end of line (1) suppress output (2) used as a row separator in a matrix specify range


MATLAB Relational Operators
14

MATLAB supports six relational operators.

Less Than < Less Than or Equal Greater Than Greater Than or Equal Equal To Not Equal To

<= > >= == ~=


MATLAB Logical Operators
15

MATLAB supports three logical operators.

not and or

~ & |

% highest precedence % equal precedence with or % equal precedence with and


MATLAB Matrices
16

MATLAB treats all variables as matrices. For our purposes

a matrix can be thought of as an array, in fact, that is how it is stored.
Vectors are special forms of matrices and contain only one

row OR one column.
Scalars are matrices with only one row AND one column


MATLAB Matrices
17

A matrix with only one row AND one column is a scalar. A

scalar can be created in MATLAB as follows: » a_value=23 a_value =

23


MATLAB Matrices
18

A matrix with only one row is called a row vector. A row

vector can be created in MATLAB as follows (note the commas):

» rowvec = [12 , 14 , 63]
rowvec = 12 14 63


MATLAB Matrices
19

A matrix with only one column is called a column vector.

A column vector can be created in MATLAB as follows (note the semicolons):

» colvec = [13 ; 45 ; -2] colvec = 13 45 -2


MATLAB Matrices
20

A matrix can be created in MATLAB as follows (note the

commas AND semicolons):
» matrix = [1 , 2 , 3 ; 4 , 5 ,6 ; 7 , 8 , 9]

matrix =
1 4 7 2 5 8 3 6 9


Extracting a Sub-Matrix
21

A portion of a matrix can be extracted and stored in a

smaller matrix by specifying the names of both matrices and the rows and columns to extract. The syntax is: sub_matrix = matrix ( r1 : r2 , c1 : c2 ) ; where r1 and r2 specify the beginning and ending rows and c1 and c2 specify the beginning and ending columns to be extracted to make the new matrix.


MATLAB Matrices
22



A column vector can be extracted from a matrix. As an example we create a matrix below:



Here we extract column 2 of the matrix and make a column vector:

» matrix=[1,2,3;4,5,6;7,8,9] matrix = 123 456 789

» col_two=matrix( : , 2) col_two =

2 5 8


MATLAB Matrices
23

A row vector can be

extracted from a matrix. As an example we create a matrix below:

Here we extract row 2 of

» matrix=[1,2,3;4,5,6;7,8,9] matrix = 1 4 7 2 5 8 3 6 9

the matrix and make a row vector. Note that the 2:2 specifies the second row and the 1:3 specifies which columns of the row.

» rowvec=matrix(2 : 2 , 1 : 3) rowvec = 4 5 6


Topics..
24

What is MATLAB ??
Basic Matrix Operations Script Files and M-files Some more Operations and Functions

APPLICATIONS: Plotting functions .. Image Processing Basics .. Robotics Applications .. GUI Design and Programming


Use of M-File
25

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.
Click to create a new M-File




M-File as script file
26
Save file as filename.m

Type what you want to do, eg. Create matrices

If you include ";" at the end of each statement, result will not be shown immediately

Run the file by typing the filename in the command window


Reading Data from files
27

MATLAB supports reading an entire file and creating a

matrix of the data with one statement.

>> load mydata.dat; % loads file into matrix. % The matrix may be a scalar, a vector, or a % matrix with multiple rows and columns. The % matrix will be named mydata. >> size (mydata) % size will return the number % of rows and number of % columns in the matrix >> length (myvector) % length will return the total % no. of elements in myvector


Topics..
28

What is MATLAB ??
Basic Matrix Operations Script Files and M-files Some more Operations and Functions

APPLICATIONS: Plotting functions .. Image Processing Basics .. Robotics Applications .. GUI Design and Programming


Matlab Selection Structures
29

An if - elseif - else structure in MATLAB.

Note that elseif is one word. if expression1 % is true % execute these commands elseif expression2 % is true % execute these commands else % the default % execute these commands end


MATLAB Repetition Structures
30

A for loop in MATLAB for x = array for ind = 1:100 b(ind)=sin(ind/10) end while loop in MATLAB while expression while x <= 10 % execute these commands end

x=0.1:0.1:10; b=sin(x); - Most of the loops can be avoided!!!


Scalar - Matrix Addition
31

» a=3; » b=[1, 2, b= 12 45 » c= b+a c= 45 78

3;4, 5, 6]
3 6 % Add a to each element of b 6 9


Scalar - Matrix Subtraction
32

» a=3; » b=[1, 2, b= 12 45 »c=b-a c= -2 -1 12

3;4, 5, 6]
3 6 %Subtract a from each element of b 0 3


Scalar - Matrix Multiplication
33

» a=3; » b=[1, 2, 3; 4, 5, 6] b= 123 456 » c = a * b % Multiply each element of b by a c= 369 12 15 18


Scalar - Matrix Division
34

» a=3; » b=[1, 2, b= 12 45 »c=b/a c= 0.3333 1.3333

3; 4, 5, 6]
3 6 % Divide each element of b by a 0.6667 1.0000 1.6667 2.0000


The use of "." ­ "Element" Operation
35

Given A:

Divide each element of A by 2

Multiply each element of A by 3

Square each element of A


Topics..
36

What is MATLAB ??
Basic Matrix Operations Script Files and M-files Some more Operations and Functions

APPLICATIONS: Plotting functions .. Image Processing Basics .. Robotics Applications .. GUI Design and Programming


Plot
PLOT Linear plot.
PLOT(X,Y) plots vector Y

Example x = [-3 -2 -1 0 1 2 3]; y1 = (x.^2) -1; plot(x, y1,'bo-.');

versus vector X

PLOT(Y) plots the columns of

Y versus their index

PLOT(X,Y,S) with plot

symbols and colors

See also SEMILOGX,

SEMILOGY, TITLE, XLABEL, YLABEL, AXIS, AXES, HOLD, COLORDEF, LEGEND, SUBPLOT...

37


Plot Properties
Example

XLABEL X-axis label.


XLABEL('text') adds text beside the X-axis on the current axis.

... xlabel('x values'); ylabel('y values');

YLABEL Y-axis label.
YLABEL('text') adds text

beside the Y-axis on the current axis.

38


Hold
Example

HOLD Hold current graph.
HOLD ON holds the current

plot and all axis properties so that subsequent graphing commands add to the existing graph.
HOLD OFF returns to the

... hold on; y2 = x + 2; plot(x, y2, 'g+:');

default mode
HOLD, by itself, toggles the

hold state.

39


Subplot
SUBPLOT Create axes in tiled positions.
SUBPLOT(m,n,p), or

SUBPLOT(mnp), breaks the Figure window into an m-by-n matrix of small axes

Example
x = [-3 -2 -1 0 1 2 3]; y1 = (x.^2) -1; % Plot y1 on the top subplot(2,1,1); plot(x, y1,'bo-.'); xlabel('x values'); ylabel('y values'); % Plot y2 on the bottom subplot(2,1,2); y2 = x + 2; plot(x, y2, 'g+:');

40


Figure
FIGURE Create figure window.
FIGURE, by itself, creates a

new figure window, and returns its handle. Example
x = [-3 -2 -1 0 1 2 3]; y1 = (x.^2) -1; % Plot y1 in the 1st Figure plot(x, y1,'bo-.'); xlabel('x values'); ylabel('y values'); % Plot y2 in the 2nd Figure figure y2 = x + 2; plot(x, y2, 'g+:'); 41


Surface Plot
x = 0:0 y = 0:0 [xx, yy zz=sin( surf(xx xlabel( ylabel( .1:2; .1:2; ]=m xx.^2 ,yy,z 'X ax 'Y ax

eshgrid(x,y); +yy.^2); z) es') es')

42


3 D Surface Plot
contourf-colorbar-plot3-waterfall-contour3-mesh-surf

43


Topics..
44

What is MATLAB ??
Basic Matrix Operations Script Files and M-files Some more Operations and Functions

APPLICATIONS: Plotting functions .. Image Processing Basics .. Robotics Applications .. GUI Design and Programming


Image Processing Toolbox
45

The Image Processing Toolbox is a collection of functions

that extend the capability of the MATLAB ® numeric computing environment. The toolbox supports a wide range of image processing operations, including:




Geometric operations Neighborhood and block operations Linear filtering and filter design Transforms Image analysis and enhancement Binary image operations Region of interest operations


MATLAB Image Types
46

Intensity images Binary images

RGB images

: [0,1] or uint8 : {0,1} : m-by-n-by-3


Intensity Images
» image = ind2gray(x,map); » imshow(image);

47


Binary Images

» imshow(edge(image));

48


RGB Images

49


Image Display
50

image







imagesc imshow colorbar getimagetruesize zoom -

create and display image object scale and display as image display image display colorbar get image data from axes adjust display size of image zoom in and zoom out of 2D plot


Image Conversion
51

Gray2ind

im2bw
Im2double Im2uint8 Im2uint16 Ind2gray mat2gray rgb2gray rgb2ind

-

intensity image to index image image to binary image to double precision image to 8-bit unsigned integers image to 16-bit unsigned integers indexed image to intensity image matrix to intensity image RGB image to grayscale RGB image to indexed image


IMAGE ENHANCEMENT
52



Adjust intensity


imadjust histeq

>>im2 = histeq(im); >>imshow(im2)

Noise removal linear filtering median filtering adaptive filtering