CHAPTER 9

The Main Group of Matrix Operations

9.1 Matrix Multiply
9.2 Determinants and Matrix Division




We will expand on the use of matrix operations on arrays in this chapter. These operations give MATLAB (for Matrix Laboratory) its name. As we saw in the section on element by element operations, there are many cases where MATLAB thinks we want to do matrix operations unless we specify (with the use of a period) otherwise. First a trivial, but important operation: forming the transpose. This is done in MATLAB with the single quote: '. It essentially reverses rows and columns in a matrix If the matrix is complex in MATLAB it also takes the conjugate of each element. If we want to keep from getting a conjugate, we need to insert a period before the quote. Here are some examples:

>> disp([1 2]') 

     1 
     2 

>> a=[1 2 3 
      4 5 6]; 

>> disp(a') 

     1     4 
     2     5 
     3     6 

>> z=a+i*[-1 0 1;-2 1 2]; 

>> disp(z) 

   1.0000 - 1.0000i   2.0000             3.0000 + 1.0000i 
   4.0000 - 2.0000i   5.0000 + 1.0000i   6.0000 + 2.0000i  

>> disp(z') 

   1.0000 + 1.0000i   4.0000 + 2.0000i 
   2.0000             5.0000 - 1.0000i 
   3.0000 - 1.0000i   6.0000 - 2.0000i 

>> disp(z.') 

   1.0000 - 1.0000i   4.0000 - 2.0000i 
   2.0000             5.0000 + 1.0000i 
   3.0000 + 1.0000i   6.0000 + 2.0000i 


9.1 Matrix Multiply


Matrix multiply finds extensive applications in a number of areas of mathematics and solution of equations. Students are usually introduced to it in the solution of $N$ simultaneous linear equations in $N$ unknowns. If the equations are written:

	a11x1 + a12x2 + ... + a1NxN = b1   
	a21x1 + a22x2 + ... + a2NxN = b2  
	. . . .                    
	aN1x1 + aN2x2 + ... + aNNxN = bN  

we find that they express what we want to solve, but they are cumbersome to write out each time we look at another problem. Looking carefully at this and similar problems, we see that there are three ``entities" that we will deal with.

The set of unknowns: x1, x2, ..., xN. The set of coefficients multiplying the unknowns:

	a11  a12  ...  a1N     
	a21  a22  ...  a2N    
	. . . .                    
	aN1  aN2  ...  aNN    

The set of given elements: b1, b2, ..., bN

It would be a lot easier to call these x, A and b and to write our set of equations as:

Ax = b.

We can do just that if:

1) We write our "vectors" x and b as matrices with a single column,

2) We define matrix multiply of A (with elements: aij) by B (with elements: bjk) to give C (with elements cik) such that: if A is an M row by N column matrix and B is an N row by P column matrix, then AB = means that:

giving C , an M row by P column matrix

3) We understand that two matrices are equal if and only if all elements in like positions are equal.

Two very important points about this operation:

a) Arrays A and B can be multiplied together in the matrix sense only if the number of columns in the first array is equal to the number of rows in the second array.
b) The product: AB is nearly always not equal to BA even when both products are defined.

Many of the error messages that you have seen in these notes (and probably in your practice sessions) when you tried to multiply arrays together occur as a result of the violation of the first of these points. Here is a MATLAB session to illustrate the first point:

>> a=[1 2 3 
      4 5 6]; 

>> a*[1 2] 

??? Error using ==> * 
Inner matrix dimensions must agree. 

>> a*[1 2 3] 

??? Error using ==> * 
Inner matrix dimensions must agree. 

