MATLAB Programming/Simple matrix manipulation

From Wikibooks, open books for an open world
< MATLAB Programming
Jump to: navigation, search


Operations[edit]

Squaring a matrix[edit]

 a=[1 2;3 4];
 a^2;

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

 a.^2

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

Determinant[edit]

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];
 det(a)
 ans = -2

Symbolic Determinant[edit]

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');

or

 syms m00 m01 m10 m11;

Then construct your matrix out of the symbolic values:

 m = [m00 m01; m10 m11];

Now ask for the determinant:

 det(m)
 ans = m00*m11-m01*m10

Transpose[edit]

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.

Example:

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

or

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

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

Example

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

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

Systems of linear equations[edit]

There are lots of ways to solve these equations.

Homogeneous Solutions[edit]

Particular Solutions[edit]

State Space Equations[edit]

Special Matrices[edit]

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

Identity matrix[edit]

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]

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

a=ones(4,3)

Produces:

a =

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

Zero matrix[edit]

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

a=zeros(5,3)

Produces:

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.