![]() |
||||||
![]() |
![]() |
|||||
![]() |
![]() |
![]() |
||||
![]() |
|
![]() |
||||
![]() |
|
|||||
![]() |
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
3.3 Assignment - Lecture 3In 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.
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.
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).
|
|||||||||||||||||||||||