Preparation for Spring 2010 Final Exam
We will have a written,
closed-book examination in the classroom from 8am to 11am on
June 11th.
I will ask general questions about Matlab, similar to the Midterm Test.
In addition, I will ask specific
programming questions related to the solutions to the Spring
2009 Final Exam and the following
"warmup programming problem" which you should solve ahead of the exam
date. You will NOT
be asked to turn in your solution to the "warmup" problem, but I will
ask questions about it on the
exam which will be very difficult to answer if you did not solve it!
Warmup Programming Problem
Write and test a Matlab function m-file called Matrix_Shift. It should accept
either two or
three input parameters:
- M, which must be a two
dimensional matrix, and
- either two scalars r and
c, or a two-element vector,
pos
and generate a new two-dimensional matrix as its single output. The
parameters r and c, or equivalently
pos(1) and pos(2) tell the new location of the
element M(1,1) in the output.
For example, suppose you
have already created a 4 x 3 matrix named test_M in the workspace that
contains the following data:
1
|
2
|
3
|
4
|
5
|
6
|
7
|
8
|
9
|
10
|
11
|
12
|
and you execute the following Matlab command:
>>
new_M = Matrix_Shift (test_M, 3, 8);
Then the variable new_M should
be a 4 x 3 array that contains the following data:
9
|
7
|
8
|
12
|
10
|
11
|
3
|
1
|
2
|
6
|
4
|
5
|
In other words, I have marked array element M(1,1) with yellow shading
so you can see where
the shift takes it. Because the value of the second parameter is 3, we
shift the rows
by two steps
downwards to bring row 1 down to row 3. At the same time row 2
becomes row 4, row 3
becomes row 1, and row 4 becomes row 2. Similarly, because the
third parameter is 8, we shift
the columns
seven steps to the right -- which is two complete circles plus one
more, and bringing
column 1 to column 2. At the same time column 2 becomes column 3 and
column 3 becomes
column 1.
In order to do a complete solution to this programming problem, your
function should handle all
of the following situations:
- Checks its input parameters to make sure
- the total number of parameters is either 2 or 3
- the first parameter is a two dimensional array (or matrix),
- the other parameters are either two scalars or one two-element
vector
- the values of the other parameters are integers (but they don't
need to be positive or smaller than the matrix size)
- Correctly handle the case where the values for the row and/or
column shifts are:
- zero
- a small positive or negative value, or
- a large integer value where you must find the final shift after
dropping some number of complete circles
- Have the function check its work!
- At least make sure that output(r,c) == input(1,1)
You should understand how to program this function two ways:
- applying built-in Matlab matrix-manipulation functions described
in section 5.11 of the textbook
- writing some loops to copy the data from the input matrix to
output matrix under your control.
- you should find the ":" notation for addressing an entire row
or column helpful here!
That's it. Good luck!