>> disp(a*[1 2]') 

 

??? Error using ==> * 
Inner matrix dimensions must agree. 
 

>> disp(a*[1 2 3]') 

    14 
    32 

The matrix a has three columns, therefore it can multiply only matrices with three rows. Row vectors of any length have only one row. A column vector of exactly three rows is the only one that worked. Here is a typical case that shows how matrix multiply depends on the order in which the matrices are listed:

>> b=[1 2 
      3 4 
      5 6]; 

>> disp(a*b) 

    22    28 
    49    64 

>> disp(b*a) 

     9    12    15 
    19    26    33 
    29    40    51 

Matrix multiply gives a direct way to find the vector product of two vectors of the same length or of a vector with itself. For real vectors with N elements, this is defined as:

It is given in MATLAB by:

>> x=[1 2 3]; 

 

>> disp(x*x')    <-- Note the transpose using a quote

    14 

 

>> y=[-1 0 1]; 
>> disp(x*y')    <-- Note the transpose using a quote

     2 



9.2 Determinants and Matrix Division


MATLAB gives us two matrix division operations. We will consider only the one that solves the set of equations: AX=B for X, given A and B. This is done with:

>> X=A\B 

The simplest case is the one where the number of unknowns equals the number of equations. In that case and if the determinant of A is not 0 (or not so small that the computer can not find one divided by it), then there should be a unique solution to the set of equations. This is frequently represented by:

A-1B

where A-1 is defined as the inverse matrix to A .The matrix product of the inverse of A with B then gives our desired solution.

Let's see what can be done in MATLAB to demonstrate this solution procedure. Suppose we want to solve:

		 3x1 +  x2 - 2x3 = 1 
		  x1 + 2x2 -  x3 = 0 
		-2x1       -  x3 = 5

We set up our matrix a:

>> a=[3 1 -2 
      1 2 -1 
     -2 0 -1]; 

The determinant can be found with det:

>> disp(det(a)) 

   -11 

It is a long way from zero, so we expect a unique solution.

>> b=[1 0 5]'; 

 

>> disp(a\b) 

   -1.1818 
   -0.7273 
   -2.6364 

Now let's check that answer (conveniently stored in ans):

>> disp(a*ans) 

    1.0000 
         0 
    5.0000 

We could also have carried out the solution using the inverse matrix function: inv. Here is how that would have worked:

>> ai=inv(a); 

 

>> disp(ai) 

    0.1818   -0.0909   -0.2727 
   -0.2727    0.6364   -0.0909 
   -0.3636    0.1818   -0.4545 

 

>> disp(ai*b) 

   -1.1818 
   -0.7273 
   -2.6364 

We arrived at the same answer as before. Since matrix inversion takes many more operations than the solution of the same number of equations, we would never follow this route unless we had the same set of equations to solve (with different values for b) many times.


Our main restriction on use of B\A is that the number of rows in each matrix must be the same. In most cases where the number of equations is larger than the number of unknowns; i.e. the number of rows of A is larger than the number of columns, there is no solution that will satisfy all the equations. In that case, we say that the system of equations is over specified. MATLAB then finds the best solution it can by making the sum of the squares of the errors in the equations as small as possible. This is said to give the ``least square error" solution. An example is:

	 4x1 + 2x2 =  3   
	  x1 -  x2 = -1   
	 3x1 + 4x2 =  7

MATLAB gives the following for the two unknowns that come closest to satisfying all three equations:

>> a=[4 2 
      1 -1 
      3 4]; 
>> b=[3 -1 7]'; 
>> disp(a\b) 

    0.0378 
    1.6324 

If we multiply the matrix by that result, we get:

>> disp(a*[.0378;1.6324]) 

    3.4160 
   -1.5946 
    6.6430 

This procedure finds frequent use in generalized curve fitting.

Finally, if there are more unknowns than there are equations (or if the matrix is square but singular) then there are an infinity of solutions. Here, MATLAB gives us one such solution. A singular example is shown next:

>> a=[ 1 2 3 
       4 3 2 
       5 5 5]; 

 >> disp(det(a)) 

     0 

 >> disp(a\[1 2 3]') 

Warning: Matrix is close to singular or badly scaled. 

         Results may be inaccurate. RCOND = 1.110223e-17 

    0.0250 

    0.7500 

   -0.1750 

The answer given works well when we multiply it by a:

>> disp(a*[.025 .75 -.175]') 

     1 

     2 

     3 


Continue on to Chapter 10
Return to Table of Contents