next up previous
Next: Inverses, Powers and such Up: Matrix Manipulation Previous: Matrix Manipulation


Simple Matrix Processing

The following code shows how you can: A discussion of this code follows...

% Hints: use ; at end of line to prevent debug'ish output from being printed

% Vector Product -- needs to be col * row or vice versa
u = [3; 1; 4]        % column vector   <-- note: vectors are defined here
v = [2 0 -1]         % Row vector
w = v'               % Transpose of 'v'

x = v*u             % Will return a scalar
X = u*v             % Will return a vector   
% X = u*w;          % Will raise and error


% Matrices
A = pascal(3)
B = magic(3)
C = [
1 2 3 4 
4 5 6 7 
7 8 9 0
]

% Add A and B
X = A + B

% Subtract B from A
Y = A - B

% Multiply A and B (not equal to B * A). Must satisfy dim reqs
Z = A * B

% Multiply by scalar
s = 7;
Z = A*s

% To show how error messages are raised if dims dont match
C = fix(10*rand(3,2))     
% X = A + C

Before going on to processing matrices, I thought I'll show you how Matlab handles vectors (1-D arrays!). You can define a column vector or row vector. In the former case you will need to use a ``;'' in between elements. In both cases you enclose the elements in ``[....]''. A trailing single-quote (`` ' '') creates the transpose of a vector. This is especially useful when you want to multiply two vectors since you can only mutiply a dimensionally corresponding column vector to row vector or vice versa - whether you get back a scalar or a vector will depend on this order.

To define test matrices you can use predefined functions like pascal(int) or magic(int) to create some funky matrix - constructs a magic square kinda matrix so that each row/column adds up to a certain value 4. The `int' parameter is used to specify the dimension. Use two integers if you want non-square matrix.

In reality you will usually have a table of data points which you want to translate into a matrix. It's quite straight forward to do. It's just like defining a few row vectors. Have each row's data comma/space separated and then a return char (newline) demarcates two rows.

Following the definition of matrices A, B and C, I have shown simple operations like Add, Subtract and multiply using the `+', `-' and the `*' operators. It is obvious how easy it is to play around with matrices in Matlab. I have also shown one instance in which the dimensions don't match up for the specific operation in which case Matlab will raise an error (The last operation in which I've tried to add a 3 x 3matrix to a 3 x 2 matrix).

u =

     3
     1
     4

v =
     2     0    -1

w =

     2
     0
    -1

x =
     2

X =
     6     0    -3
     2     0    -1
     8     0    -4

A =

     1     1     1
     1     2     3
     1     3     6

B =

     8     1     6
     3     5     7
     4     9     2

C =
     1     2     3     4
     4     5     6     7
     7     8     9     0

X =
     9     2     7
     4     7    10
     5    12     8

Y =
    -7     0    -5
    -2    -3    -4
    -3    -6     4

Z =
    15    15    15
    26    38    26
    41    70    39

Z =
     7     7     7
     7    14    21
     7    21    42

C =
     4     6
     8     8
     8     6


next up previous
Next: Inverses, Powers and such Up: Matrix Manipulation Previous: Matrix Manipulation
Arvind Gopu 2006-03-24