From charlesreid1

Line 119: Line 119:
|<math>
|<math>
\left[  
\left[  
A + \left( \frac{i}{n-1} \right) B
A + \left( \frac{i}{N-1} \right) B
\right] \qquad i=0, \dots, N-1
\right] \qquad i=0, \dots, N-1
</math>
</math>
Line 130: Line 130:
|<math>
|<math>
\left[
\left[
10^{A} + 10^{ \left( \frac{i}{n-1} \right) B }
10^{A} + 10^{ \left( \frac{i}{N-1} \right) B }
\right] \qquad i=0, \dots, N-1
\right] \qquad i=0, \dots, N-1
</math>
</math>

Revision as of 01:33, 27 November 2010

Matrices

Basics

See Introduction to Matlab

Special matrices/vectors

Name (matrix type) Matlab syntax Result
Ones
>> ones(3,2);
$ \left[ \begin{array}{cc} 1 & 1 \\ 1 & 1 \\ 1 & 1 \end{array} \right] $
Zeros
>> zeros(3,1);
$ \left[ \begin{array}{cc} 0 \\ 0 \\ 0 \end{array} \right] $
Eye (identity)
>> eye(3);
$ \left[ \begin{array}{ccc} 1 & 0 & 0 \\ 0 & 1 & 0 \\ 0 & 0 & 1 \end{array} \right] $
Rand (random numbers)
>> rand(3,2);
$ \left[ \begin{array}{cc} 0.21955 & 0.27560\\ 0.42385 & 0.62212\\ 0.53343 & 0.69182 \end{array} \right] $
Meshgrid
>> [x,y] = meshgrid(1:4,1:4);
$ x = \left[ \begin{array}{cccc} 1& 2& 3& 4\\ 1& 2& 3& 4\\ 1& 2& 3& 4\\ 1& 2& 3& 4 \end{array} \right] $

$ y = \left[ \begin{array}{cccc} 1& 1& 1& 1\\ 2& 2& 2& 2\\ 3& 3& 3& 3\\ 4& 4& 4& 4 \end{array} \right] $

Magic (magic square matrix)

(The sum of each row and column is equal to the same value)

>> magic(4);
$ \left[ \begin{array}{cccc} 16& 2& 3& 13\\ 5& 11& 10& 8\\ 9& 7& 6& 12\\ 4& 14& 15& 1 \end{array} \right] $
Linspace
>> linspace(A,B,N)
$ \left[ A + \left( \frac{i}{N-1} \right) B \right] \qquad i=0, \dots, N-1 $
Logspace
>> logspace(A,B,N)
$ \left[ 10^{A} + 10^{ \left( \frac{i}{N-1} \right) B } \right] \qquad i=0, \dots, N-1 $

Functions

Function name Matlab syntax/output
det Returns the determinant of a matrix:
>> A=magic(4)
A =

   16    2    3   13
    5   11   10    8
    9    7    6   12
    4   14   15    1

>> det(A)
ans = -1.4495e-12
find
flipud
fliplr
length
max
min
repmat This function creates a new matrix consisting of several copies of an existing matrix.
>> A = magic(3)
A =

   8   1   6
   3   5   7
   4   9   2

>> repmat(A,2,2)
ans =

   8   1   6   8   1   6
   3   5   7   3   5   7
   4   9   2   4   9   2
   8   1   6   8   1   6
   3   5   7   3   5   7
   4   9   2   4   9   2
size
sort

Operators

Addition, subtraction

Addition/subtraction can be done with vectors or matrices as with numbers:

>> A=ones(2,3)
A =

   1   1   1
   1   1   1

>> B=ones(2,3)
B =

   1   1   1
   1   1   1

>> C = A + B
C =

   2   2   2
   2   2   2

>> C = A - B
C =

   0   0   0
   0   0   0

Multiplication, division

Multiplication of matrices requires that the inner dimensions must match (i.e. $ M \times N) * (N \times P) $):

>> A
A =

   0.85645   0.86793   0.39228
   0.22329   0.82611   0.40042
   0.79097   0.45921   0.30861

>> B
B =

   0.976938   0.200895   0.239939
   0.300156   0.205414   0.963250
   0.396226   0.425022   0.041877

>> C = A*B
C =

   1.25264   0.51707   1.05796
   0.62476   0.38474   0.86609
   1.03284   0.38440   0.64504

Division of matrices is defined as $ A/B = A B^{-1} $:

>> A = rand(3,3)
A =

   0.85645   0.86793   0.39228
   0.22329   0.82611   0.40042
   0.79097   0.45921   0.30861

>> B = rand(3,3)
B =

   0.976938   0.200895   0.239939
   0.300156   0.205414   0.963250
   0.396226   0.425022   0.041877

>> 

>> C = A/B
C =

   0.015664   0.321640   1.879233
  -0.763591   0.516569   2.054946
   0.435077   0.177713   0.788906

>> C = A*inv(B)
C =

   0.015664   0.321640   1.879233
  -0.763591   0.516569   2.054946
   0.435077   0.177713   0.788906

Component-wise multiplication and division can also be done:

>> A
A =

   0.85645   0.86793   0.39228
   0.22329   0.82611   0.40042
   0.79097   0.45921   0.30861

>> B
B =

   0.976938   0.200895   0.239939
   0.300156   0.205414   0.963250
   0.396226   0.425022   0.041877

>> C = A.*B
C =

   0.836694   0.174363   0.094122
   0.067023   0.169693   0.385709
   0.313402   0.195175   0.012924

>> C = A./B
C =

   0.87666   4.32032   1.63489
   0.74392   4.02167   0.41570
   1.99626   1.08044   7.36944

Input/output

Switches

Functions

Graphics

Examples

Fluid mechanics

Heat transfer

Optimization

Statistics

See also