Lab (1) - Image Processing
Introduction to Matrix manipulation in Matlab

Objectives:
In this lab you will learn about:
Arrays and their operations in Matlab
Matrices and their operations in Matlab
Linear Algebra in Matlab

In this lab you will learn about some of the commands used in Matlab.

Throughout this semester we use Matlab to perform some image processing related tasks.  In most of these tasks we use matrix manipulation.  Using the built-in tools in Matlab, we can simplify our calculations significantly.   In this lab, we will learn about some of the tools and commands.

You can cut and paste most of the instructions on this manual at the Matlab prompt or to a .m file.

## Arrays (Vectors)

A vector is what we usually refer to as a one-dimensional array (1-D array).  The array can be of different types.  Here, we assume that an array holds real or integer values.  A signal can be viewed as a 1-D array.  The 1-D array representing a signal holds the discrete values corresponding to the amplitude at different times.  For example: a signal with a sin shape is shown on Figure (1).  The signal is generated for the time period of 0 to 8 with a 0.25 step size.  Following you will see the values for t followed by the corresponding values of sin(t).

Values for t:
0    0.2500    0.5000    0.7500    1.0000    1.2500    1.5000 1.7500    2.0000    2.2500    2.5000    2.7500    3.0000    3.2500  3.5000    3.7500    4.0000    4.2500    4.5000    4.7500    5.0000  5.2500    5.5000    5.7500    6.0000    6.2500    6.5000    6.7500 7.0000    7.2500    7.5000    7.7500    8.0000

Corresponding sin(t) values:
0    0.2474    0.4794    0.6816    0.8415    0.9490    0.9975   0.9840    0.9093    0.7781    0.5985    0.3817    0.1411   -0.1082   -0.3508   -0.5716   -0.7568   -0.8950   -0.9775   -0.9993   -0.9589   -0.8589   -0.7055   -0.5083   -0.2794   -0.0332    0.2151    0.4500  0.6570    0.8231    0.9380    0.9946    0.9894

Figure (1) - Sin (t) function

As you may have noticed, vector t consists of a set of numbers (1-D array).  Thus, sin(t) creates another array consisting of sin values corresponding to the t.  In Matlab we create vector t by typing:

t = [0 0.25 0.5 0.75 1.0 1.25 1.5 1.75  2.0  2.25 2.50 2.75 3.00 3.25  3.5 3.75 4.0 4.25 4.5  4.75  5.0  5.25  5.5  5.75  6.0  6.25 6.5  6.75 7.0  7.25  7.50 7.750 8.0]

This will generate an output that looks like this:
t =
Columns 1 through 7
0    0.2500    0.5000    0.7500    1.0000    1.2500    1.5000
Columns 8 through 14
1.7500    2.0000    2.2500    2.5000    2.7500    3.0000    3.2500
Columns 15 through 21
3.5000    3.7500    4.0000    4.2500    4.5000    4.7500    5.0000
Columns 22 through 28
5.2500    5.5000    5.7500    6.0000    6.2500    6.5000    6.7500
Columns 29 through 33
7.0000    7.2500    7.5000    7.7500    8.0000

There are many built-in functions in Matlab, for example: sin, cos, sqrt, .... We can also build own own functions.  In the following example we will compute sin(t) for all t values. To do this, at the prompt, type y = sin(t).  Note that since t is an array, then y will be an array that holds exactly the same number of elements as array t.  I.e., for each ti we have a yi = sin(ti).
>> y = sin(t)

y =
Columns 1 through 7
0    0.2474    0.4794    0.6816    0.8415    0.9490    0.9975
Columns 8 through 14
0.9840    0.9093    0.7781    0.5985    0.3817    0.1411   -0.1082
Columns 15 through 21
-0.3508   -0.5716   -0.7568   -0.8950   -0.9775   -0.9993   -0.9589
Columns 22 through 28
-0.8589   -0.7055   -0.5083   -0.2794   -0.0332    0.2151    0.4500
Columns 29 through 33
0.6570    0.8231    0.9380    0.9946    0.9894

Please note that to clear values of t from the memory, you need to type >> clear t.

As you perhaps noticed in the above example, t started from 0 and went up 0.25 by 0.25 until we got to 8.  We can use a different method to generate t values and avoid typing all those numbers.

Another method for creating the same array:

>> t = 0:0.25:8

This will result in the same numbers for t as you had before.  Then, you can generate the sin(t) values in the same way as before.
>> y = sin(t).

A quick note:  All angles must be represented in Radian.  In order to convert an angle, t, to Radian, we use (t*pi/180).  For example: sin(45) = 0.7071, but if you type at the Matlab prompt:
>> sin(45) you will get 0.8509.  In order to get the right answer, you have to type:
>> y = sin(45*pi/180), which results into the right answer, 0.7071.

