next up previous contents
Next: 4.2 Dot, Cross, Norm, Up: 4. Linear algebra Previous: 4. Linear algebra   Contents

4.1 Basic operations

When matrices and vectors are involved, the multiplication operator implies matrix multiplication rather than scalar multiplication. For matrix-matrix or matrix-vector multiplication to work, as you learned in your math class, the number of columns in the first must be the same as the number of rows in the second. That is, when multiplying a $m\times n$ matrix by a $p\times q$ matrix, $n$ and $p$ must match, and the resulting matrix will be $m\times q$. Here's an example.
% Basic arithmetic for matrices and vectors
clear all; clc;

C = [3 1 1  % C is a 3x3 matrix
    0 2 4
    -1 0 1]
D = [2 3 4]  % D is a 1x3 matrix
E = D' % Note the use of transpose. E is a 3x1 array

C*D % doesn't work! For matrix multiplication of m x n matrix
    % with p x q, n and q must be the same (i.e., the "inner
    % dimensions" must agree

D*C % This works
C*E % So does this
The multiplication C*D doesn't work because C has 3 columns but D has only one row (it's a 3-column row vector). MATLAB lets us know what is wrong by saying that the inner dimensions of the arrays must agree. Whenever you get this error, you should realize that MATLAB is attempting matrix multiplication rather than scalar multiplication. The exponentiation operator ^also has a special meaning for matrices. To get $CCC$, try C^3. What is the difference between this and C.^3?
next up previous contents
Next: 4.2 Dot, Cross, Norm, Up: 4. Linear algebra Previous: 4. Linear algebra   Contents
Gus Hart 2005-01-28