0% found this document useful (0 votes)
72 views

Matrix Cheatsheet Table

The document summarizes a matrix cheatsheet from Sebastian Raschka for common matrix operations in MATLAB/Octave, Python NumPy, R, and Julia. It provides code examples for creating matrices, vectors, random matrices, zero and identity matrices, accessing matrix elements by selecting rows and columns, and extracting rows and columns based on criteria. The matrix cheatsheet is made available under a Creative Commons license.

Uploaded by

Lan Ngo
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
72 views

Matrix Cheatsheet Table

The document summarizes a matrix cheatsheet from Sebastian Raschka for common matrix operations in MATLAB/Octave, Python NumPy, R, and Julia. It provides code examples for creating matrices, vectors, random matrices, zero and identity matrices, accessing matrix elements by selecting rows and columns, and extracting rows and columns based on criteria. The matrix cheatsheet is made available under a Creative Commons license.

Uploaded by

Lan Ngo
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

1/29/2017

cseweb.ucsd.edu/~gary/2014_matrix_cheatsheet_table.html

ThispagecopiedfromSebastianRaschka....Thanks,Sebastian!Seebelowforlicenseinformation.
[backtoarticle]

TheMatrixCheatsheetbySebastianRaschkaislicensedunderaCreativeCommonsAttribution4.0InternationalLicense.

(lastupdated:June18,2014)

http://cseweb.ucsd.edu/~gary/2014_matrix_cheatsheet_table.html

1/9

1/29/2017

cseweb.ucsd.edu/~gary/2014_matrix_cheatsheet_table.html

MATLAB/Octave

Task

PythonNumPy

Julia

Task

CREATINGMATRICES
CreatingMatrices
(here:3x3matrix)

M>A=[123;456;789]

P>A=np.array([[1,2,3],

R>A=

J>A=[123;456;789]
matrix(c(1,2,3,4,5,6,7,8,9),nrow=3,byrow=T) 3x3Array{Int64,2}:

123
456
#equivalentto
789
#A=matrix(1:9,nrow=3,byrow=T)

R>A
[,1][,2][,3]
[1,]123
[2,]456
[3,]789

CreatingMatrices
(here:3x3matrix)

Creatingan1D
columnvector

M>a=[1;2;3]

P>a=

R>a=matrix(c(1,2,3),nrow=3,byrow=T)

J>a=[1;2;3]

Creatingan1D
columnvector

J>b=[123]

Creatingan
1Drowvector

A=
123
456
789

a=
1
2
3

[4,5,6],[7,8,9]])

P>A
array([[1,2,3],
[4,5,6],
[7,8,9]])

np.array([1,2,3]).reshape(1,3)

[,1]
[1,]1
[2,]2
[3,]3

P>b.shape
(1,3)

R>a

3elementArray{Int64,1}:
1
2
3

Creatingan
1Drowvector

M>b=[123]
b=
123

P>b=np.array([1,2,3])

R>b=matrix(c(1,2,3),ncol=3)

P>b

R>b

array([1,2,3])

[,1][,2][,3]
[1,]123

#notethatnumpydoesn'thave
#explicitrowvectors,but1D
#arrays

1x3Array{Int64,2}:
123

#notethatthisisa2Darray.
#vectorsinJuliaarecolumns

P>b.shape
(3,)

Creatinga
randommxn
matrix

M>rand(3,2)

P>np.random.rand(3,2)

R>matrix(runif(3*2),ncol=2)
array([[0.29347865,0.17920462], [,1][,2]
[0.51615758,0.64593471], [1,]0.56751270.7751204
[0.01067605,0.09692771]]) [2,]0.34394120.5261893
[3,]0.22731770.223438

J>rand(3,2)

Creatinga
randommxnmatrix

Creatinga
zeromxnmatrix

M>zeros(3,2)

P>np.zeros((3,2))

R>mat.or.vec(3,2)

J>zeros(3,2)

Creatinga
zeromxnmatrix

