User Tools

Site Tools


notes:tools:matlab

MATLAB

Matrix:

[1 2; 3 4]
ans =
     1     2
     3     4

Row vector addition:

[1 2] + [2 3]
ans = 3 5

Column vector multiplication and addition:

v = [2 3 4]' ; w = [1 1 1]' ; u = 2 * v + 3 * w
u =
     7
     9
    11
>> eye(2)
 
ans =
     1     0
     0     1
>> ones(2,2)
 
ans =
     1     1
     1     1
>> 6*eye(5)-ones(5,5)
 
ans =
     5    -1    -1    -1    -1
    -1     5    -1    -1    -1
    -1    -1     5    -1    -1
    -1    -1    -1     5    -1
    -1    -1    -1    -1     5
>> b = rand(4,1)
 
b =
    0.8147
    0.9058
    0.1270
    0.9134

The dot product is a row vector times a column vector.

Dot product:

[1 2] * [3 4]'
ans = 11

The length of vector:

v = [1 2 2] ; norm(v)
ans = 3

The vector length is the same as sqrt(v*v):

v = [1 2 2] ; sqrt(v * v')
ans = 3

Cosine between two vectors:

v = [1 2] ; w = [3 4]; cosine = v * w' / (norm(v) * norm(w))
cosine = 0.9839

Angle (in radians) between two unit-length vectors:

i = [1 0] ; j = [0 1] ; cosine = i * j' ; angle = acos(cosine)
angle = 1.5708

Special matrices:

>> pascal(4) // 4 x 4 symmetric Pascal matrix
 
ans =
     1     1     1     1
     1     2     3     4
     1     3     6    10
     1     4    10    20
 
>> inv(pascal(4))
 
ans =
    4.0000   -6.0000    4.0000   -1.0000
   -6.0000   14.0000  -11.0000    3.0000
    4.0000  -11.0000   10.0000   -3.0000
   -1.0000    3.0000   -3.0000    1.0000
 
>> L = abs(pascal(4,1)) // Pascal's lower triangular
 
ans =
     1     0     0     0
     1    -1     0     0
     1    -2     1     0
     1    -3     3    -1
 
>> L * L' // == pascal(4)
 
ans =
     1     1     1     1
     1     2     3     4
     1     3     6    10
     1     4    10    20
 
>> inv(L') * inv(L) // == inv(pascal(4))
 
ans =
     4    -6     4    -1
    -6    14   -11     3
     4   -11    10    -3
    -1     3    -3     1
 
>> hilb(6) // the approximation of a Hilbert matrix (fractions are rounded off)
 
ans =
    1.0000    0.5000    0.3333    0.2500    0.2000    0.1667
    0.5000    0.3333    0.2500    0.2000    0.1667    0.1429
    0.3333    0.2500    0.2000    0.1667    0.1429    0.1250
    0.2500    0.2000    0.1667    0.1429    0.1250    0.1111
    0.2000    0.1667    0.1429    0.1250    0.1111    0.1000
    0.1667    0.1429    0.1250    0.1111    0.1000    0.0909
 
>> inv(hilb(6)) // the approximated inverse of the Hilbert matrix
 
ans =
   1.0e+06 *
 
    0.0000   -0.0006    0.0034   -0.0076    0.0076   -0.0028
   -0.0006    0.0147   -0.0882    0.2117   -0.2205    0.0832
    0.0034   -0.0882    0.5645   -1.4112    1.5120   -0.5821
   -0.0076    0.2117   -1.4112    3.6288   -3.9690    1.5523
    0.0076   -0.2205    1.5120   -3.9690    4.4100   -1.7464
   -0.0028    0.0832   -0.5821    1.5523   -1.7464    0.6985
 
>> invhilb(6) // the exact inverse of the Hilbert matrix
 
ans =
        36      -630      3360     -7560      7560     -2772
      -630     14700    -88200    211680   -220500     83160
      3360    -88200    564480  -1411200   1512000   -582120
     -7560    211680  -1411200   3628800  -3969000   1552320
      7560   -220500   1512000  -3969000   4410000  -1746360
     -2772     83160   -582120   1552320  -1746360    698544
notes/tools/matlab.txt · Last modified: 2020/08/26 (external edit)