CS 99

Summer 2003: Lab 2                                                                   7/01

 

 

1.     Objectives

This lab will help you get comfortable with relational & logic operators, it will introduce you to some new helpful Matlab functions, and you will learn some techniques for generating random data.

 

2.     The Bottom Corner

Recall that to access the i,jth entry of a matrix A, we type A( i ,j ). 

 

The Matlab function size can be used to get the dimensions of an array.  For example, the assignment statement:

 

[ m, n ] = size(A)                             stores the number of rows of A in the variable m, and the number of columns of A in the variable n.

 

 

Task 1:

Write a script file that asks the user for an array of type double.  It should output 1 if the lower right entry of the array is > 0, and it should output 0 if the lower right entry is < 0.  Test this on two matrices: one with -1 in the bottom right corner and one with 1 in the bottom right corner.

 

          Since this is a lab, you do not need to comment your code in this m-file.  It should not be fancy – just a few lines of code.

      Record your work in a word document called LabTwo.doc.

 

3.     Random Task
In Matlab, sometimes we want to generate random data.  The function rand is quite helpful in doing this.  The rand function has the same syntax as the ones and zeros functions.

 

The rand function generates a uniformly distributed variable between 0 and 1.  Try typing commands of the form rand(n) and rand(m,n) for some integers m & n.

 

Task 2:

Read about the Matlab function round.  Use it with rand to generate a 10 x 10 matrix of zeros and ones, where each cell gets a value of 1 with probability ˝.  Assign this matrix to a variable named M.

 

Try to figure out how to use this to get a 10 x 10 matrix of zeros and nines, where each cell has a value of 9 with probability ˝.  Assign this matrix to a variable called N.

 

Type the command G = N > M at the command prompt, then type the command H = N>=M at the command prompt.  In a few sentences, explain what these commands do and explain the difference between these two matrices, G and H.  Does one matrix have more ones than the other? Why?

 

Make sure to include both the commands that you entered and the four matrices in LabTwo.doc.

 

4.     Yet Another Random Task
This time, we want to generate a matrix of random numbers of a different sort.

If we have two matrices, A & B, of the same size, then the operation A + B is defined by adding the corresponding entries in A and B.  In other words, the ith entry of A + B is equal to A( i ) + B( i ).  The difference A – B is defined by subtracting the corresponding entries in A and B.

We can also multiply an array by a scalar.  If C is an array and a is a scalar, then the product a*C is defined by multiplying each entry in C by a.

 

Spend a few minutes playing with these types of array operations in the command window.


Task 3:

Using these new operations and what you learned in Task 2, generate a 10 x 10 matrix of random entries between -100 and 100.  Copy and paste your commands and the resulting matrices into LabTwo.doc.

 

1.                 Submitting Your Work

Type your name (and your partner’s name if you have one), student ID, and the date at the top of LabTwo.doc.  Print the document and give it to the instructor or the teaching assistant.