P>np.ones((3,2))

R>matrix(1L,3,2)

J>ones(3,2)

Creatingan
mxnmatrixofones

ans=
0.219770.10220
0.389590.69911
0.156240.65637

ans=
00
00
00

Creatingan
M>ones(3,2)
mxnmatrixofones ans=
11
11
11

http://cseweb.ucsd.edu/~gary/2014_matrix_cheatsheet_table.html

array([[0.,0.],
[0.,0.],
[0.,0.]])

array([[1.,1.],
[1.,1.],
[1.,1.]])

[,1][,2]
[1,]00
[2,]00
[3,]00

[,1][,2]
[1,]11

3x2Array{Float64,2}:
0.368820.267725
0.5718560.601524
0.8480840.858935

3x2Array{Float64,2}:
0.00.0
0.00.0
0.00.0

3x2Array{Float64,2}:
1.01.0
1.01.0
1.01.0

2/9

1/29/2017

cseweb.ucsd.edu/~gary/2014_matrix_cheatsheet_table.html
[2,]11
[3,]11

Creatingan
identitymatrix

M>eye(3)

P>np.eye(3)

R>diag(3)

J>eye(3)

Creatingan
identitymatrix

Creatinga
diagonalmatrix

M>a=[123]

P>a=np.array([1,2,3])

R>diag(1:3)

J>a=[1,2,3]

M>diag(a)

P>np.diag(a)

Creatinga
diagonalmatrix

J>A=[123;456]

Gettingthe
dimension
ofamatrix
(here:2D,rowsx
cols)

J>A=[123;456;789];

Selectingrows

ans=
DiagonalMatrix
100
010
001

ans=
DiagonalMatrix
100
020
003

array([[1.,0.,0.],
[0.,1.,0.],
[0.,0.,1.]])

array([[1,0,0],
[0,2,0],
[0,0,3]])

[,1][,2][,3]
[1,]100
[2,]010
[3,]001

[,1][,2][,3]
[1,]100
[2,]020
[3,]003

3x3Array{Float64,2}:
1.00.00.0
0.01.00.0
0.00.01.0

#addedcommasbecausejulia
#vectorsarecolumnar

J>diagm(a)
3x3Array{Int64,2}:
100
020
003

ACCESSINGMATRIXELEMENTS

Gettingthe
dimension
ofamatrix
(here:2D,rowsx
cols)

M>A=[123;456]
A=
123
456

M>size(A)
ans=
23

Selectingrows

M>A=[123;456;789]
%1strow
M>A(1,:)
ans=
123
%1st2rows
M>A(1:2,:)
ans=
123
456

Selectingcolumns

http://cseweb.ucsd.edu/~gary/2014_matrix_cheatsheet_table.html

P>A=np.array([[1,2,3],[4,5,6] R>A=matrix(1:6,nrow=2,byrow=T)
])

P>A

[,1][,2][,3]
[1,]123
[2,]456

array([[1,2,3],
[4,5,6]])

P>A.shape
(2,3)

P>A=np.array([[1,2,3],

[4,5,6],[7,8,9]])

#1strow
P>A[0,:]
array([1,2,3])

#1st2rows
P>A[0:2,:]
array([[1,2,3],[4,5,6]])

R>A

2x3Array{Int64,2}:
123
456

J>size(A)
(2,3)

R>dim(A)
[1]23

R>A=matrix(1:9,nrow=3,byrow=T)

#1strow
R>A[1,]
[1]123

#1st2rows

R>A[1:2,]

[,1][,2][,3]
[1,]123
[2,]456

#semicolonsuppressesoutput

#1strow
J>A[1,:]
1x3Array{Int64,2}:
123

#1st2rows
J>A[1:2,:]
2x3Array{Int64,2}:
123
456

Selectingcolumns

3/9

1/29/2017

cseweb.ucsd.edu/~gary/2014_matrix_cheatsheet_table.html
M>A=[123;456;789]
%1stcolumn
M>A(:,1)
ans=
1
4
7
%1st2columns
M>A(:,1:2)
ans=
12
45
78

