MATLAB Programming/Simple matrix manipulation

From Wikibooks, the open-content textbooks collection

Jump to: navigation, search

MATLAB Programming

Chapter 1: A Tutorial Introduction

Chapter 2: Basic MATLAB Concepts

Saving and loading a MAT-file
MATLAB's Command Prompt
Basic Reading and Writing Data from a File

Chapter 3: Data Storage and Manipulation

Data Types and Operations on Point Values

Boolean and Rational
Strings
Portable Functions
Complex Numbers

Arrays and Matrices

What is an array?
Introduction to array operations
Vectors and basic vector operations
Struct Arrays
Cell Arrays
Sparse Matrices

Chapter 4: Graphics

Basic Graphics Commands
Annotating Plots

Chapter 5: M File Programming

Scripts
Comments
The Input Function
Control Flow
Loops and Branches
Error Messages
MATLAB Caveats
Debugging M Files

Chapter 6: Mathematical Manipulations

Linear Algebra

Simple matrix manipulation
More complicated matrix operations

Differential Equations

Ordinary Differential Equations
Partial Differential Equations

Chapter 7: More advanced I/O

Writing and Reading to a Serial Port
Writing to a USB Port

Chapter 8: Examples

Filtering
Controls
Phase Vocoder and Encoder

Chapter 9: Object-Oriented Programming

Struct arrays
MATLAB Classes

Chapter 10: An alternative to MATLAB: Octave

What is Octave ?
Octave/MATLAB differences

Chapter 11: Toolboxes

Symbolic Math Toolbox
GUIDE
Simulink
Psych Toolbox

edit this box


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 a 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

Ther 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.