MATLAB Programming/Advanced Topics/Numerical Manipulation/Simple matrix manipulation
Squaring a matrix
a=[1 2;3 4]; a^2;
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.
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
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:
det(m) ans = m00*m11-m01*m10
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
There are lots of ways to solve these equations.
State Space Equations
Often in MATLAB it is necessary to use different types of unique matrices to solve problems.
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
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
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.