Manipulation of arrays:
Suppose, we want to compute sin(2t).  Notice that you want to do this for all t values.  If you wanted to do this in C++, you would use a loop to go through all t values and you would multiply the values by 2, then you would compute sin(2t) .  But here, this done much easier and faster.
>> clear t
>> clear y
>> t = 0:0.25:8
>> z = t + t
>> y = sin(z)

In the following exercise(s), use vector t given above.
Exercise (1) : sin(2t) = 2 sin(t)cos(t).  Use Matlab to find the values for sin(2t) using the above procedure and using 2sin(t)cos(t).  Confirm that both methods generate the same results.

Exercise (1) G:  (Graduate students only) :  sin2(t) + cos2(t) = 1, use matlab to do this using array operations.

## Matrices

A matrix is what we refer to as an arrays with more than one dimension.  There are many commands designed in Matlab to do matrix manipulations.  In Matlab, setting up matrices can be done the same way as done for vectors, except that each row of elements in the matrix is separated by a semicolon (;) or by a Carriage return.  Let's look at an example.

Suppose we have a 3x3 matrix, A:

A =
20 22 18
12 16 12
8  10  10

and another one with the same size, B:
B =
2 4 8
1 -1 6
-2 2 4

How do we enter these matrices in Matlab.
First method:
A = [20 22 18; 12 16 12; 8  10  10]

The output looks like this:
A =
20    22    18
12    16    12
8    10    10

Second method:
B =
[2 4 8
1 -1 6
-2 2 4]

The output looks like this:
B =
2     4     8
1    -1     6
-2     2     4

Let's try a few operations in linear algebra here.  Suppose, you want to find the transpose of matrix A, rows are interchanged with columns, and call the new matrix C.
>> C = A'
This generates the following output;
C =
20    12     8
22    16    10
18    12    10

Exercise (2): Suppose C is a complex matrix, what procedure would you use to generate its transpose?  compute the transpose matrix for C = A + jB, where A and B are given as above.

Suppose, we want to compute D = A * B.
>> D = A*B
D =
26    94   364
16    56   240
6    42   164

Exercise (3): How do you multiply the corresponding elements of matrix A and B ?  Please note that I didn't ask you to multiply A * B.  I want you to tell me how to multiply A(1,1) by B(1,1), and A(1,2) by B(1,2), ....?  Find matrix C the result of such a multiplication between A and B.

Exercise (3) G: How do we find a matrix A to the power of n?  One way to compute An is to multiply A by itself n times, but there is an easier way to do this using Matlab commands. How?

You can also find the inverse of a matrix:

>> X = inv(A)
X =
0.3125   -0.3125   -0.1875
-0.1875    0.4375   -0.1875
-0.0625   -0.1875    0.4375

Did you know how much work you needed to do if you wanted to do this using traditional method?  Please make sure you know how the inverse of a matrix is computed.

We can find the eigenvalues of a matrix too.

>> eig(A)
ans =
42.4939
2.0000
1.5061

There is even a function to find the coefficients of the characteristic polynomial of a matrix. The "poly" function creates a vector that includes the coefficients of the characteristic polynomial.

>> p = poly(A)

p =
1.0000  -46.0000  152.0000 -128.0000

We can find the roots of the characteristic polynomial, which is the same as eigenvalues:
>> roots(p)
ans =
42.4939
2.0000
1.5061

ns = 5.3723 -0.3723

In this lab, you tried several Matlab commands individually. You could put all the commands that you want to execute together in a m-file, and just run the file.

Postlab - Due Next Wednesday
Suppose we have matrix A (8x8) all integers defined as:

127 120   0     255  250  170  150 100
227 220   200 255  250  170   150 100
17   20    100  255  250  100   120 100
120 10     0      55   150  70   250   200
120 10     0      55   150  70   250   200
17   20    100   255  250  100  120 100
227 220   200   255  250 170  150 100
127 120   0      255  250  170  150 100

Compute Ai,j+1 = (Ai,j+1 - Ai,j) and Ai,j = Ai,j + (Ai,j+1)/2 where i is the index used for rows and j is the index used for columns. Note that the the first number (top-left) has index (0,0).  Assume 0 is even. Thus, in this matrix, columns with i or j = 0, 2, 4, and 6 are the even columns.  Also note that when you evaluate the second statement, Ai,j+1 has the new value as computed in the first statement.

Example: In the above matrix,    First) 120 will be replaced with 120-127 = -7 and
Second) 127 will be replaced with 127+(-7/2) = 127 -3 = 124

You need to repeat this for all pairs on each row until all rows are processed.