Extractingrowsand M>A=[123;459;789]
columnsbycriteria A=
123

(here:getrowsthat 459
789
havevalue9in
column3)
M>A(A(:,3)==9,:)
ans=
459
789

P>A=np.array([[1,2,3],

[4,5,6],[7,8,9]])

#1stcolumn(asrowvector)
P>A[:,0]
array([1,4,7])

#1stcolumn(ascolumnvector)
P>A[:,[0]]
array([[1],
[4],
[7]])

#1st2columns
P>A[:,0:2]
array([[1,2],
[4,5],
[7,8]])

P>A=np.array([[1,2,3],
[4,5,9],[7,8,9]])

P>A
array([[1,2,3],
[4,5,9],
[7,8,9]])

P>A[A[:,2]==9]
array([[4,5,9],
[7,8,9]])

R>A=matrix(1:9,nrow=3,byrow=T)

J>A=[123;456;789];

R>A[,1:2]

#1stcolumn
J>A[:,1]
3elementArray{Int64,1}:
1
4
7

#1st2columns
J>A[:,1:2]
3x2Array{Int64,2}:
12
45
78

R>A=matrix(1:9,nrow=3,byrow=T)

J>A=[123;459;789]

#1stcolumnasrowvector

R>t(A[,1])

[,1][,2][,3]
[1,]147

#1stcolumnascolumnvector

R>A[,1]

[1]147

#1st2columns
[,1][,2]
[1,]12
[2,]45
[3,]78

R>A

[,1][,2][,3]
[1,]123
[2,]459
[3,]789

R>A[A[,3]==9,]
[1]789

3x3Array{Int64,2}:
123
459
789

#use'.=='for
#elementwisecheck
J>A[A[:,3].==9,:]
2x3Array{Int64,2}:
459
789

Extractingrowsand
columnsbycriteria

(here:getrowsthat
havevalue9in
column3)

Accessingelements M>A=[123;456;789]
(here:1stelement)
M>A(1,1)
ans=1

P>A=np.array([[1,2,3],
[4,5,6],[7,8,9]])

P>A[0,0]
1

R>A=

J>A=[123;456;789];
matrix(c(1,2,3,4,5,9,7,8,9),nrow=3,byrow=T)
J>A[1,1]

1
R>A[1,1]
[1]1

Accessingelements
(here:1stelement)

MANIPULATINGSHAPEANDDIMENSIONS

Converting
amatrixintoarow
vector(bycolumn)

M>A=[123;456;789]
M>A(:)
ans=
1
4
7
2
5
8
3

http://cseweb.ucsd.edu/~gary/2014_matrix_cheatsheet_table.html

P>A=np.array([[1,2,3],[4,5,6],
[7,8,9]])

P>A.flatten(1)
array([1,4,7,2,5,8,3,6,9])

R>A=matrix(1:9,nrow=3,byrow=T)

J>A=[123;456;789]

J>vec(A)

R>as.vector(A)

9elementArray{Int64,1}:

1
4
7
2
5
8
3

[1]147258369

Converting
amatrixintoarow
vector(bycolumn)

4/9

1/29/2017
Converting
rowtocolumn
vectors

cseweb.ucsd.edu/~gary/2014_matrix_cheatsheet_table.html
6
9

M>b=[123]

P>b=np.array([1,2,3])

R>b=matrix(c(1,2,3),ncol=3)

M>b=b'

P>b=b[np.newaxis].T

R>t(b)

P>A=np.array([[1,2,3],[4,5,6],

R>A=matrix(1:9,nrow=3,byrow=T)

b=
1
2
3

ReshapingMatrices M>A=[123;456;789]
(here:3x3matrixto
rowvector)

A=
123
456
789

M>total_elements=numel(A)
M>B=reshape(A,1,total_elements)

%orreshape(A,1,9)
B=
147258369

#alternatively
#b=b[:,np.newaxis]

P>b
array([[1],
[2],
[3]])

[7,8,9]])

