More on Matrices
- More ways of contructing matrices
- More Matrix operations
- Using matrices to solve systems of equations
- Saving and loading matrices
More ways of constructing matrices
Built-in ConstructionsThere are many built-in matrix constructions. Here are a few:
| Input | Output | Comments |
|---|---|---|
| rand(2) rand(2,3) |
ans = 0.9501 0.6068 0.2311 0.4860 ans = 0.8913 0.4565 0.8214 0.7621 0.0185 0.4447 |
Generates a matrix with entries randomly distributed between 0 and 1 |
| zeros(2) ones(2) |
ans = 0 0 0 0 ans = 1 1 1 1 |
Generates a 2x2 matrix with all zero (or all ones) entries. |
| eye(2) |
ans = 1 0 0 1 |
Identity matrix I. |
| hilb(3) |
ans = 1.0000 0.5000 0.3333 0.5000 0.3333 0.2500 0.3333 0.2500 0.2000 |
3x3 Hilbert matrix. |
To get more information on these, look at the help page for special matrices- type help elmat .
Concatenation
New matrices may be formed out of old ones. Suppose we have
a = [1 2; 3 4]
a = 1 2
3 4
Then we can make a new matrix in any of the following ways.
| Input | Output |
|---|---|
| [a, a, a] |
ans = 1 2 1 2 1 2 3 4 3 4 3 4 |
| [a; a; a] |
ans = 1 2 3 4 1 2 3 4 1 2 3 4 |
| [a, zeros(2); zeros(2), a'] |
ans = 1 2 0 0 3 4 0 0 0 0 1 3 0 0 2 4 |
Programming
Matrices may also be constructed by programming. Here is an example using two "for" loops.
for i=1:10,
for j=1:10,
t(i,j) = i/j;
end
end
Notice that there isn't any output, since the only line that would produce any (t(i,j) = i/j;) ends in a semi-colon. Without the semi-colon, Matlab would print the matrix t 100 times!
We'll cover programming in more detail later.
More matrix operations
As we saw earlier, +, -, *, and / are defined in an intuitive manner for matrices. When there is ambiguity about whether an operation will do matrix arithmetic (versus entry-by-entry arithmetic), note that ".*" (dot-star) will multiply entry-by-entry, and "*" (star) will do matrix multiplication.
Scalars
A scalar is just a number. Matlab stores them internally as 1x1 matrices, but treats them as if they were numbers.
All operation involving a scalar and a matrix affect the matrix on an entry-by-entry basis, with one exception: the power ("^") operator. With a as defined above, try these:
| Input | Output | Comments |
|---|---|---|
| b=2 | b=2 | Define b to be a scalar. |
| a + b | ans = 3 4 5 6 |
Addition works entry-by-entry. |
| a * b | ans = 2 4 6 8 |
So does multiplication. |
| a ^ b | ans = 7 10 15 22 |
This is matrix power - a*a |
| a .^ b | ans = 1 4 9 16 |
Entry-by-entry power. |
Vectors
A vector is just a matrix with only one row (or column). Matlab makes a distinction between row vectors and column vectors, and will complain if
it doesn't get what it expects.
In all arithmetic operations, Matlab treats a vector as a matrix, so we already know how to multiply a vector by a scalar. Matlab also gives you the following operations:
| Input | Output | Comments |
|---|---|---|
| v = [1 2 3] u = [3 2 1] |
v = [1 2 3] u = [3 2 1] |
Define a pair of vectors. |
| v * u | Error | The dimensions don't agree. |
| v * u' | ans = 10 | Taking the transpose works. |
| dot(v,u) | ans = 10 | The dot product is the same thing. |
| cross(v,u) | ans = -4 8 -4 | The cross product works only for 3-d vectors. |
Matrices
Matlab has all the common operations built-in, as well as most of the obscure ones.
| Input | Output | Comments |
|---|---|---|
| k = 16 2 3 5 11 10 9 7 6 |
k = 16 2 3 5 11 10 9 7 6 |
Define a matrix. |
| rref(k) | ans = 1 0 0 0 1 0 0 0 1 |
Row-reduced echelon form |
| rank(k) | ans = 3 | The rank. |
| det(k) | ans = -136 | The determinant. |
| inv(k) | ans = 0.0294 -0.0662 0.0956 -0.4412 -0.5074 1.0662 0.4706 0.6912 -1.2206 |
Inverse of the matrix |
| [vec,val] = eig(k) | vec = -0.4712 -0.4975 -0.0621 -0.6884 0.8282 -0.6379 -0.5514 0.2581 0.7676 val = 22.4319 0 0 0 11.1136 0 0 0 -0.5455 |
Eigenvectors and eigenvalues of the matrix. The columns of "vec" are the eigenvectors, and the diagonal entries of "val" are the eigenvaules. |
Solving Equations
One of the main uses of matrices is in representing systems of linear equations. If a is a matrix containing the coefficients of a system of linear equations, x is a column vector containing the "unknowns," and b is the column vector of "right-hand sides," the constant terms, then the matrix equation
If you know a little linear algebra, you could use the Matlab commands from above to augment the matrix and then find the row-reduced echelon form. However, Matlab provides a more simple mechanism
for solving linear equations:
x = a \ b
This can be read "x equals a-inverse times b."
Try it with
a = [1 2 3; 4 5 6; 7 8 10]; b = [1 1 1]';
You should get
x = -1 1 0
To verify this assertion, try this:
a*x, a*x - b, eps
The results are:
ans = 1 1 1
ans = 1.0e-015 *
-0.1110
-0.6661
-0.2220
ans = 2.2204e-016
Notice that a*x - b is very close to eps - which means that it is as close to zero as possible.
If there is no solution, a "least-squares" solution is provided (a*x - b is as small as possible). Enter
a(3,3) = 9; b = [1 1 0]';
(which makes the matrix singular and changes b) and enter the above commands again, using the up-arrow to recall them. Notice that the solution is quite inaccurate.
Saving and loading matrices
Unlike Maple and Mathematica, Matlab has no notion of a "worksheet." When you exit Matlab, you will not be prompted to save your work.If you would like a record of your work, you can turn on "logging" by typing
diary 'm:\scratch\session.txt' on
Windows machines
diary '~/session.txt' on Unix.
Notice that output will be saved alongside your input, so the file can't be used directly as a script.
You may just want to save one or more matrices. The diary is a very clumsy way to do this.
To save the value of the variable "x" to a plain text file named "x.value" use
save x.value x -ascii
To save all variables in a file named "mysession.mat" in reloadable format, use
save mysession
To restore the session, use
load mysession
Then, to see the saved files from your session, on UNIX systems type
the commands:
% more session.txt % more x.valueUnder Windows open the appropriate file with Notepad.



