MATLAB Programming/Advanced Topics/Numerical Manipulation/Simple matrix manipulation

From Wikibooks, open books for an open world
Jump to navigation Jump to search

Operations[edit | edit source]

Squaring a matrix[edit | edit source]

 a=[1 2;3 4];

a^2 is the equivalent of a*a. To square each element:


The period before the operator tells MATLAB to perform the operation element by element.

Determinant[edit | edit source]

Getting the determinant of a matrix, requires that you first define your matrix, then run the function "det()" on that matrix, as follows:

 a = [1 2; 3 4];
 ans = -2

Symbolic Determinant[edit | edit source]

You can get the symbolic version of the determinant matrix by declaring the values within the matrix as symbolic as follows:

 m00 = sym('m00'); m01 = sym('m01'); m10 = sym('m10'); m11 = sym('m11');


 syms m00 m01 m10 m11;

Then construct your matrix out of the symbolic values:

 m = [m00 m01; m10 m11];

Now ask for the determinant:

 ans = m00*m11-m01*m10

Transpose[edit | edit source]

To find the transpose of a matrix all you do is place an apostrophe after the bracket. Transpose- switch the rows and columns of a matrix.


 a=[1 2 3]
 aTranspose=[1 2 3]'


 b=a' %this will make b the transpose of a

when a is complex, the apostrophe means transpose and conjugate.


a=[1 2i;3i 4];
a'=[1 -3i;-2i 4];

For a pure transpose, use .' instead of apostrophe.

Systems of linear equations[edit | edit source]

There are lots of ways to solve these equations.

Homogeneous Solutions[edit | edit source]

Particular Solutions[edit | edit source]

State Space Equations[edit | edit source]

Special Matrices[edit | edit source]

Often in MATLAB it is necessary to use different types of unique matrices to solve problems.

Identity matrix[edit | edit source]

To create an identity matrix (ones along the diagonal and zeroes elsewhere) use the MATLAB command "eye":

>>a = eye(4,3)
a =
   1   0   0
   0   1   0
   0   0   1
   0   0   0

Ones Matrix[edit | edit source]

To create a matrix of all ones use the MATLAB command "ones"



a =

    1     1     1
    1     1     1
    1     1     1
    1     1     1

Zero matrix[edit | edit source]

The "zeros" function produces an array of zeros of a given size. For example,



a =

    0     0     0
    0     0     0
    0     0     0 
    0     0     0
    0     0     0 

This type of matrix, like the ones matrix, is often useful as a "background", on which to place other values, so that all values in the matrix except for those at certain indices are zero.