next up previous
Next: Data Analysis Up: Matrix Manipulation Previous: Simple Matrix Processing


Inverses, Powers and such

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

% Matrices
A = pascal(3)
B = magic(3)

% Identity Matrices
eye(3,4);           % Returns a 3 x 4 identity matrix

% Determinant -- WARNING: Round off error 
d = det(A)

% Inverse of Matrix 
X = inv(A)         
d = det(A)         % A is symmetric and has integer values -- same det


X = A^2        % Computes matrix squared
X = A.^2       % Squares each element

Y = B^(-3)

sqrt(A)        % Element-based sqrt

sqrtm(A)       % Matrix-based sqrt

We start with the usual test-matrix definitions. Then I have shown how you can create an identity matrix using the eye(int, int) function. This is pretty handy when you want to do scalar-vector or scalar-matrix conversions.

Then I have shown the use of det(matrix) function to compute the determinant and the use of inv(matrix) function to compute its inverse. Now that's cool, I think because usually to compute matrix inverses you end up doing a lot of processing.

Following that I have shown the use of the `' operator to compute powers of a matrix. Note that `2' (for example) is used to compute the squared-matrix whereas `.2' is used to calculate the sqaure of each individual element. Negative powers can also be computed as shown.

Another useful function is the squareroot-computing function. Again it has two variants depending on whether we want to handle individual matrix elements or the matrix as a whole.

A =
     1     1     1
     1     2     3
     1     3     6

B =
     8     1     6
     3     5     7
     4     9     2

d =
     1

X =
     3    -3     1
    -3     5    -2
     1    -2     1

d =
     1

X =
     3     6    10
     6    14    25
    10    25    46

X =
     1     1     1
     1     4     9
     1     9    36

Y =
    0.0053   -0.0068    0.0018
   -0.0034    0.0001    0.0036
   -0.0016    0.0070   -0.0051

ans =
    1.0000    1.0000    1.0000
    1.0000    1.4142    1.7321
    1.0000    1.7321    2.4495

ans =

    0.8775    0.4387    0.1937
    0.4387    1.0099    0.8874
    0.1937    0.8874    2.2749


next up previous
Next: Data Analysis Up: Matrix Manipulation Previous: Simple Matrix Processing
Arvind Gopu 2006-03-24