Introduction On Matlab: Environment, Mathematics, Programming and Data Types, and For Information About
Introduction On Matlab: Environment, Mathematics, Programming and Data Types, and For Information About
1. For an introduction on Matlab we strongly recommend the student to read the systems
on-line help, particularly the sections MATLAB and subsections Development
Environment, Mathematics, Programming and Data Types, and for information about
Matlabs graphical resources, the student should read the subsections Graphics, 3-D
Visualization and Creating Graphical User Interfaces.
Exercises:
2. Image Types:
RGB:
Create 3 matrixes (Red, Green and Blue) with the same dimensions, containing
values between 0 and 1.
To see the contents of the matrixes, type their names and then ENTER. Notice
that if you place the character ; at the end of a line, the content of the variable is
not shown on the screen. And it is very important to notice that Matlab is case
sensitive.
Concatenate the three matrixes to create a single three dimensional matrix (RGB)
using the function cat (use the help to learn about the function).
1
Example:
RGB=cat( 3, RED, GREEN, BLUE);
Visualize the content of the matrix RGB. The two first indices of the elements of
the matrix define, respectively, their line and column numbers. The third index
refers now to one of the three original matrixes. If we now consider matrix RGB
as an image, the first two indices define the coordinates of a pixel, and the third,
one of the RGB components of the pixel.
Indexed Image:
Create a matrix with the name map, containing 3 lines and three columns, and
values between 0 and 1, to represent a color map. Create a two dimensional
matrix X, with integer values between 1 and 3. Visualize the indexed image using
the functions:
2
Gray Level Image:
Visualize the matrix as a gray level (intensity) image with the functions (where N
is the number of gray levels to be shown):
Binary Image:
Repeat the previous exercise using exclusively the values 0 and 1 for the elements
of X (binary image).
Image Reading:
Create an image using the program Paint (Windows), and save the image in two
different formats: 256 colors and 24 bits.
Try to find out, with the aid of the function whos if the images are RGB or
indexed images (to learn about the function type help whos).
Image Writing:
Save one of the images created in the previous exercise with the bmp format
using the function imwrite.
3
Open the saved image with the Paint program.
Experiment:
Read the image forest.tif and visualize it. What sort of image is it?
4. Image Conversion:
With the aid of the help on-line, learn to use the functions who, whos and
clear.
Transform the images created in the previous exercises into different image types
and visualize the resulting images.
5. Visualization:
Learn to use the function figure. Show two different images in different
windows.
Learn to use the functions subplot and subimage. Show the two images of the
previous exercise in a single window using the functions.
Learn to use the function title. Place titles on the windows of the previous
exercises.
4
6. Matrixes Basics:
Observe what happens when the character ; is placed at the end of the
command.
Calculate the sum of the four corner cells of the matrix, referencing the respective
indices.
A
A(1,3) + A(3,1)
A(1:3,3)
A(1:4,2:4)
A(:,3)
A(1:3,:)
Create a 5x3 matrix B, and apply the command A(:,[3 5 2])=B(:,1:3)
A(:)
A
A(:,[1 2 2 3 3 3 4 4 4 4])
A([1 2 2 3 3 3 4 4 4 4],:)
A(A>10)=0
8. Concatenation:
Create a 2x2 matrix A. Use the following commands and check their outputs:
B=[A 2*A]
5
10. The colon : operator:
X=[2.5:0.4:4.0]
X=[2.5:4.5]
Type help arith and learn about the operators: +, -, *, .*, /, ./, ^,
.^.
Experiment with the commands * and .*, and with / and ./.
Create a script that calculates the length of edge c of a triangle, given the length
of the other two edges a and b, and the angle between edges a and b. Use the
formula:
c2 = a2 + b2 2ab cos
Learn to use the statements "if " , "for", "switch", "while" and "break" from
Matlabs help.
6
14. Function Graphs:
Create a vector x containing 100 values between 0 and 2*pi. Create the vector y
containing the sine of each value in x (use the function sin). Graph the sine
function (use the plot(x,y) function). To alter the axis characteristics, read the
help section for the function axis.
Create a function to calculate the roots of the quadratic equation for arbitrary a,
b and c coefficients and plot the equation.
Read the help section on the function save. Save in a file a (set of) variable(s)
from your workspace.
Clear your workspace (clear all), and confirm that it is really empty (whos).
Read the help section on the function load and load the saved variable(s). Check
your workspace.
Proposed Exercises:
The first two input parameters of the function are column vectors with two
elements each (2x1) the first element corresponds to the x coordinate and the
second, to the y coordinate of a pixel.
The third parameter is a character that defines one of three possibilities: e for
Euclidean distance; m for Manhatan distance; or c for chessboard distance.
7
1. Performance Investigation:
Create a function that calculates the logarithm of the elements of a square (NxN)
matrix in two different ways. The input matrix should be initialized with random
values (use the function rand) and the output of the function must be a matrix
with the same size of the input.
Initially implement the function using a loop that calculates at each iteration the
log of a single element of the input matrix. Then implement the function using
vector processing, that is, apply the function log to the entire input matrix.
Modify the function to evaluate the execution time for different values of N (use
the functions plot, hold, xlabel, ylabel, legend and title).