MATLAB LESSON 2
Creating Matrices
Although MATLAB does not required formal memory allocation, it is a good idea to do so when working with matrices. MATLAB tends to respect the format in the following declarations:
zeros is used to create a matrix of all zeros
Z = zeros(2,4)
Z =
0 0 0 0
0 0 0 0
ones is used to create a matrix of all ones
F = 5*ones(3,3)
F =
5 5 5
5 5 5
5 5 5
fix is used to round elements toward 0. rand generates uniformly distributed numbers.
N = fix(10*rand(1,10))
N =
4 9 4 4 8 5 2 6 8 0
randn generates normally distributed numbers
R = randn(4,4)
R =
1.0668 0.2944 -0.6918 -1.4410
0.0593 -1.3362 0.8580 0.5711
-0.0956 0.7143 1.2540 -0.3999
-0.8323 1.6236 -1.5937 0.6900
When
in doubt, read the help for any function! (help randn, for example)
The
load and save commands are used to retrieve from or store data on
disk, using .mat files.
save
‘variables’ will
save all variables we have been working with to the file ‘variables.mat’.
To
verify that, clear the workspace with clear all and the list the
workspace variable with who.
Now
load ‘variables’ should load the variables back. Verify with who
again.
Concatenation
The square brackets [ ] are the concatenation operators. Matrices can be concatenated as if they were Lego blocks. Their sizes must fit together to form complete rectangles or cubes.
Let’s draw a cross with numbers 0 and 1:
a0 = zeros(3, 2)
b1 = ones(3, 2)
x = [a0, b1, a0; b1, b1, b1; a0, b1, a0]
Deleting Rows and Columns
Let’s reduce the cross a little bit, by removing the first and last columns and the first and last rows.
small_x = x
small_x (:,[1 end]) = [];
small_x ([1 end],:) = [];
In the previous example, end means the last row or the last column if you don’t know its value.
Caveat: small_x (:,[1: end]) = [] will erase all the columns in small_x! [1 end] means 1 and end, [1:end] means 1 to end.
If the matrix after the deletion of the elements does not result in a regular quadrilateral, the operation is illegal. Try removing one element from the corner of small_x. small_x (1,1) = []
But, small_x (1) = [] works. Notice that now small_x lost its shape! If you use only one numbers as an index, Matlab considers an array as a long sequence of columns.
Linear
Algebra
Addition and subtraction work with matrices and scalars, provided that the matrices have the same dimensions.
a = [1 2; 3 4]
b = a + a
c = a + 2
b is a result of adding two matrices. c is the result of adding a matrix to a scalar.
d = a – ones(size(a))
e = a - 1
Here we are using two different methods to achieve the same goal. ones (size(a)) creates a “mask” of ones, of the same size as a. This is very useful to speed up and eliminate for loops when operating on large data structures.
Multiplication and division work as they would for matrices in linear algebra. A * B only works if A is m x n and B is n x p
f = ones(2,3) * 2
g = ones(3,2) * 3
Notice the difference between f * g and g * f.
We can become quite creative and create a multiplication table.
N = 1:12
Table = N’*N
Isn’t this shorter than writing for loops?
Now, try this S = N.*N
What is this table showing? .* is actually an array (not matrix) multiplication. Elements of N are paired one by one and multiplied. The results are placed in S in the same position they would appear in N
Now, you can explain the difference between N / N and
N./N
If you remember matrix algebra, you’ll remember that A / B is the same as AB-1 (i.e, inv(B)), provided that B is square. MATLAB accepts both notations.
Table / N works, for the same reason that N./N works. MATLAB in this case treats each row as a vector and the result in each row represents the vector division between Table and N.
Table * inv(N) doesn’t work in this case, because N isn’t a square matrix.
Another two useful array operations are the determinant and the identity matrix.
L = [2 1; 2 2]
det(L)
Generate the identity matrix with eye. eye(2)
gives the same result as L * inv(L)
Powers are the same as a series of multiplications, the ^ operator respects the same rules as the * operator
Compare L ^ 2 with L.^2. Are there other ways to perform the same operations using only L?