Title: BINF 5030
1BINF 5030 MATLAB COMMANDS To Initiate MATLAB Try
the following in the MATLAB Window (Note
MATLAB's prompt is " gtgt ") . To invoke MATLAB go
to START-Programs/ Programming/ imaging or double
click on programming icon.
2You can use MATLAB for image data in following
file formats .BMP - Bitmap file .TIF Tagged
Image File Format .JPG Joint Photographic
Experts Group format .PCX .HDF .XWD Display of an
image imread (flowers.tif) If reading from a
drive, use (a\ filename) imshow filename
imshow flowers.tif
3Types of Images Intensity Images Matlab stores
intensity images as a single two dimensional
matrix, with each element of the matrix
corresponding to one image pixel. The matrix can
be of class double, in which case it contains
intensity values in the range of 0-1 or class
uint8, in the range of 0-255, 0 representing
black while 255 being white. Binary images Each
pixel assumes only one of the two possible values
of 0 and 1. The image can be uint8 or double. The
toolbox uses a logic flag to signify that the
data is in the range 0, 1. If the flag is off,
the data is assumed to be 0, 255.
4Indexed Images An indexed image consists of two
arrays, an image matrix and a color map matrix.
The color map is an ordered set of values that
represents the colors in the image. For each
image pixel, the image matrix contains a value
that is an index into the color map ( the maximum
entries in the color map is 255). Image
matrix color map ( R G B) 14 17 21 5 0
0 0 1 3 7 4 .06 .06
.031 4 31 21 27 0.29 .06
.06 0.50 .250 .200 To display an indexed
image use imshow(X,map) you specify image name
and map.
5NTSC Format The main advantage of this format is
that the grayscale information is available along
with color data.Luminance component is same as
gray information, and the other components are
hue and saturation. RGB imread
(flowers.tif) YIQ rgb2ntsc(RGB) YIQ(,,1)
is gray info. RGB Images RGB image represents
each pixel color as a set of three values,
representing red, green and blue intensities that
make up the color. RGB image is represented by a
single m x n x 3 matrix in the form of three
aligned planes of the size m x n. Multi-frame
arrays Represent collection of images such as MRI
slices or movie
6frames. Each separate image is called a frame.
These frames are concatenated along in the fourth
dimension. For example an image array with five
color frames of 400 x 300 size will be
represented as a 400 x 300 x 3 x 5. FRM3
MULTI(,,,3) MULTI is the name of image and
FRM3 represents third frame. Mathematical
operations on image matrices in the Matlab are
supported for the class double ( not in uint8).
You can convert an image in the form double by B
double (A) A im2double(A) A
im2uint8(A) You can also specify the number of
gray levels to be displayed in a gray
image imshow(I,32) shows the image I with 32
gray levels.
7Image Processing Operations Reading an
image... Iimread('flowers.tif') left side of
equation is user defined
filename should be in single quotes Displaying
the image imshow(I)
If you have not named the image , you can also
use imview(moon.tif) imshow(moon.tif)
8 Image resizing Jimresize(I,0.5) halves the
image size figure, imshow(J) Image resized to
half
9Image Rotation K imrotate(I,45,'bilinear')
45 degree rotation in both dimensions figure,
imshow(K) Rotation by 45 degrees bilinear
command generates subpixel balues based on
bilinear extrapolation.
10IMAGE CONVERSION Converting an image to a
GRAY/INTENSITY image I1 rgb2gray(I) figure,
imshow(I1) Displays the gray image in a new
window
GRAY/INTENSITY Image
11Converting an image to an INDEXED
image Original Image is I and it has already
been read X,maprgb2ind(I,4) figure,
imshow(X,map) Figure V INDEXED IMAGE
12Converting to NTSC format figure,
imshow(I) Nrgb2ntsc(I) n abs(N)Converting
all image matrix values to absolute figure,
imshow(n) Some of the intensity values could
become negative. Ensure that none of these values
are negative.
Convert an intensity image to an indexed
image (x,map) gray2ind(I,n) It will convert
an intensity image I into an indexed image x with
color map n. If n is omitted , it is taken as 64
13Obtaining an Image Profile Obtaining the
profile of the selected portion of the
image. Just type Iimread('flowers.tif') imshow(
I) gt improfile and press Carriage Return The
image shows up for which profile is desired
Using the mouse, click on on two points keep
right side of mouse pressed. You should get a
profile as shown on the next transparency. The
profile is for all the color components.
14(No Transcript)
15To crop an image, Cimcrop Now select an
image to be cropped Select the size using the
curser imshow(C) display cropped image
Select the area to be cropped using the mouse by
dragging it onto image while holding down the
mouse button.
16Image Area BW imread (circbw.tif) AREA
bwarea(BW) AREA followed by enter will give you
area in terms of number of pixels.. Perimeter
Determination BW1 imread(circbw.tif) BW2
bwperim(BW1) Imshow(BW1) Figure, imshow(BW2)
17Pixel Intensity Values in an Image Iimread('flo
wers.tif') imshow(I) After retrieving and
displaying image, type gt impixel the image
will be displayed, click on any number of
points. The answer shown as ans 97
72 0 244 241 231 19 14 21
18Image Histogram imread ( name of the
image) figure, imhist(I,64) 64 is default
value. 2n can be specified
19Image Contour Rimread('rice.tif') imshow(R)
To achieve image contour use command gt
imcontour gt figure, imcontour(R)
20Averaging Filter I imread (blood1.tif) H
fspecial (average,5) I2 uint8(round(filter2(H
,I))) imshow(I) figure, imshow(I2) Binary Edges
of an image I imread (ic.tif) BW
edge(I) imshow(I) Figure, imshow(BW)
21(No Transcript)
22Edge Detection Bimread('blood1.tif') imshow(
B) Cedge(B,'sobel') Applying "sobel" filter
on image B figure, imshow ( C) . You can use
other edge filters also. Sobel, Prewitt, Roberts,
Log, Zerocross are typical filters
23Histogram Equalization Improving an image by
Pimread('flowers.tif') imshow(P) Jhisteq(P)
figure,imshow(J)
24The histogram of this new image is as follows
(compare with earlier histogram)
25Adding Noise to an Image Iimread('eight.tif')
imshow(I) Jimnoise(I,'salt pepper',0.05)
Salt pepper is type of noise
0.05 is intensity of noise
level figure, imshow(J) displaying noisy image
26Removing Noise - Defining special filters to
clear noise Kfilter2(fspecial('average',3),J)/25
5 averaging filter figure, imshow
(K) Lmedfilt2(J,3,3) Using another filter
operation median filter figure, imshow(L)
Take image satern.tif and add gaussian noise with
5 intensity. Subsequently get rid of this
noise. Which filter is best? Find out.
27Image Enhancement - using imadjust Rimread('ri
ce.tif') imshow(R) Jimadjust(R,0.15 0.9,0
1) ( image name, original intensity range,
adjusted range) figure,imshow(J)
28imadjust can be used to lighten or darken part
on an image. Example BW imread (
text.tif) I imread(cameraman.tif) F
inline (imadjust(x,,,0.3) transforms
values low to high to top and bottom. Gamma
specifies correction factor. I2
roifilt2(I,BW,F) processes data in I using
function F. Results are computed for pixel
locations where BW contains 1 ( white). imshow(I2)
29(No Transcript)
30You can also display a binary image using a color
map. If the image is of class uint8, 0 displays
the first color in the color map and 1 displays
the second color imshow(BW,1 0 0 0 0 1 )
displays 0 as red and 1 as blue. If the image
is of class double, you need to add 1 to each
value in the image matrix imshow (BW1, 1 0 0
0 0 1 ) Selecting an Object in a Binary Image
Scene bwselect (BW1, c, r,n) n is type of
connectivity and BW1 is image name. c and r
represent image coordinates for selection of
object. You can also select the object using
mouse also. BW1 imread (circbw.tif)BW2
bwselect ( BW1, n)
31The cursor changes to a cross-hair when it is
over the image. Click on the objects you want to
select bwselect displays a small star over each
pixel you click on. When you are done, press
ENTER. bwselect returns a binary image
consisting of the objects you selected, and
removes the stars. Note Whenever you plan to
perform arithmetic operations on a image, always
convert the image matrix into floating point
representation format. B double (A) Above
command converts an intensity image into double
format. If you want arithmetic operations on the
image matrix B, the image has to be in floating
point mode.
32Selecting an Object in a BINARY image
bwselect(BW,c,r,n) BW is image name, c and r
refers to image coordinates, n is the type of
connectivity. The mouse can be used to select the
coordinates/ location. BWimread('text.tif')
c16 90 144 r85 197 245 BW1bwselect(BW,
c,r,4) imshow (BW) figure, imshow(BW1)
33(No Transcript)
34Multilevel Thresholding grayslice Creates indexed
image from an intensity image using multilevel
thresholding. The general format is X
grayslice(I,n). This command thresholds the
intensity image I into n slices. It can create
artificial colors in the image. It thresholds the
intensity image I using cutoff values, returning
an indexed image in X. You can view the
thresholded image using imshow(X,map) with a
colormap of appropriate length. Iimread('ngc4024
m.tif') Xgrayslice(I,16) imshow(I) figure,imsho
w(X, jet(16))
35 36Morphological Operations These operations are
used to enhance the clarity of binary image for
further processing, object counting etc. The
command line is BW2bwmorph(BW1,'operation')
BW1 is the image in which a particular operation
is to be done Operations included are skel,
dilate, erode, fill,remove , etc.
Example BW1imread('circles.tif') imshow(BW1)
BW2bwmorph(BW1,'dilate',3) BW3bwmorph(BW1,'remo
ve') figure, imshow(BW2) figure, imshow(BW3)
37(No Transcript)
38 39To Count the number of objects in a BINARY
Image It is advisable to perform certain
morphological operations before counting the
objects, otherwise the count would be incorrect
Iimread('rice.tif') Rim2bw(I) Convert to
a binary image before start the count process.
eulbweuler(R,n) n is type of connectivity. eul
81
40Selection of the Region Of Interest
Iimread('eight.tif') imshow(I) BWroipoly
select region of interest and press ENTER
figure, imshow(BW) Filtering the Selected
Region You can change contrast of the region of
interest You can use roifilt2 function to
process a region of interest. When you call
roifilt2, you specify an intensity image, a
binary mask, and a filter. roifilt2 filters the
input image, and returns an image that consists
of filtered values for pixels where
41 42the binary mask contains values 1 and unfiltered
values for pixels where the binary mask contains
values 0. hfspecial('unsharp',0.5) 0.5 is the
degree of sharpness. I2roifilt2(h,I,BW)
figure, imshow(I2)
43Superimposition of Multiple Number of
Images I imread('rice.tif') J
imread('cameraman.tif') K imadd(I,J) imshow(K)
The images must be of same size
44bwlevel The bwlabel function performs
connected-components labeling, which is a method
for indicating each discrete object in a binary
image. For example, suppose you have the binary
image BW. bwlabel ( BW, n) BW is a binary
image and n is type of connectivity. If BW is
defined as a binary image, bwlabel(BW, 4) will
return labeled objects as shown in next
transparency. The objects is defined as per 4
connectivity.
45BW1 0 0 0 0 0 0 0
0 0 1 1 0 0 1 1 1 0
1 1 0 0 0 1 1 0 1
1 0 0 0 0 0 0 0 0
1 1 0 0 0 0 0 0 1
1 0 0 0 0 0 0 1 1
0 0 0 0 0 0 0 0 0
0 0 You call bwlabel, specifying
4-connected neighborhoods. L,num
bwlabel(BW,4) L 0 0 0 0 0
0 0 0 0 1 1 0 0 3
3 3 0 1 1 0 0 0 3
3 0 1 1 0 0 0 0 0 0
0 0 2 2 0 0 0 0 0
0 2 2 0 0 0 0 0 0
2 2 0 0 0 0 0 0 0
0 0 0 0
r c find L 2 rc r c rc 5 4
5 5 6 4 6 5
7 4 7 5
46Display of Multiple Frames of an Image load mri
mri is the name of image with multiple
frames. montage (D,map) will display a
multiframe indexed image Converting an Array to a
Movie movimmovie(D,map) movie(mov)
47- conv 2
- Performs a two dimensional convolution
- C conv2(A,B, full) options are full,
same, and valid - conv2 produces the output image by performing
these steps - Rotate the convolution kernel, B,by 180 degrees
to produce a computational molecule. - Determine the center pixel of the computational
molecule. - Apply the computational molecule to each pixel in
the input image. - It performs a two dimensional convolution of A
and B, returning the output in C. The size in
each dimension in C is sum of both matrices A and
B and is - ma mb -1 , na nb -1
48Two Dimensional Correlation Function) corr2 Comp
utes two dimensional correlation coefficient
between two matrices and the matrices must be of
the same size. r corr2 (A,B) gtgt A1 2 34 5
6 gtgt B10 20 30 40 50 60 gtgt r
corr2(A,B) r 1 This shows a perfect
correlation between the matrix A and B.
49Frequency Domain Transforms dct2 computes two
dimensional discrete cosine transform. idct2
performs two dimensional inverse discrete cosine
transform. These are frequency domain
elements. RGB imread (autumn.tif) I
rgb2gray(RGB) J dct2 (I) taking
logarithm imshow (log(abs(J)),),
colormap(jet(64)), colorbar Now set values
smaller than 10 in dct matrix to 0 and
reconstruct the image using idct. J(abs(J)lt10)
0 K idct2(J)/255 imshow(K)
50Original Image
Post operations image
51- Subimage command
- Displays multiple images in the same figure.
Types of images are - subimage(X,map) subimage(I) subimage(BW)
- subimage(RGB)
- subimage(I) displays the intensity image I in the
current axes. - subimage(BW) displays the binary image BW in the
current axes. - subimage(RGB) displays the truecolor image RGB in
the current axes. - subimage(X,map) displays the indexed image X
- You can use subimage in conjunction with subplot
to create figures with multiple images, even if
the images have different colormaps.
52- subimage works by converting images to truecolor
for display purposes, thus avoiding colormap
conflicts. - subimage(X,map) displays the indexed image X with
colormap map in the current axes. - Support The input image can be of class uint8,
uint16, or double. - Example
- load trees this is an indexed image under the
name (X, map) - X2,map2 imread('forest.tif')
- subplot(1,2,1), subimage(X, map)
- subplot(1,2,2), subimage(X2,map2)
- It will display
53(No Transcript)
54- Representation of RGB/NTSC Images
- Any color image ( RGB or NTSC ) will consist of
three image frames of equal size. - Let us say if an image is of size 200 x 200 , and
the name of the image is RGB. - It will be stored as matrix RGB(200,200,3)
- RGB (200,200,1) will consist red intensity values
- RGB (200,200,2) will consist green intensity
values. - RGB (200,200,3) will consist blue intensity
values.
55 To display the selected color ( red )components
of an image. A imread ('flowers.tif')
imshow (A) A(,,2) 0 A(,,3) 0
imshow(A)
56Reading image from a drive. i imread (a\
example\ saturn.tif) Getting image
information info imfinfo (a\ example\
saturn.tif) info this command provides
detailed information about the image. Following
command creates a grayscale intensity image from
an RGB image and displays as n intensity
levels. I rgb2gray (i) imshow (I, n )
Displays the image I with n gray levels. N must
be an integral power of 2.
57Processing a Region of Interest from an Image
Scene. roicolor command BW roicolor(A,low,high)
Description roicolor selects a region of
interest within an indexed or intensity image and
returns a binary image. (You can use the returned
image as a mask for masked filtering using
roifilt2.) BW roicolor(A,low,high) returns a
region of interest selected as those pixels that
lie within the colormap range low high BW
(A gt low) (A lt high) BW is a binary image
with 0's outside the region of interest and 1's
inside.
58Example I imread (rice.tif) BW roicolor
(I, 128, 255) imshow(I) figure, imshow (BW) h
fspecial (unsharp) J roifilt2(h,I,BW)
imshow(J) blkproc implements distinct
blockprocesing for an image. B blkproc(A,m n,
fun) or Bblkproc(A,m n,mborder nborder,
fun) ( pads m x n blocks by mborder x nborder )
59(No Transcript)
60A function handle can be used to define the
function in blkproc by using _at_ I
imread('cameraman.tif') imshow(I) func
_at_dct2 J blkproc(I,8 8,func) figure,
imshow(J), colormap(hot)
61(No Transcript)
62Block Processing Bblkproc(A,m,n, fun)
Processes the image A by applying the function
to each m x n block of A, padding A with zeros,
if necessary. The function should operate on m x
n block, and return a matrix, vector or a scalar
y y fun(x) in blkproc does not require that y
be same size as x. However, if B is of the same
size as A, only then y is same size as
x. Example This example uses blkproc to set the
pixels in each 8 x 8 block to the standard
deviation of the elements in that block. I
imread(alumgrns.tif) fun inline('std2(x)ones
(size(x))') I2 blkproc(I,8 8,fun)
63or the last two commands can be written as one
single command I2 blkproc(I,8,8,std2(x)one
s(size(x))) imshow(I) figure, imshow(I2,)
64Adding two Image Matrices If images are defined
as type double. You can perform arithmetic
operations on two images using for loop. Assume
that images b and c have been defined previously
and are of size 100 x 100, and you are interested
to add these two images. for i 1100 for j
1100 b(i,j) b (i,j) c(i,j) end end imshow
(b) You can perform arithmetic operations on
image matrices provided the images are of type
double.
65Measure properties of image regions SyntaxSTATS
regionprops(L,properties) DescriptionSTATS
regionprops(L,properties) measures a set of
properties for each labeled region in the label
matrix L. Positive integer elements of L
correspond to different regions. BW
imread('text.tif') L bwlabel(BW) stats
regionprops(L,'all') stats(23)
66ans Area 89
Centroid 95.6742 192.9775 BoundingBox
87.5000 184.5000 16 15 MajorAxisLength
19.9127 MinorAxisLength 14.2953
Eccentricity 0.6961 Orientation 9.0845
ConvexHull 28x2 double
ConvexImage 15x16 uint8 ConvexArea
205 Image 15x16 uint8
FilledImage 15x16 uint8 FilledArea
122 EulerNumber 0 Extrema
8x2 double EquivDiameter 10.6451
Solidity 0.4341 Extent 0.3708
PixelList 89x2 double
67label2rgb Convert a label matrix into an RGB
image SyntaxRGB label2rgb(L) RGB
label2rgb(L,map) RGB label2rgb(L) converts a
label matrix L, such as those returned by bwlabel
or watershed, into an RGB color image for the
purpose of visualizing the labeled regions. The
label2rgb function determines the color to assign
to each object based on the number of objects in
the label matrix and range of colors in the
colormap. The label2rgb function picks colors
from the entire range. RGB label2rgb(L,map)
defines the colormap to be used in the RGB image.
I imread('rice.tif') figure, imshow(I),
title('original image') BW im2bw(I,
graythresh(I)) L bwlabel(BW) RGB
label2rgb(L) RGB2 label2rgb(L, 'spring', 'c',
'shuffle') imshow(RGB), figure, imshow(RGB2)
68Matrices in MATLAB Basic Mathematical operations
in matrices Declaration of a matrix A defining
the contents gtgt A 1 2 3 4 The
output is A 1 2 3 4 To
redisplay, simply type gtgtA
69Defining another matrix B gtgtB3 45 6 Product
of matrices A and B gtgt AB gives ans 13
16 29 36 Verify this answer and try
another example with a 3x 3 matrix.
70gtgt BA ans 15 22 23 34 Note AB
and BA are not the same, i.e, they are
non-commutative You can also invert a matrix by
using command inv (C) Note that inv(C) C will
give you an identity matrix. Division of two
matrices is represented as A/B.
71Addition of matrices gtgt A B ans
4 6 8 10 gtgt C A B Storing the
result in matrix C Subtraction gtgtA-B gtgt
B-A ans ans -2 -2 2 2
-2 -2 2 2
72Inversion of matrices A matrix can be inverted
ONLY if its determinant is NON ZERO. gtgtdet(A) ans
-2 gtgt inv(A) ans -2.0000 1.0000
1.5000 -0.5000
73Product of Matrix, and its inverse is an IDENTITY
MATRIX gtgt Ainv(A) ans 1.0000 0
0.0000 1.0000 The Division can also be
written as gtgt B / A This also means B
inv(A) We can also write gtgt A \ B which
is the same as gtgt inv(A) B
74Systems of Equations Now consider a linear
equation ax by p
cx dy q We can write this more compactly as
AX B where the coefficient
matrix A is the vector of unknowns is
75and the vector on the right-hand side is
If A is invertible, X (1/A)B, or,
using Matlab notation, X A\B. Solve Ax b
with A as before and b 1 0 . Note that b is
a column vector. gtgt b 1 0
gtgt A\b ans -2.0000 1.5000
76Loops Suppose A be the matrix a
0.8 0.1 0.2 0.9 and let x be
the column vector x 1 0 We
regard x as representing (for example) the
population state of an island. The first entry
(1) gives the fraction of the population in the
west half of the island, the second entry (0)
give the fraction in the east half. The state of
the population T units of time later is given by
the rule y ax. This expresses the fact that an
individual in the west half stays put with
probability 0.8 and moves east with probability
0.2 (note 0.8 0.2 1), and the fact that in
individual
77in the east stays put with probability 0.9 and
moves west with probability 0.1. Thus, successive
population states can be predicted/computed by
repeated matrix multiplication. This can be done
by the following Matlab program Running a for
loop to come to the result as to how many people
will be in each area of the island a 0.8
0.1 0.2 0.9 x 1 0 for i 120, x
ax end gt a 0.8 0.1 0.2 0.9
gt x 1 0 gt for i 120, x ax,
end What did you learn? how to write a loop to
do much of the repetitive work.
78Graphs and Plots - Functions of one variable To
make a graph of y sin(t) on the interval t 0
to t 10, the following is done gtgt t
00.210 gtgt y sin(t) gtgt plot(t,y)
79Explanation of the previous code and graph The
command t 00.210 defines a vector with
components ranging from 0 to 10 in increments of
0.2. The y sin(t) defines a vector whose
components are sin(0), sin(0.2), sin(0.4), etc.
Finally, the command plot(t,y) uses the vectors
t and y to construct the graph.
80Functions of two variables Consider a 2
dimensional function z(x,y) x e (-
x2 - y2) To plot it - do the following in
MATLAB gt x,y meshgrid(-20.22,
-20.22) gt z x . exp(-x.2 - y.2) gt
mesh(z) The first command creates a matrix
whose entries are the points of a grid in the
square -2 lt x lt 2, -2 lt y lt 2. The small
squares which make up the grid are 0.2 units wide
and 0.2 units high. The
second command creates a matrix whose entries are
the values of the function z(x,y) at the grid
points. The third command uses this information
to construct the graph.
81(No Transcript)
82if, else and elseif if evaluates a logical
expression and executes a group of statements
based on the logical value of the expression. In
its simplest form the expression is if
logical_expression statements end Example if
rem(a,2) 0 disp( a is even) b a/2 end
83while loop The while loop executes a statement
or a group of statements repeatedly as long as
the controlling condition ( expression) is
true. while expression Statements end for
loop The for loop executes a statement or a group
of statements a predetermined number of times.
Its syntex is for index start increment
end Statements end
84The default increment is 1 you can specify any
increment, including a negative value. The
execution terminates when the value of index
exceeds the end value. For negative increments,
it terminates when the index is less than the end
value. Example X(1) 0.3333 for i 26 x(i)
2 x(i-1) end x 0.3333 0.6677
1.3355 2.6709 5.3418 10.6837
85NESTED LOOPS for i 1 m for j 1n a(i,j)
1/(ij-1) end end Vectorizing Loops The
M-file code can be speeded by vectorizing
algorithms. Vectorization means converting for
and while loops to equivalent vector or matrix
operations, e.g. i 0 for t 00.0110 i
i1 y(i) sin(t) end
A vectorized version of the same code is t
00.0110 y sin(t) This executes much faster.
86Image Matrix Operations. Ensure that all the
intensity matrix values have been defined as
DOUBLE. X double ( A) will define X as type-
double. Once defined as double, matrices can be
added, subtracted , and multiplied as usual. X
A B will multiply two matrices. B eye (n)
defines B as an identity matrix of size n x n. D
det (A) defines determinant of matrix A X inv
(A) defines inverse of matrix A Matrix power is
defined as X A n, sqrtm(A) and expm(A)
define square root and exponential functions
87Data Plotting t 0 0.3 0.8 1.1 1.6 2.3
3.5 / y 0.5 0.82 1.00 0.14 1.25 1.35
1.40 / plot ( t,y, o), grid on o is the
symbol for data points plotted Defining a
Matrix A 5 7 8 0 1 9 4 3 6 defines a 3
x 3 matrix A(,,2) 1 0 4 3 5 6 9 8 7
defines page 2 of the matrix A. A(,,3) 5
will assign all values to page 3 of the matrix as
5. To Increase the Size of a Matrix Let us say
that A is a 4 x 4 matrix and values have been
defined. Define A(4,5) 0 It will cause matrix A
to be of size 4 x 5 with 5th column as 0.
88Concatenating Matrices B A A 32 A 48
A 16 will increase the original matrix A
which is 4 x 4 to an 8 x 8 matrix. A matrix is
stored in MATLAB space column wise. Deleting Rows
and Columns You can delete rows and columns of a
matrix using just a pair of square bracket. X
A then delete the second column of X by X( ,
2) . This will delete second column of the
matrix X.
89Representing Polynomials in Matlab p(x) x3 -
2x -5 Enter this as p 1 0 -2 -5 You
can get the roots of the polynomial by using the
command root(p). You can evaluate the polynomial
by using the polyval (p,x) command. This command
evaluates the value of the polynomial for any
given value of x. Example gtgt x3 gtgt p1 0 -2
-5 gtgt polyval(p,x) ans 16
90Polynomial curve fitting p polyfit (
x,y,n) where x and y are equal size vectors
containing the x and y data points to be fitted
and n is the order of polynomial to
return. Consider x 1 2 3 4 5 y
5.5 4.31 12.8 290.7 489.4 p polyfit(
x,y,3) will fit third order polynomial. p
-0.1917 31.5821 -60.3262 35.34 You can plot
the estimated values over the real data values
for comparison. x2 1 0.1 5 y2 polyval
(p, x2) plot ( x, y, '', x2, y2) grid on
91 92Regression and Curve Fitting Polynomial
Regression Data are modeled by a polynomial
function y a0 a1 t a2 t2 . The
coefficients are computed by the least square
fit. It is represented by x ones(size(t))
t t.2 The solution is found using
backlash operator. That is a x \ y ( y ax
is the relationship) Example t 0 0.3
0.8 1.1 1.6 2.3' y 0.5 0.82
1.14 1.25 1.35 1.40 ' a.5318 .919
-0.2387
93..Continued T (0 0.1 2.5)' Y
ones(size(T)) T T.2a plot ( T, Y, '', t,
y, 'o'), grid on
94Linear-in- Parameter Regression The form is y
a0 a1 e -t a2 t e - t t 0 0.3
0.8 1.1 1.6 2.3' y 0.5 0.82
1.14 1.25 1.35 1.40 ' x
ones(size(t)),exp(-t),t.(exp (-t)) a
x\y This plots fit is better than the first
model. plot(t,y,''), grid on The value of a
is 1.3974 -0.8988 0.4097
95(No Transcript)
96Multiple Regression If y is a function of more
than one independent variables, the matrix
equation that expresses the relationship among
the variables can be expanded to accommodate the
additional data. Let us take the following x1
0.2 0.5 0.6 0.8 1.0 1.1 x2
0.1 0.3 0.4 0.9 1.1 1.4 y
0.17 0.26 0.28 0.23 0.27 0.24
The multivariate model of the data will be y
a0 a1 x1 a2 x2 define using x
ones( size(x1)) x1 x2 a x \ y
97Multiple regression solves for unknown
coefficients by performing the least square fit.
The solution will be a 0.1018 0.4844 -0.284
7 The least square fit model is a 0.1018
0.4844 x1 - 0.2847 x2 To validate the model, find
the maximum of the absolute value of the
deviation of the data from the model. Y
xa MaxErr max (abs(Y-y)) MaxErr 0.0038
98e.g. x1 0.2 0.5 0.6 0.8 1.0 1.1
' x2 0.1 0.3 0.4 0.9 1.1 1.4
' y 0.17 0.26 0.28 0.23 0.27
0.24 ' x ones( size(x1)) x1 x2
a x \ y T (00.12.5)' Y
ones(size(T)) T T a plot(T,Y,'-',x1
x2,y,'o'), grid on
99(No Transcript)
100Some of the images available in the Matlab
database are Coins.png m83.tif Fabric.png onion
.tif Football.jpg rice.png Greens.jpg spine.tif K
ids.tif moon.tif