MATLAB Programming/Simple matrix manipulation

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


Contents

[edit] Operations

[edit] Squaring a matrix

 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.

[edit] Determinant

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

[edit] Symbolic Determinant

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

[edit] Transpose

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.

[edit] Systems of linear equations

There are lots of ways to solve these equations.

[edit] Homogeneous Solutions

[edit] Particular Solutions

[edit] State Space Equations

[edit] Special Matrices

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

[edit] Identity matrix

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

[edit] Ones Matrix

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

[edit] Zero matrix

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

a=zeros(4,3)

Produces:

a =

    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.

Personal tools
Namespaces
Variants
Actions
Navigation
Community
Toolbox
Sister projects
Print/export