next up previous contents
Next: Solving matrix equations using Up: Arithmetic operations on matrices Previous: Arithmetic operations on matrices

Standard operations

If A and B are arrays, then Matlab can compute A+B and A-B when these operations are defined. For example, consider the following commands:

>> A = [1 2 3;4 5 6;7 8 9]; 
>> B = [1 1 1;2 2 2;3 3 3];
>> C = [1 2;3 4;5 6];
>> whos
  Name      Size         Bytes  Class

  A         3x3             72  double array
  B         3x3             72  double array
  C         3x2             48  double array

Grand total is 24 elements using 192 bytes

>> A+B
ans =
     2     3     4
     6     7     8
    10    11    12
>> A+C
??? Error using ==> +
Matrix dimensions must agree.
Matrix multiplication is also defined:
>> A*C
ans =
    22    28
    49    64
    76   100
>> C*A
??? Error using ==> *
Inner matrix dimensions must agree.
If A is a square matrix and m is a positive integer, then A^m is the product of m factors of A.

However, no notion of multiplication is defined for multi-dimensional arrays with more than 2 dimensions:

>> C = cat(3,[1 2;3 4],[5 6;7 8])
C(:,:,1) =
     1     2
     3     4
C(:,:,2) =
     5     6
     7     8
>> D = [1;2]
D =
     1
     2
>> whos
  Name      Size         Bytes  Class

  C         2x2x2           64  double array
  D         2x1             16  double array

Grand total is 10 elements using 80 bytes

>> C*D
??? Error using ==> *
No functional support for matrix inputs.
By the same token, the exponentiation operator ^ is only defined for square 2-dimensional arrays (matrices).



Mark S. Gockenbach
Wed Sep 8 10:44:13 EDT 1999