Aqua Phoenix
     >>  Lectures >>  Matlab 4  


4.4 Assignment - Lecture 4

In this assignment you will experiment with conditionals and function m-files.

4.4.1 Part A: Cartman in 2-bit color

Using the discussed example code from the lecture notes as a starting point, modify the conditional of the color-reduction algorithm to include one more "bit" for possible colors. In other words, possible intensity values now include 0, 85, 170, and 255. For example, a black to red gradient in this color space would be grad1=(0,0,0), grad2=(85,0,0), grad3=(170,0,0), grad4=(255,0,0).

You will have to partition the existing color space of 0..255 into four portions, and make decisions based on the existing intensity value to transform them into the new space of (0,85,170,255). Note that the four partitions will not be of equal size - intensities reduced to value 0 will come from a space between 0 and X, while intensities reduced to value 85 will NOT come from a space between 85 and Y. In other words, the set of new intensity values (0,85,170,255) act as centers, and values in proximity of those centers will be replaced by the center values. The center of zero will not have any values to the left, because negative values do not exist in the intensity space. The greatest proximity value for center zero is one less than the smallest proximity value for center 85. You should make some decision as to what range of original color values maps to the new color values. Be sure to document the decision in your m-file as comments. You may hard-code the values in your code.

You are required to use one sequence of conditionals, including if, elseif's, and else. Display the image after modifying it.

4.4.2 Part B: The Snake revisited (still not a real one)

Recall this exercise from the last assignment. You were required to use for-loops to fill a matrix with numbers, such that the path of increasing numbers forms a snake. Using several for-loops is one way of completing the problem, but it is much more efficient to use conditionals as well. You would use one variable for keeping track of the increasing number. Its value is then placed into the matrix in a nested for loop, where the outer loop iterates over all rows, and the inner loop iterates over columns. The conditional is used to decide which direction the inner loop takes on; whether it goes from left to right, or right to left. The decision is based on whether the row number is odd or even. Implement this method of using a nested for-loop and conditionals to make the decision of direction.

4.4.3 Part C: Area of an Ellipse

Write a function that computes the area of an ellipse. This function takes as it input either one or two arguments. If one argument has been passed then you are to assume that both axes are equally long, i.e. a circle.

Important: The arguments passed to the function correspond to half the short and long axes, see Figure 4.18.

Figure 4.18
You will need to consider the following in your function:
  1. Checking of arguments: make sure that the arguments are not negative. If they are negative, the area should automatically be set to 0 (zero).
  2. Include comments that describe the purpose of the function and its usage
  3. You must include a conditional to check for the number of arguments passed
  4. You may only allow 1 or 2 arguments passed to the function; any other number of arguments should cause an appropriate error to be displayed. See in-class exercise for a complete example!

Hint: The number of arguments passed, i.e. whether 1 or 2 or more, is assigned to the variable named nargin (number of arguments in). This variable is available within a function - you need not define it yourself. You should use this variable in your if statement to figure out how to compute the area (either circle or ellipse).

Hint: The scalar of Π is pre-defined in Matlab as the variable name pi .

4.4.4 Part D: size representation

Write a function that takes as input one argument: a certain number of unnamed units. The returned value for this function is a formatted string that best represents the numerical value in terms of common size representation (e.g. milli, kilo, Mega, etc.). You will have to decide which quantity is most suitable for the input. For example, if the input to the function is 18000, then a representative value is best described in kilo: 18 Kilo; similarly, 5620000 is 5.62 Mega, 123456789 is 123.45 Mega, 0.00001 is 10 nano, etc. You will have to implement the logic behind deciding how far to simplify (divide or multiply) a number. Because naming convention for units suggests splitting after every 1000, you should have at most 3 non-zero digits before the decimal after any conversion. Below is a table of conversions you are responsible for. The returned value should be a string, which includes the converted numerical value, as well as the abbreviated and non-abbreviated units, e.g. your_function(18000) returns '18 k (kilo)'. You can produce a string that includes a number using the following expression:

strcat(num2str(x), ' abbr.unit (full named unit)') where num2str(x) converts number x to a string, and strcat(a,b) concatenates strings a and b.

Exa E 1018
Peta P 1015
Tera T 1012
Giga G 109
Mega M 106
Kilo k 103
Milli m 10-3
Micro mu 10-6
Nano n 10-9
Pico p 10-12
Femto f 10-15
Atto a 10-18