Review from Day 2:

`Creating Matrices:`
` `

zeros(m, n) à Creates a m x n matrix or array that will be used in addition;

zeros(size(A)) à Creates a matrix or arrays of zeros with the same size as A;

ones(m, n) à Create a m x n matrix or array that will be used in multiplication;

ones(size(A)) à Creates a matrix or ones of zeros with the same size as A;

Rounding Functions:

fix(X) à rounds the elements of X to the nearest integers towards zero.

round(X) à rounds the elements of X to the nearest integers.

ceil(X) à rounds the elements of X to the nearest integers towards infinity.

floor(X) à rounds the elements of X to the nearest integers towards minus infinity.

Example: Applying the previous functions to X = [-1.5 -1.49 1.49 1.5] results in:

fix(X) =  [ -1    -1     1     1 ]                           round(X) = [ -2    -1     1     2 ]

ceil(X) = [ -1    -1     2     2  ]                         floor(X) = [ -2    -2     1     1 ]

Note that fix behaves as ceil when X < 0 and as floor when X > 0.

Random Number Generation:

rand(m, n) generates a m x n array of uniformly distributed numbers between 1 and 0.

randn(m, n) generates a m x n array of normally distributed numbers with mean 0 and std deviation 1.

save and load are used to quickly store or retrieve MATLAB variables. They work form both command line and m-files (programs)

Matrix and Array Operations:

; in a matrix declaration separates rows.        , separates columns.

end when indexing an array stands for the last index in that dimension (the last row, column, page, etc)

Setting columns or rows = [] is an effective way to delete them.

Matrix Operations:      +          -           *          /           ^          inv       det

Array or Vector Operations:  +           -           .*         ./          .^         .’

Remember the previous example:       N = 1:12                      Table = N’*N.

Table is a square matrix, but it does not an inverse because det(Table) = 0. 1/Table is the same as 1/0.

Table/N works because MATLAB treats this as row by row vector division. The result is 12 rows and each row represents Table(row, :) / N. The result for each row is equivalent to s, where sv = u means u is parallel to v, but s times longer.

Remember the previous example  L = [2 1; 2 2]. Here are the results of some operations:

L ^ 2 = L * L = [ 6, 4; 8, 6]                            L .^ 2 = L .* L = [ 4, 1; 4, 4]

L / L’ = [ 1.5, –1; 1, 0]                                   L ./ L’ = [ 1, 0.5; 2, 1]

det (L) = 2                                                       det(L')= 2

inv(L) = [ 1,  -0.5; -1, 1 ]                               inv(L') = [ 1, -1; -0.5, 1 ]

Can you remember what this all means?

A little help: det([a, b; c, d]) = a * d – b * c.

Now the exercises. To get into the habit, keep your code in a m-file. Create a new program, type in your code. Save it with a good name, for example, ex1.m. Then, you can call it by typing ex1 from the command line.

Exercises:

1.     Produce a table of 20 rows. Each row should have the row number (n), n squared and the nth power of 2. Use only matrix operations.

2.     Let 1 be a white square and 0 be a black square. Create a matrix that would represent a chess board.

3.     randperm(N) is a random permutation of the integers from 1 to n. For example, randperm(6) might be [2 4 5 6 1 3].  So let’s use it to produce very quick bingo cards. Bingo cards have 5 rows and 5 columns. The first columns has 5 numbers between 1 and 15, the second has 5 between 16 and 30, etc. A good way to start is to generate 5 different sequences of 15 numbers, use only the first 5 and put them all together, column by column. Using the concatenation and other matrix operations, create this 5 x 5 bingo card. Make this into a program, so we can generate many cards.