Multivariate Statistical Techniques
Matrix Operations in Octave


The Matrix

octave: A = [2,1;3,2;-2,2]
A =

   2   1
   3   2
  -2   2

octave: A
A =

   2   1
   3   2
  -2   2

Multiplication by a Scalar

octave: c = 3
c = 3

octave: c*A
ans =

   6   3
   9   6
  -6   6

Matrix Addition & Subtraction

octave: B = [1,1;4,2;-2,1]
B =

   1   1
   4   2
  -2   1

octave: C = A + B
C =

   3   2
   7   4
  -4   3

octave: D = A - B
D =

   1   0
  -1   0
   0   1

Matrix Multiplication

octave: D = [2,1,3;-2,2,1]
D =

   2   1   3
  -2   2   1

octave: C = D*A
C =

   1  10
   0   4

octave: C = A*D
C =

    2    4    7
    2    7   11
   -8    2   -4

octave: D = [2,1,3]
D =

  2  1  3

octave: C = D*A
C =

   1  10

octave: C = A*D
error: operator *: nonconformant arguments (op1 is 3x2, op2 is 1x3)
error: evaluating binary operator `*' near line 44, column 6
error: evaluating assignment expression near line 44, column 3

Transpose of a Matrix

octave: AT = A'
AT =

   2   3  -2
   1   2   2

octave: ATT = AT'
ATT =

   2   1
   3   2
  -2   2

Common Vectors

Unit Vector

octave: U = ones(3,1)
U =

  1
  1
  1

Common Matrices

Unit Matrix

Using Stata
octave: U = ones(3,2)
U =

  1  1
  1  1
  1  1

Diagonal Matrix

octave: S = [2,1,4;3,2,2;-2,2,3]
S =

   2   1   4
   3   2   2
  -2   2   3

octave: D = diag(S)
D =

  2
  2
  3

octave: D = diag(diag(S),0)
D =

  2  0  0
  0  2  0
  0  0  3

Identity Matrix

octave: I = eye(3)
I =

  1  0  0
  0  1  0
  0  0  1

Symmetric Matrix

octave: C = [2,1,5;1,3,4;5,4,-2]
C =

   2   1   5
   1   3   4
   5   4  -2

octave: CT = C'
CT =

   2   1   5
   1   3   4
   5   4  -2

Inverse of a Matrix

octave: A = [4,2,2;4,6,8;-2,2,4]
A =

   4   2   2
   4   6   8
  -2   2   4

octave: AI = inv(A)
AI =

   1.00000  -0.50000   0.50000
  -4.00000   2.50000  -3.00000
   2.50000  -1.50000   2.00000

octave: A*AI
ans =

  1  0  0
  0  1  0
  0  0  1

octave: AI*A
ans =

  1  0  0
  0  1  0
  0  0  1

Inverse & Determinant of a Matrix

octave: C = [2,1,6;1,3,4;6,4,-2]
C =

   2   1   6
   1   3   4
   6   4  -2

octave: CI = inv(C)
CI =

   0.215686  -0.254902   0.137255
  -0.254902   0.392157   0.019608
   0.137255   0.019608  -0.049020

octave: d = det(C)
d = -102
c

Number of Rows & Columns

octave: X = [3,2;2,-2;4,6;3,1]
X =

   3   2
   2  -2
   4   6
   3   1

octave: r = rows(A)

r = 3

octave: c = columns(X)

c = 2

Computing Column & Row Sums

octave: A = [2,1;3,2;-2,2]
A =

   2   1
   3   2
  -2   2

octave: c = sum(A)
c =

  3  5

octave: r = sum(A')
r =

  3  5  0

octave:  a = sum(sum(A))

a = 8

Computing Column & Row Means

octave: cm = sum(A)/rows(A)
cm =

  1.0000  1.6667

octave: rm = sum(A')/columns(A)
rm =

  1.50000  2.50000  0.00000

Horizontal Concatenation

octave: A
A =

   2   1
   3   2
  -2   2

octave: B = [1,1;3,4;2,2]
B =

  1  1
  3  4
  2  2

octave: C = [A,B]
C =

   2   1   1   1
   3   2   3   4
  -2   2   2   2

Vertical Concatenation (Appending)

octave: C = [A;B]
C =

   2   1
   3   2
  -2   2
   1   1
   3   4
   2   2


Multivariate Course Page

Phil Ender, 23oct03