Aqua Phoenix
     >>  Lectures >>  Matlab 3  
 

Navigator
   
 
       
   

3.3 Assignment - Lecture 3

In this assignment you will experiment with one- and two-dimensional loops in the form of sounds and images.

Use vector and matrix operations where necessary. This includes arithmetic and indexing operations. For example, when asked to divide one vector by another, do it is not permitted to perform this operration explicitely on each pair of numbers - you should use vector operations instead.

Suppress the Command Window output of all steps, unless otherwise noted.

3.3.1 Part A - Single Loop: Fibonacci Numbers

In this part, you will produce the first 45 values of the Fibonacci number sequence. Create a 1x45 matrix (a vector) of length 45 and all zeros. Set the 2nd value to 1. Write a for loop that loops over the entire vector starting with position 3, and set the value at each iteration to the sum of the previous two values. You must use matrix subscripting (indexing) to retrieve the two previous values. For output, show the initial vector after you have initialized it with value 1 at position 2. Also show the final output after the loop has terminated. Suppress all other steps.

3.3.2 Part B - Single Loop: Sound vector

Generate a pseudo ambulance siren, a sound that is quickly increasing in pitch then decreasing in pitch, and repeating like this for a few iterations (not too many please). Using loops, create one vector that resembles the increasing pitch, and another vector that resembles the decreasing pitch. Concatenate the vectors a few times in sequence and play back the final sound vector. You may wish to use the material discussed in class, specifically the water drop sound as a starting point.

3.3.3 Part C - Nested Loops: 2D gradient

Generate a two dimensional gradient between 4 colors. It is recommended that you start with the in-class exercise in which we have generated a one dimensional gradient between 2 colors. You should think about expanding the code by including a second loop (a nested loop) that iterates over rows in addition to the already iterated-over columns. You may choose the 4 colors, but make sure that they are all different. Also note that some gradients are easier to generate than others. A simple and certainly acceptable example you may wish to work on is: Blue (0,0,255) (left upper corner), Cyan (0,255,255) (right upper corner), Red (255,0,0) (left lower corner), Yellow (255,255,0) (right lower corner). Your image can be any size, but for simplicity it is recommended that you use a range from 0-255 for intensities, and thus 1-256 for the dimensions of the image.

If you are having trouble thinking about the problem, you may wish to start by taking an empty sheet of paper and drawing in each corner the colors in RGB format. You can then figure out how the values of red, green, and blue change in terms of rows and columns. You can then use the derived pattern in the nested loop, e.g. if i is the variable that loops over rows, and j loops over columns, how can i and j effect the intensity of red, green, and blue in the gradient. Hint: They either increase DIRECTLY with the intensity or INVERSLY with the intensity. We have seen both cases in the in-class exercises.

3.3.4 Part D - Nested Loops: Snake (not a real one)

In this part, you will produce a matrix (8x8) in which the values are increasing in rows alternating between left-to-right and right-to-left. Here's an example of the output:

01234
98765
1011121314
...

You will need to build a matrix like this with 2 nested for-loops. In the first nested loop, you will populate all of the odd rows (1, 3, ...). In the second nested loop, you will populate the even rows. This is not the most compact way of creating the matrix, but assuming that we don't know conditionals yet (and you are not allowed to use them), we will have to break-down the problem this way.

Create a variable (scalar) and use it to keep track of the increasing value in the matrix. Initialize it to 0.

Write the first nested for-loop, in which the outer loop iterates over the odd rows, and the inner loop iterates over the columns in increasing order (left to right). You must use vector range notations here, i.e. 1:4 as opposed to [1,2,3,4], as well as vectors with decrementing (4,3,2,...) ranges. As the loop iterates over elements, place the value of your increasing scalar into the matrix at the appropriate position. Increment (add one to) the scalar after placing its value into the matrix. Once a row has been completed, you must take care of incrementing the scalar appropriately in order to skip a row.

Now set your scalar to the value, which should be in place in the last column of the second row. This value is one plus the value in the first row, last column. Write the second nested for-loop, in which the outer loop iterates over the even rows, and the inner loop iterates over the columns in decreasing order (right to left). Again, you must use the short vector range notations here. Repeat the steps from the first nested loop to complete the snake.

Display the matrix after the first loop, and the final matrix in the Command Window. Use the following command to get a colorful visualization of it:

image(n), colormap(jet(64)); ... assuming that your matrix is named n (you can name it whatever you like).