http://www.informatics.sussex.ac.uk/users/bg22/mtcs/mtcs3a.html
< o = ones(5,3) > creates a matrix of 1's with 5 rows and 3 columns.
NB: An m x n matrix has m rows and n columns. (Rows first, then columns.)
< r = rand(4,4) > creates a matrix of random numbers with 4 rows and 4 columns.
Enter an explicit list of elements using square brackets [ ]:
< m1 = [2 3 4] > creates a matrix with 1 row and 3 columns.
< m2 = [5; 6; 7] > creates a matrix with 3 rows and 1 column (the semicolon separates the rows).
< m3 = [1 1; 3 3; 5 5] > creates a matrix with 3 rows and 2 columns.
Type the following into the editor (use spaces or tabs between the numbers) -
16 3 2 13
5 10 11 8
9 6 7 12
4 15 14 1
Save this as a file called magic1.dat and load it into matlab using the command
< load magic1.dat >
The matrix will now be in your workspace, type < magic1 > to see it.
Every entry of the matrix is multiplied separately by the scalar.
Multiply the matrix < m3 > by 4 and call it < m4
>.
Divide < m4 > by 2.
The elements of each matrix
are multiplied by the corresponding element in the other matrix
which must
have the same shape.
Two matrices can only be multiplied if the number of columns in the first is
equal to the
number of rows in the second.
If matrix A has m rows and n columns: A(m,n)
and matrix B has n rows and p columns: B(n,p)
then their product, matrix C, has m rows and p columns:
A(m,n) x B(n,p) = C(m,p)
Each
element in the first row of A is multiplied by the
corresponding element in the
first column of B and the products
added to give the first element of C:
c11 = (a11*b11 + a12*b21 + a13*b31 + ... a1nbn1)
Similarly,
the next element of C in the first row is given by the sum of the products
of the elements in the first row of A and the second column of B.
For example:
On
paper, try multiplying the following pairs of matrices.
Then check if you
are correct using matlab.
To access an element in row 3 and column 2 of matrix A type < A(3,2) >.
< sum > | sums the elements of a matrix column-wise |
< rot90 > | rotate a matrix by 90— |
< flipud > | flip matrix upside-down |
< fliplr > | flip the matrix left-right |
< diag > | extracts matrix elements on the main diagonal (top left to bottom right) |
Now go to Part b