# Lab 2

## Matrix Operations

Last time, we learned how to create, select, and modify matrices. MATLAB is powerful because it allows one to perform matrix operations. In this lab, we will practice mathematical operations on matrices.

### Arithmetic Operations

To begin, create these matrices (i.e., copy and paste them into the Command Window or script):

A = [ 3 4 8; 16 21 22 ];

B = [ 2 7 8 ; 16 20 22 ];

First, let's add A and B by entering the command:
``` AB = A + B ```
Examine the resulting matrix. Notice that many arithmetic operators (+, -, etc.) are defined for matrices according to the rules of linear algebra.

 Q1: Create a matrix C = [ 1 2 ; 3 4 ] and compute A + C. What did you get and why?

We can also add a single number (called an offset) to every element in a matrix. The command:
``` B5 = B + 5 ```
adds 5 to every element in B.

#### Multiplication and Division

Similar to addition, we can multiply a matrix by a scalar (called scaling). For example, the command:
``` A3 = 3 .* A ```
multiplies each element of A by 3. Note that the operator is ".*" and the "." is before the "*". This is the operator you need to use for element-wise multiplication. Note also that both operators ".*" and "*" have the same effect when multiplying or dividing a matrix by a scalar.

The command:
``` AB1 = A .* B ```
creates a new matrix AB1 where each element of AB1 is the product of corresponding elements in A and B. This is a so called element-wise operation. The period (".") before an operator indicates that the operation should be performed element-wise. For example, element-wise division is accomplished using "./". Another example is exponentiation, which is accomplished using the hat, i.e., ".^".

 Q2: Try: >> D = A*B What did you get and why? Now try: >> D = A*B' and >> E = A.*B Compare the results. What are the difference between the two operations?
 lab2_1.m: Ten particles are moving on a 2D grid. The particles move in straight lines and the initial and final positions are given by: ini =      1      1      9      9      5      4      7      9      1      6      3      2      8      8      9      9      3      7      0      1 and final =      8      5      1      2      6      8      7      1      6      3      7      4      8      9      9      2      7      6      5      7 where the first column represents the x coordinate and the second column represents the y coordinate. Calculate the straight-line distance (i.e., Euclidean distance) traveled by each particle. What is the maximum/minimum distance traveled by any one particle? Hint: Remember that taking the square root of a number is equivalent to raising it to the 0.5 power. For example, 160.5 = 4. The distance calculation can be performed in one line, however, it may be more straightforward to compute parts of the equation and save them as variables that can be used in subsequent steps.

### Boolean Operations

Suppose we have two scalar variables, s1 and s2, and we want to test whether they are equal. To check whether s1 and s2 are equal, we use the equality test ("=="). The result of a Boolean test on two matrices is a matrix of 1's (true) and 0's (false) called a Boolean matrix. In addition to the normal matrix operations, we can apply logical operators: & (and), | (or) and ~ (not).

 Q3: Let s1 = [10 2] and s2 = [10 3]. What value is returned when executing s1 == s2? What are the values of s1 and s2 afterward? Again, let s1 = [10 2] and s2 = [10 3]. What value is returned when executing s1 = s2? What are the values of s1 and s2 afterward? What is the difference between "=" and "=="?

It is very easy to mix up "=" and "==". This leads to strange bugs. Be careful.

There are other Boolean tests for not equals ("~="), greater than (">"), less than ("<"), greater than or equal to (">="), etc.

 Q4: Using the distances calculated in lab2_1, show a Boolean column vector that indicates which particles traveled a distance greater than 5. Using the distances calculated in lab2_1 and the find command (use help find or see Lab 1), show the indices of the particles that traveled a distance greater than 5.

#### Boolean Indexing

Boolean matrices and vectors can also be used to index (as opposed to using `find` to list the indexes). Some of the particles in lab2_1 have initial x coordinates less than 3. If we want all the particles to have an initial x coordinate of at least 3, we would write:
``` ini(ini(:,1) < 3,1) = 3 ```

 lab2_2.m: Suppose both the initial x and y coordinates of all the particles should be at least 2. Before, you begin, create a copy of the ini matrix called ini2 and only modify this matrix. Create a script that computes the new initial coordinates in ini2.

### Other Matrix Operations

Some functions accumulate the elements of matrices. For example, the `max` function computes the maximum value in each column and the `sum` function computes the sum of each column. See the help for these functions to learn how to make them compute row-wise accumulations.

 lab2_3.m: The rand function creates random matrices. For example, rand(3,9) produces a random 3 by 9 matrix. (See the help for the statisical properties of the generated numbers.) Use the rand function to generate a 200 by 300 matrix. Scale the matrix to be values between 0 and 100. Use the round function to convert the matrix to integers. Use the max and sum functions to return the sum and maximum over all elements of the matrix. Note: If you want to stop MATLAB from performing a long operation (like printing a large matrix), press Control-C.

MATLAB has an enormous number of functions for performing operations on matrices. A subset of these will be covered throughout the labs. Feel free to use MATLAB help to find other functions that might be useful to you. Chances are, someone has already written a function that does what you want.

 When you are ready, demonstrate your answers to Q1-Q4 and lab2_1.m, lab2_2.m, and lab2_3.m to the TA or instructor and answer some questions to checkout.