P>A
array([[1,2,3],
[4,5,9],
[7,8,9]])

P>total_elements=
np.prod(A.shape)

P>B=A.reshape(1,

total_elements)

#alternativeshortcut:
#A.reshape(1,1)

P>B
array([[1,2,3,4,5,6,7,8,
9]])

Concatenating
matrices

M>A=[123;456]
M>B=[789;101112]
M>C=[A;B]

123
456
789
101112

Stacking
M>a=[123]
vectorsandmatrices
M>b=[456]
M>c=[a'b']
c=
14
25
36

M>c=[a;b]
c=
123
456

http://cseweb.ucsd.edu/~gary/2014_matrix_cheatsheet_table.html

[,1]
[1,]1
[2,]2
[3,]3

R>A

[,1][,2][,3]
[1,]123
[2,]456
[3,]789

R>total_elements=dim(A)[1]*dim(A)[2]

R>B=matrix(A,ncol=total_elements)

R>B
[,1][,2][,3][,4][,5][,6][,7][,8]
[,9]
[1,]147258369

P>A=np.array([[1,2,3],[4,5, R>A=matrix(1:6,nrow=2,byrow=T)

6
9

J>b=vec([123])

Converting
rowtocolumn
vectors

J>A=[123;456;789]

ReshapingMatrices

(here:3x3matrixto
rowvector)

J>A=[123;456];

Concatenating
matrices

3elementArray{Int64,1}:
1
2
3

3x3Array{Int64,2}:
123
456
789

J>total_elements=length(A)
9

J>B=reshape(A,1,total_elements)
1x9Array{Int64,2}:
147258369

6]])

P>B=np.array([[7,8,9],
[10,11,12]])

P>C=np.concatenate((A,B),
axis=0)

P>C
array([[1,2,3],
[4,5,6],
[7,8,9],
[10,11,12]])

P>a=np.array([1,2,3])
P>b=np.array([4,5,6])

R>a=matrix(1:3,ncol=3)

J>a=[123];

P>np.c_[a,b]

R>b=matrix(4:6,ncol=3)

J>b=[456];

R>matrix(rbind(A,B),ncol=2)

J>c=[a'b']

array([[1,4],
[2,5],
[3,6]])

P>np.r_[a,b]
array([[1,2,3],
[4,5,6]])

R>B=matrix(7:12,nrow=2,byrow=T)

J>B=[789;101112];

R>C=rbind(A,B)

J>C=[A;B]

R>C

[,1][,2][,3]
[1,]123
[2,]456
[3,]789
[4,]101112

[,1][,2]
[1,]15
[2,]43

R>rbind(A,B)
[,1][,2][,3]
[1,]123
[2,]456

4x3Array{Int64,2}:
123
456
789
101112

Stacking
vectorsandmatrices

3x2Array{Int64,2}:
14
25
36

J>c=[a;b]
2x3Array{Int64,2}:
123
456

5/9

1/29/2017

cseweb.ucsd.edu/~gary/2014_matrix_cheatsheet_table.html

BASICMATRIXOPERATIONS

Matrixscalar
operations

M>A=[123;456;789]
M>A*2

ans=
246
81012
141618

M>A+2
M>A2
M>A/2

P>A=np.array([[1,2,3],
[4,5,6],[7,8,9]])

P>A*2
array([[2,4,6],
[8,10,12],
[14,16,18]])

P>A+2

P>A2

P>A/2

R>A=matrix(1:9,nrow=3,byrow=T)

J>A=[123;456;789];

Matrixscalar
operations

R>A=matrix(1:9,nrow=3,byrow=T)

J>A=[123;456;789];

R>A%*%A

J>A*A

Matrixmatrix
multiplication

R>A=matrix(1:9,ncol=3)

J>A=[123;456;789];

R>b=matrix(1:3,nrow=3)

J>b=[1;2;3];

R>t(b%*%A)
[,1]
[1,]14
[2,]32
[3,]50

J>A*b

R>A=matrix(1:9,nrow=3,byrow=T)

J>A=[123;456;789];

R>A*2

[,1][,2][,3]
[1,]246
[2,]81012
[3,]141618

R>A+2

R>A2

R>A/2

#NotethatNumPywasoptimizedfor
#inplaceassignments
#e.g.,A+=Ainsteadof
#A=A+A

Matrixmatrix
multiplication

Matrixvector
multiplication

M>A=[123;456;789]
ans=
303642
668196
102126150

[4,5,6],[7,8,9]])

P>np.dot(A,A)#orA.dot(A)
array([[30,36,42],
[66,81,96],
[102,126,150]])

M>A=[123;456;789]

P>A=np.array([[1,2,3],

M>A*A

M>A*b

[4,5,6],[7,8,9]])

P>b=np.array([[1],[2],[3]])

P>np.dot(A,b)#orA.dot(b)

array([[14],[32],[50]])

M>A=[123;456;789]

P>A=np.array([[1,2,3],

M>b=[1;2;3]
ans=
14
32
50

Elementwise
matrix
matrixoperations

P>A=np.array([[1,2,3],

M>A.*A

ans=
149
162536
496481

M>A.+A
M>A.A
M>A./A

http://cseweb.ucsd.edu/~gary/2014_matrix_cheatsheet_table.html

[4,5,6],[7,8,9]])

P>A*A
array([[1,4,9],
[16,25,36],
[49,64,81]])

P>A+A

P>AA

P>A/A

[,1][,2][,3]
[1,]303642
[2,]668196
[3,]102126150

R>A*A

[,1][,2][,3]
[1,]149
[2,]162536
[3,]496481

R>A+A

R>AA

R>A/A

#elementwiseoperator

J>A.*2
3x3Array{Int64,2}:
246
81012
141618

J>A.+2;

J>A.2;

J>A./2;

3x3Array{Int64,2}:
303642
668196
102126150

Matrixvector
multiplication

3elementArray{Int64,1}:
14
32
50

J>A.*A

3x3Array{Int64,2}:
149
162536
496481

J>A.+A;

J>A.A;

J>A./A;

Elementwise
matrix
matrixoperations

6/9

1/29/2017

cseweb.ucsd.edu/~gary/2014_matrix_cheatsheet_table.html

Matrixelementsto
powern

M>A=[123;456;789]

(here:individual
elementssquared)

ans=
149
162536
496481

#NotethatNumPywasoptimizedfor
#inplaceassignments
#e.g.,A+=Ainsteadof
#A=A+A
P>A=np.array([[1,2,3],
[4,5,6],[7,8,9]])

P>np.power(A,2)
array([[1,4,9],
[16,25,36],
[49,64,81]])

Matrixtopowern

M>A=[123;456;789]

P>A=np.array([[1,2,3],

M>A.^2

668196
102126150

[4,5,6],[7,8,9]])

P>np.linalg.matrix_power(A,2)
array([[30,36,42],
[66,81,96],
[102,126,150]])

M>A=[123;456;789]

P>A=np.array([[1,2,3],

M>A^2
(here:matrix
matrixmultiplication ans=
303642
withitself)

Matrixtranspose

R>A^2

J>A.^2

R>A=matrix(1:9,ncol=3)

J>A=[123;456;789];

[,1][,2][,3]
[1,]149
[2,]162536
[3,]496481

#requirestheexpmpackage

R>install.packages('expm')

R>library(expm)

R>A%^%2
[,1][,2][,3]
[1,]3066102
[2,]3681126
[3,]4296150

R>A=matrix(1:9,nrow=3,byrow=T)

ans=
147
258
369

M>A=[611;425;287]

P>A=np.array([[6,1,1],[4,2,5], R>A=matrix(c(6,1,1,4,2,5,2,8,7),

A=
611
425
287

M>det(A)

ans=306

Inverseofamatrix

J>A=[123;456;789];

[4,5,6],[7,8,9]])

P>A.T
array([[1,4,7],
[2,5,8],
[3,6,9]])

M>A'

Determinantofa
matrix:
A>|A|

R>A=matrix(1:9,nrow=3,byrow=T)

M>A=[47;26]
A=
47
26

M>A_inv=inv(A)

A_inv=
0.600000.70000
0.200000.40000

http://cseweb.ucsd.edu/~gary/2014_matrix_cheatsheet_table.html

R>t(A)

[,1][,2][,3]
[1,]147
[2,]258
[3,]369

3x3Array{Int64,2}:
149
162536
496481

J>A^2

Matrixtopowern

(here:matrix
matrixmultiplication
withitself)

J>A=[123;456;789]

Matrixtranspose

J>A=[611;425;287]

Determinantofa
matrix:
A>|A|

3x3Array{Int64,2}:
303642
668196
102126150

3x3Array{Int64,2}:
123
456
789

J>A'
3x3Array{Int64,2}:
147
258
369

[2,8,7]])

P>A
array([[6,1,1],
[4,2,5],
[2,8,7]])

P>np.linalg.det(A)
306.0

nrow=3,byrow=T)

R>A
[,1][,2][,3]
[1,]611
[2,]425
[3,]287

R>det(A)
[1]306

P>A=np.array([[4,7],[2,6]])

R>A=matrix(c(4,7,2,6),nrow=2,byrow=T) J>A=[47;26]

P>A

R>A

array([[4,7],
[2,6]])

P>A_inverse=np.linalg.inv(A)

P>A_inverse
array([[0.6,0.7],
[0.2,0.4]])

[,1][,2]
[1,]47
[2,]26

R>solve(A)
[,1][,2]
[1,]0.60.7
[2,]0.20.4

Matrixelementsto
powern

(here:individual
elementssquared)

3x3Array{Int64,2}:
611
425
287

J>det(A)
306.0

Inverseofamatrix

2x2Array{Int64,2}:
47
26

J>A_inv=inv(A)
2x2Array{Float64,2}:
0.60.7
0.20.4

7/9

1/29/2017

cseweb.ucsd.edu/~gary/2014_matrix_cheatsheet_table.html

ADVANCEDMATRIXOPERATIONS

Calculatingthe
covariancematrix
of3random
variables

M>x1=[4.00004.20003.90004.3000
4.1000]

M>x2=[2.00002.10002.00002.1000
2.2000]'

(here:covariancesof
M>x3=[0.600000.590000.580000.62000
themeans
0.63000]
ofx1,x2,andx3)
M>cov([x1,x2,x3])
ans=
2.5000e027.5000e031.7500e03
7.5000e037.0000e031.3500e03
1.7500e031.3500e034.3000e04

Calculating
eigenvectors
andeigenvalues

M>A=[31;13]
A=
31
13

M>[eig_vec,eig_val]=eig(A)
eig_vec=
0.707110.70711
0.707110.70711
eig_val=
DiagonalMatrix
20
04

Generatinga
Gaussiandataset:
creatingrandom
vectorsfromthe
multivariatenormal

%requiresstatisticstoolboxpackage
%howtoinstallandloaditinOctave:

P>x1=np.array([4,4.2,3.9,

4.3,4.1])

P>x2=np.array([2,2.1,2,2.1,
2.2])

P>x3=np.array([0.6,0.59,
0.58,0.62,0.63])

P>np.cov([x1,x2,x3])
Array([[0.025,0.0075,
0.00175],
[0.0075,0.007,
0.00135],
[0.00175,0.00135,
0.00043]])

R>x1=matrix(c(4,4.2,3.9,4.3,4.1),
ncol=5)

R>x2=matrix(c(2,2.1,2,2.1,2.2),
ncol=5)

R>x3=matrix(c(0.6,0.59,0.58,0.62,
0.63),ncol=5)

R>cov(matrix(c(x1,x2,x3),ncol=3))
[,1][,2][,3]
[1,]0.025000.007500.00175
[2,]0.007500.007000.00135
[3,]0.001750.001350.00043

P>A=np.array([[3,1],[1,3]])

R>A=matrix(c(3,1,1,3),ncol=2)

P>A

R>A

array([[3,1],
[1,3]])

P>eig_val,eig_vec=
np.linalg.eig(A)

P>eig_val
array([4.,2.])

P>eig_vec
Array([[0.70710678,0.70710678],
[0.70710678,0.70710678]])

P>mean=np.array([0,0])

[,1][,2]
[1,]31
[2,]13

R>eigen(A)
$values
[1]42

$vectors
[,1][,2]
[1,]0.70710680.7071068
[2,]0.70710680.7071068

#requiresthemasspackage

R>install.packages('MASS')
P>cov=np.array([[2,0],[0,2]])
%downloadthepackagefrom:

%
P>
R>library(MASS)
http://octave.sourceforge.net/packages.php
np.random.multivariate_normal(mean,
%pkginstall

http://cseweb.ucsd.edu/~gary/2014_matrix_cheatsheet_table.html

J>x1=[4.04.23.94.34.1]';

J>x2=[2.2.12.2.12.2]';

J>x3=[0.6.59.58.62.63]';

J>cov([x1x2x3])

3x3Array{Float64,2}:
0.0250.00750.00175
0.00750.0070.00135
0.001750.001350.00043

J>A=[31;13]

2x2Array{Int64,2}:
31
13

J>(eig_vec,eig_val)=eig(a)
([2.0,4.0],
2x2Array{Float64,2}:
0.7071070.707107
0.7071070.707107)

Calculatingthe
covariancematrix
of3random
variables

(here:covariancesof
themeans
ofx1,x2,andx3)

Calculating
eigenvectors
andeigenvalues

#requirestheDistributionspackagefrom
Generatinga
https://github.com/JuliaStats/Distributions.jl Gaussiandataset:

J>usingDistributions
creatingrandom

vectorsfromthe
J>mean=[0.,0.]

multivariatenormal

8/9

1/29/2017
distributiongiven
meanand
covariancematrix

cseweb.ucsd.edu/~gary/2014_matrix_cheatsheet_table.html
%~/Desktop/io2.0.2.tar.gz
%pkginstall
%~/Desktop/statistics1.2.3.tar.gz

M>pkgloadstatistics
(here:5random
vectorswith
M>mean=[00]
mean0,covariance
=0,variance=2)
M>cov=[20;02]
cov=
20
02

cov,5)

Array([[1.55432624,1.17972629],
[2.01185294,1.96081908],
[2.11810813,1.45784216],
[2.93207591,0.07369322],
[1.37031244,1.18408792]])

R>mvrnorm(n=10,mean,cov)
[,1][,2]
[1,]0.84078300.1882706
[2,]0.84968220.7889329
[3,]0.15641710.8422177
[4,]0.62887791.0618688
[5,]0.51038790.1303697
[6,]0.84131890.1623758
[7,]1.04954660.4161082
[8,]1.32363390.7755572
[9,]0.27710131.4900494
[10,]1.35362680.2338913

2elementArray{Float64,1}:
0.0
0.0

J>cov=[2.0.;0.2.]
2x2Array{Float64,2}:
2.00.0
0.02.0

J>rand(MvNormal(mean,cov),5)
2x5Array{Float64,2}:
0.5276340.3707250.7619283.917471.47516
0.4488212.219042.245610.6920630.390495

distributiongiven
meanand
covariancematrix

(here:5random
vectorswith
mean0,covariance
=0,variance=2)

M>mvnrnd(mean,cov,5)

2.4801500.559906
2.9330470.560212
0.0982063.055316
0.9852150.990936
1.1225280.686977

http://cseweb.ucsd.edu/~gary/2014_matrix_cheatsheet_table.html

9/9

You might also like