[back to article] | |||||
The Matrix Cheatsheet by Sebastian Raschka is licensed under a Creative Commons Attribution 4.0 International License. | |||||
(last updated: June 22, 2018) | |||||
Task | MATLAB/Octave | Python NumPy | R | Julia | Task |
CREATING MATRICES | |||||
Creating Matrices | M> A = [1 2 3; 4 5 6; 7 8 9] | P> A = np.array([ [1,2,3], [4,5,6], [7,8,9] ]) | R> A = matrix(c(1,2,3,4,5,6,7,8,9),nrow=3,byrow=T) | J> A=[1 2 3; 4 5 6; 7 8 9] | Creating Matrices |
(here: 3x3 matrix) | A = | 3x3 Array{Int64,2}: | (here: 3x3 matrix) | ||
1 2 3 | P> A | # equivalent to | 1 2 3 | ||
4 5 6 | array([[1, 2, 3], | # A = matrix(1:9,nrow=3,byrow=T) | 4 5 6 | ||
7 8 9 | [4, 5, 6], | 7 8 9 | |||
[7, 8, 9]]) | R> A | ||||
[,1] [,2] [,3] | |||||
[1,] 1 2 3 | |||||
[2,] 4 5 6 | |||||
[3,] 7 8 9 | |||||
Creating an column vector (nx1 matrix) |
M> a = [1; 2; 3] | P> a = np.array([1,2,3]).reshape(3,1) | R> a = matrix(c(1,2,3), nrow=3, byrow=T) | J> a=[1; 2; 3] | Creating a column vector (nx1 matrix) |
a = | 3-element Array{Int64,1}: | ||||
1 | R> a | 1 | |||
2 | P> b.shape | [,1] | 2 | ||
3 | (3, 1) | [1,] 1 | 3 | ||
[2,] 2 | |||||
[3,] 3 | |||||
Creating an | M> b = [1 2 3] | P> b = np.array([1,2,3]).reshape(1, 3) | R> b = matrix(c(1,2,3), ncol=3) | J> b=[1 2 3] | Creating an |
row vector (1xn matrix) | b = | 1x3 Array{Int64,2}: | row vector (1xn matrix) | ||
1 2 3 | P> b | R> b | 1 2 3 | ||
array([[1], [2], [3]]) |
[,1] [,2] [,3] | ||||
[1,] 1 2 3 | # note that this is a 2D array. | ||||
# note that
in numpy, 1D arrays # can be multiplied # with 2d arrays, too |
|||||
P> b.shape | |||||
(1, 3) | |||||
Creating a | M> rand(3,2) | P> np.random.rand(3,2) | R> matrix(runif(3*2), ncol=2) | J> rand(3,2) | Creating a |
random m x n matrix | ans = | array([[ 0.29347865, 0.17920462], | [,1] [,2] | 3x2 Array{Float64,2}: | random m x n matrix |
0.21977 0.10220 | [ 0.51615758, 0.64593471], | [1,] 0.5675127 0.7751204 | 0.36882 0.267725 | ||
0.38959 0.69911 | [ 0.01067605, 0.09692771]]) | [2,] 0.3439412 0.5261893 | 0.571856 0.601524 | ||
0.15624 0.65637 | [3,] 0.2273177 0.223438 | 0.848084 0.858935 | |||
Creating a | M> zeros(3,2) | P> np.zeros((3,2)) | R> mat.or.vec(3, 2) | J> zeros(3,2) | Creating a |
zero m x n matrix | ans = | array([[ 0., 0.], | [,1] [,2] | 3x2 Array{Float64,2}: | zero m x n matrix |
0 0 | [ 0., 0.], | [1,] 0 0 | 0.0 0.0 | ||
0 0 | [ 0., 0.]]) | [2,] 0 0 | 0.0 0.0 | ||
0 0 | [3,] 0 0 | 0.0 0.0 | |||
Creating an | M> ones(3,2) | P> np.ones((3,2)) | R> matrix(1L, 3, 2) | J> ones(3,2) | Creating an |
m x n matrix of ones | ans = | array([[ 1., 1.], | 3x2 Array{Float64,2}: | m x n matrix of ones | |
1 1 | [ 1., 1.], | [,1] [,2] | 1.0 1.0 | ||
1 1 | [ 1., 1.]]) | [1,] 1 1 | 1.0 1.0 | ||
1 1 | [2,] 1 1 | 1.0 1.0 | |||
[3,] 1 1 | |||||
Creating an | M> eye(3) | P> np.eye(3) | R> diag(3) | J> eye(3) | Creating an |
identity matrix | ans = | array([[ 1., 0., 0.], | [,1] [,2] [,3] | 3x3 Array{Float64,2}: | identity matrix |
Diagonal Matrix | [ 0., 1., 0.], | [1,] 1 0 0 | 1.0 0.0 0.0 | ||
1 0 0 | [ 0., 0., 1.]]) | [2,] 0 1 0 | 0.0 1.0 0.0 | ||
0 1 0 | [3,] 0 0 1 | 0.0 0.0 1.0 | |||
0 0 1 | |||||
Creating a | M> a = [1 2 3] | P> a = np.array([1,2,3]) | R> diag(1:3) | J> a=[1, 2, 3] | Creating a |
diagonal matrix | [,1] [,2] [,3] | diagonal matrix | |||
M> diag(a) | P> np.diag(a) | [1,] 1 0 0 | # added commas because julia | ||
ans = | array([[1, 0, 0], | [2,] 0 2 0 | # vectors are columnar | ||
Diagonal Matrix | [0, 2, 0], | [3,] 0 0 3 | |||
1 0 0 | [0, 0, 3]]) | J> diagm(a) | |||
0 2 0 | 3x3 Array{Int64,2}: | ||||
0 0 3 | 1 0 0 | ||||
0 2 0 | |||||
0 0 3 | |||||
ACCESSING MATRIX ELEMENTS | |||||
Getting the dimension | M> A = [1 2 3; 4 5 6] | P> A = np.array([ [1,2,3], [4,5,6] ]) | R> A = matrix(1:6,nrow=2,byrow=T) | J> A=[1 2 3; 4 5 6] | Getting the dimension |
of a matrix | A = | 2x3 Array{Int64,2}: | of a matrix | ||
(here: 2D, rows x cols) | 1 2 3 | P> A | R> A | 1 2 3 | (here: 2D, rows x cols) |
4 5 6 | array([[1, 2, 3], | [,1] [,2] [,3] | 4 5 6 | ||
[4, 5, 6]]) | [1,] 1 2 3 | ||||
M> size(A) | [2,] 4 5 6 | J> size(A) | |||
ans = | P> A.shape | (2,3) | |||
2 3 | (2, 3) | R> dim(A) | |||
[1] 2 3 | |||||
Selecting rows | M> A = [1 2 3; 4 5 6; 7 8 9] | P> A = np.array([ [1,2,3], [4,5,6], [7,8,9] ]) | R> A = matrix(1:9,nrow=3,byrow=T) | J> A=[1 2 3; 4 5 6; 7 8 9]; | Selecting rows |
#semicolon suppresses output | |||||
% 1st row | # 1st row | # 1st row | |||
M> A(1,:) | P> A[0,:] | R> A[1,] | #1st row | ||
ans = | array([1, 2, 3]) | [1] 1 2 3 | J> A[1,:] | ||
1 2 3 | 1x3 Array{Int64,2}: | ||||
# 1st 2 rows | # 1st 2 rows | 1 2 3 | |||
% 1st 2 rows | P> A[0:2,:] | R> A[1:2,] | |||
M> A(1:2,:) | array([[1, 2, 3], [4, 5, 6]]) | [,1] [,2] [,3] | #1st 2 rows | ||
ans = | [1,] 1 2 3 | J> A[1:2,:] | |||
1 2 3 | [2,] 4 5 6 | 2x3 Array{Int64,2}: | |||
4 5 6 | 1 2 3 | ||||
4 5 6 | |||||
Selecting columns | M> A = [1 2 3; 4 5 6; 7 8 9] | P> A = np.array([ [1,2,3], [4,5,6], [7,8,9] ]) | R> A = matrix(1:9,nrow=3,byrow=T) | J> A=[1 2 3; 4 5 6; 7 8 9]; | Selecting columns |
% 1st column | # 1st column (as row vector) | # 1st column as row vector | #1st column | ||
M> A(:,1) | P> A[:,0] | R> t(A[,1]) | J> A[:,1] | ||
ans = | array([1, 4, 7]) | [,1] [,2] [,3] | 3-element Array{Int64,1}: | ||
1 | [1,] 1 4 7 | 1 | |||
4 | # 1st column (as column vector) | 4 | |||
7 | P> A[:,[0]] | # 1st column as column vector | 7 | ||
array([[1], | R> A[,1] | ||||
% 1st 2 columns | [4], | [1] 1 4 7 | #1st 2 columns | ||
M> A(:,1:2) | [7]]) | J> A[:,1:2] | |||
ans = | # 1st 2 columns | 3x2 Array{Int64,2}: | |||
1 2 | # 1st 2 columns | R> A[,1:2] | 1 2 | ||
4 5 | P> A[:,0:2] | [,1] [,2] | 4 5 | ||
7 8 | array([[1, 2], | [1,] 1 2 | 7 8 | ||
[4, 5], | [2,] 4 5 | ||||
[7, 8]]) | [3,] 7 8 | ||||
Extracting rows and columns by criteria | M> A = [1 2 3; 4 5 9; 7 8 9] | P> A = np.array([ [1,2,3], [4,5,9], [7,8,9]]) | R> A = matrix(1:9,nrow=3,byrow=T) | J> A=[1 2 3; 4 5 9; 7 8 9] | Extracting rows and columns by criteria |
A = | 3x3 Array{Int64,2}: | ||||
(here: get rows that have value 9 in column 3) | 1 2 3 | P> A | R> A | 1 2 3 | (here: get rows that have value 9 in column 3) |
4 5 9 | array([[1, 2, 3], | [,1] [,2] [,3] | 4 5 9 | ||
7 8 9 | [4, 5, 9], | [1,] 1 2 3 | 7 8 9 | ||
[7, 8, 9]]) | [2,] 4 5 9 | ||||
M> A(A(:,3) == 9,:) | [3,] 7 8 9 | # use '.==' for | |||
ans = | P> A[A[:,2] == 9] | # element-wise check | |||
4 5 9 | array([[4, 5, 9], | R> A[A[,3]==9,] | J> A[ A[:,3] .==9, :] | ||
7 8 9 | [7, 8, 9]]) | 2x3 Array{Int64,2}: | |||
[1] 7 8 9 | 4 5 9 | ||||
7 8 9 | |||||
Accessing elements | M> A = [1 2 3; 4 5 6; 7 8 9] | P> A = np.array([ [1,2,3], [4,5,6], [7,8,9] ]) | R> A = matrix(c(1,2,3,4,5,9,7,8,9),nrow=3,byrow=T) | J> A=[1 2 3; 4 5 6; 7 8 9]; | Accessing elements |
(here: 1st element) | (here: 1st element) | ||||
M> A(1,1) | P> A[0,0] | R> A[1,1] | J> A[1,1] | ||
ans = 1 | 1 | [1] 1 | 1 | ||
MANIPULATING SHAPE AND DIMENSIONS | |||||
Converting | M> A = [1 2 3; 4 5 6; 7 8 9] | P> A = np.array([[1,2,3],[4,5,6],[7,8,9]]) | R> A = matrix(1:9,nrow=3,byrow=T) | J> A=[1 2 3; 4 5 6; 7 8 9] | Converting |
a matrix into a row vector (by column) | a matrix into a row vector (by column) | ||||
M> A(:) | P> A.flatten(1) # returns a copy | J> vec(A) | |||
R> as.vector(A) | |||||
ans = | array([1, 4, 7, 2, 5, 8, 3, 6, 9]) | 9-element Array{Int64,1}: | |||
1 | # alternatively A.ravel() | [1] 1 4 7 2 5 8 3 6 9 | 1 | ||
4 | # ravel() returns a view | 4 | |||
7 | 7 | ||||
2 | 2 | ||||
5 | 5 | ||||
8 | 8 | ||||
3 | 3 | ||||
6 | 6 | ||||
9 | 9 | ||||
Converting | M> b = [1 2 3] | P> b = np.array([1, 2, 3]) | R> b = matrix(c(1,2,3), ncol=3) | J> b=vec([1 2 3]) | Converting |
row to column vectors | 3-element Array{Int64,1}: | row to column vectors | |||
M> b = b' | P> b = b[np.newaxis].T | R> t(b) | 1 | ||
b = | # alternatively | [,1] | 2 | ||
1 | # b = b[:,np.newaxis] | [1,] 1 | 3 | ||
2 | [2,] 2 | ||||
3 | P> b | [3,] 3 | |||
array([[1], | |||||
[2], | |||||
[3]]) | |||||
Reshaping Matrices | M> A = [1 2 3; 4 5 6; 7 8 9] | P> A = np.array([[1,2,3],[4,5,6],[7,8,9]]) | R> A = matrix(1:9,nrow=3,byrow=T) | J> A=[1 2 3; 4 5 6; 7 8 9] | Reshaping Matrices |
A = | 3x3 Array{Int64,2}: | ||||
(here: 3x3 matrix to row vector) | 1 2 3 | P> A | R> A | 1 2 3 | (here: 3x3 matrix to row vector) |
4 5 6 | array([[1, 2, 3], | [,1] [,2] [,3] | 4 5 6 | ||
7 8 9 | [4, 5, 9], | [1,] 1 2 3 | 7 8 9 | ||
[7, 8, 9]]) | [2,] 4 5 6 | ||||
M> total_elements = numel(A) | [3,] 7 8 9 | J> total_elements=length(A) | |||
P> total_elements = np.prod(A.shape) | 9 | ||||
M> B = reshape(A,1,total_elements) | R> total_elements = dim(A)[1] * dim(A)[2] | ||||
% or reshape(A,1,9) | P> B = A.reshape(1, total_elements) | J>B=reshape(A,1,total_elements) | |||
B = | R> B = matrix(A, ncol=total_elements) | 1x9 Array{Int64,2}: | |||
1 4 7 2 5 8 3 6 9 | # alternative shortcut: | 1 4 7 2 5 8 3 6 9 | |||
# A.reshape(1,-1) | R> B | ||||
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] | |||||
P> B | [1,] 1 4 7 2 5 8 3 6 9 | ||||
array([[1, 2, 3, 4, 5, 6, 7, 8, 9]]) | |||||
Concatenating matrices | M> A = [1 2 3; 4 5 6] | P> A = np.array([[1, 2, 3], [4, 5, 6]]) | R> A = matrix(1:6,nrow=2,byrow=T) | J> A=[1 2 3; 4 5 6]; | Concatenating matrices |
M> B = [7 8 9; 10 11 12] | P> B = np.array([[7, 8, 9],[10,11,12]]) | R> B = matrix(7:12,nrow=2,byrow=T) | J> B=[7 8 9; 10 11 12]; | ||
M> C = [A; B] | P> C = np.concatenate((A, B), axis=0) | R> C = rbind(A,B) | J> C=[A; B] | ||
1 2 3 | 4x3 Array{Int64,2}: | ||||
4 5 6 | P> C | R> C | 1 2 3 | ||
7 8 9 | array([[ 1, 2, 3], | [,1] [,2] [,3] | 4 5 6 | ||
10 11 12 | [ 4, 5, 6], | [1,] 1 2 3 | 7 8 9 | ||
[ 7, 8, 9], | [2,] 4 5 6 | 10 11 12 | |||
[10, 11, 12]]) | [3,] 7 8 9 | ||||
[4,] 10 11 12 | |||||
Stacking | M> a = [1 2 3] | P> a = np.array([1,2,3]) | R> a = matrix(1:3, ncol=3) | J> a=[1 2 3]; | Stacking |
vectors and matrices | P> b = np.array([4,5,6]) | vectors and matrices | |||
M> b = [4 5 6] | R> b = matrix(4:6, ncol=3) | J> b=[4 5 6]; | |||
P> np.column_stack([a,b]) | |||||
M> c = [a' b'] | array([[1, 4], | R> matrix(rbind(A, B), ncol=2) | J> c=[a' b'] | ||
c = | [2, 5], | [,1] [,2] | 3x2 Array{Int64,2}: | ||
1 4 | [3, 6]]) | [1,] 1 5 | 1 4 | ||
2 5 | [2,] 4 3 | 2 5 | |||
3 6 | P> np.row_stack([a,b]) | 3 6 | |||
array([[1, 2, 3], | R> rbind(A,B) | ||||
M> c = [a; b] | [4, 5, 6]]) | [,1] [,2] [,3] | J> c=[a; b] | ||
c = | [1,] 1 2 3 | 2x3 Array{Int64,2}: | |||
1 2 3 | [2,] 4 5 6 | 1 2 3 | |||
4 5 6 | 4 5 6 | ||||
BASIC MATRIX OPERATIONS | |||||
Matrix-scalar | M> A = [1 2 3; 4 5 6; 7 8 9] | P> A = np.array([ [1,2,3], [4,5,6], [7,8,9] ]) | R> A = matrix(1:9, nrow=3, byrow=T) | J> A=[1 2 3; 4 5 6; 7 8 9]; | Matrix-scalar |
operations | operations | ||||
M> A * 2 | P> A * 2 | R> A * 2 | # elementwise operator | ||
ans = | array([[ 2, 4, 6], | [,1] [,2] [,3] | |||
2 4 6 | [ 8, 10, 12], | [1,] 2 4 6 | J> A .* 2 | ||
8 10 12 | [14, 16, 18]]) | [2,] 8 10 12 | 3x3 Array{Int64,2}: | ||
14 16 18 | [3,] 14 16 18 | 2 4 6 | |||
P> A + 2 | 8 10 12 | ||||
M> A + 2 | R> A + 2 | 14 16 18 | |||
P> A - 2 | |||||
M> A - 2 | R> A - 2 | J> A .+ 2; | |||
P> A / 2 | |||||
M> A / 2 | R> A / 2 | J> A .- 2; | |||
# Note that NumPy was optimized for | |||||
# in-place assignments | J> A ./ 2; | ||||
# e.g., A += A instead of | |||||
# A = A + A | |||||
Matrix-matrix | M> A = [1 2 3; 4 5 6; 7 8 9] | P> A = np.array([ [1,2,3], [4,5,6], [7,8,9] ]) | R> A = matrix(1:9, nrow=3, byrow=T) | J> A=[1 2 3; 4 5 6; 7 8 9]; | Matrix-matrix |
multiplication | multiplication | ||||
M> A * A | P> np.dot(A,A) # or A.dot(A) | R> A %*% A | J> A * A | ||
ans = | array([[ 30, 36, 42], | [,1] [,2] [,3] | 3x3 Array{Int64,2}: | ||
30 36 42 | [ 66, 81, 96], | [1,] 30 36 42 | 30 36 42 | ||
66 81 96 | [102, 126, 150]]) | [2,] 66 81 96 | 66 81 96 | ||
102 126 150 | [3,] 102 126 150 | 102 126 150 | |||
Matrix-vector | M> A = [1 2 3; 4 5 6; 7 8 9] | P> A = np.array([ [1,2,3], [4,5,6], [7,8,9] ]) | R> A = matrix(1:9, ncol=3) | J> A=[1 2 3; 4 5 6; 7 8 9]; | Matrix-vector |
multiplication | multiplication | ||||
M> b = [ 1; 2; 3 ] | P> b = np.array([ [1], [2], [3] ]) | R> b = matrix(1:3, nrow=3) | J> b=[1; 2; 3]; | ||
M> A * b | P> np.dot(A,b) # or A.dot(b) | R> t(b %*% A) | J> A*b | ||
ans = | [,1] | 3-element Array{Int64,1}: | |||
14 | array([[14], [32], [50]]) | [1,] 14 | 14 | ||
32 | [2,] 32 | 32 | |||
50 | [3,] 50 | 50 | |||
Element-wise | M> A = [1 2 3; 4 5 6; 7 8 9] | P> A = np.array([ [1,2,3], [4,5,6], [7,8,9] ]) | R> A = matrix(1:9, nrow=3, byrow=T) | J> A=[1 2 3; 4 5 6; 7 8 9]; | Element-wise |
matrix-matrix operations | matrix-matrix operations | ||||
M> A .* A | P> A * A | R> A * A | J> A .* A | ||
ans = | array([[ 1, 4, 9], | [,1] [,2] [,3] | 3x3 Array{Int64,2}: | ||
1 4 9 | [16, 25, 36], | [1,] 1 4 9 | 1 4 9 | ||
16 25 36 | [49, 64, 81]]) | [2,] 16 25 36 | 16 25 36 | ||
49 64 81 | [3,] 49 64 81 | 49 64 81 | |||
P> A + A | |||||
M> A .+ A | R> A + A | J> A .+ A; | |||
P> A - A | |||||
M> A .- A | R> A - A | J> A .- A; | |||
P> A / A | |||||
M> A ./ A | R> A / A | J> A ./ A; | |||
# Note that NumPy was optimized for | |||||
# in-place assignments | |||||
# e.g., A += A instead of | |||||
# A = A + A | |||||
Matrix elements to power n | M> A = [1 2 3; 4 5 6; 7 8 9] | P> A = np.array([ [1,2,3], [4,5,6], [7,8,9] ]) | R> A = matrix(1:9, nrow=3, byrow=T) | J> A=[1 2 3; 4 5 6; 7 8 9]; | Matrix elements to power n |
(here: individual elements squared) | M> A.^2 | P> np.power(A,2) | R> A ^ 2 | J> A .^ 2 | (here: individual elements squared) |
ans = | array([[ 1, 4, 9], | [,1] [,2] [,3] | 3x3 Array{Int64,2}: | ||
1 4 9 | [16, 25, 36], | [1,] 1 4 9 | 1 4 9 | ||
16 25 36 | [49, 64, 81]]) | [2,] 16 25 36 | 16 25 36 | ||
49 64 81 | [3,] 49 64 81 | 49 64 81 | |||
Matrix to power n | M> A = [1 2 3; 4 5 6; 7 8 9] | P> A = np.array([ [1,2,3], [4,5,6], [7,8,9] ]) | R> A = matrix(1:9, ncol=3) | J> A=[1 2 3; 4 5 6; 7 8 9]; | Matrix to power n |
(here: matrix-matrix multiplication with itself) | M> A ^ 2 | P> np.linalg.matrix_power(A,2) | # requires the ÔexpmÕ package | J> A ^ 2 | (here: matrix-matrix multiplication with itself) |
ans = | array([[ 30, 36, 42], | 3x3 Array{Int64,2}: | |||
30 36 42 | [ 66, 81, 96], | R> install.packages('expm') | 30 36 42 | ||
66 81 96 | [102, 126, 150]]) | 66 81 96 | |||
102 126 150 | R> library(expm) | 102 126 150 | |||
R> A %^% 2 | |||||
[,1] [,2] [,3] | |||||
[1,] 30 66 102 | |||||
[2,] 36 81 126 | |||||
[3,] 42 96 150 | |||||
Matrix transpose | M> A = [1 2 3; 4 5 6; 7 8 9] | P> A = np.array([ [1,2,3], [4,5,6], [7,8,9] ]) | R> A = matrix(1:9, nrow=3, byrow=T) | J> A=[1 2 3; 4 5 6; 7 8 9] | Matrix transpose |
3x3 Array{Int64,2}: | |||||
M> A' | P> A.T | R> t(A) | 1 2 3 | ||
ans = | array([[1, 4, 7], | [,1] [,2] [,3] | 4 5 6 | ||
1 4 7 | [2, 5, 8], | [1,] 1 4 7 | 7 8 9 | ||
2 5 8 | [3, 6, 9]]) | [2,] 2 5 8 | |||
3 6 9 | [3,] 3 6 9 | J> A' | |||
3x3 Array{Int64,2}: | |||||
1 4 7 | |||||
2 5 8 | |||||
3 6 9 | |||||
Determinant of a matrix: | M> A = [6 1 1; 4 -2 5; 2 8 7] | P> A = np.array([[6,1,1],[4,-2,5],[2,8,7]]) | R> A = matrix(c(6,1,1,4,-2,5,2,8,7), nrow=3, byrow=T) | J> A=[6 1 1; 4 -2 5; 2 8 7] | Determinant of a matrix: |
A -> |A| | A = | 3x3 Array{Int64,2}: | A -> |A| | ||
6 1 1 | P> A | R> A | 6 1 1 | ||
4 -2 5 | array([[ 6, 1, 1], | [,1] [,2] [,3] | 4 -2 5 | ||
2 8 7 | [ 4, -2, 5], | [1,] 6 1 1 | 2 8 7 | ||
[ 2, 8, 7]]) | [2,] 4 -2 5 | ||||
M> det(A) | [3,] 2 8 7 | J> det(A) | |||
ans = -306 | P> np.linalg.det(A) | -306 | |||
-306 | R> det(A) | ||||
[1] -306 | |||||
Inverse of a matrix | M> A = [4 7; 2 6] | P> A = np.array([[4, 7], [2, 6]]) | R> A = matrix(c(4,7,2,6), nrow=2, byrow=T) | J> A=[4 7; 2 6] | Inverse of a matrix |
A = | 2x2 Array{Int64,2}: | ||||
4 7 | P> A | R> A | 4 7 | ||
2 6 | array([[4, 7], | [,1] [,2] | 2 6 | ||
[2, 6]]) | [1,] 4 7 | ||||
M> A_inv = inv(A) | [2,] 2 6 | J> A_inv=inv(A) | |||
A_inv = | P> A_inverse = np.linalg.inv(A) | 2x2 Array{Float64,2}: | |||
0.60000 -0.70000 | R> solve(A) | 0.6 -0.7 | |||
-0.20000 0.40000 | P> A_inverse | [,1] [,2] | -0.2 0.4 | ||
array([[ 0.6, -0.7], | [1,] 0.6 -0.7 | ||||
[-0.2, 0.4]]) | [2,] -0.2 0.4 | ||||
ADVANCED MATRIX OPERATIONS | |||||
Calculating the covariance matrix | M> x1 = [4.0000 4.2000 3.9000 4.3000 4.1000]Õ | P> x1 = np.array([ 4, 4.2, 3.9, 4.3, 4.1]) | R> x1 = matrix(c(4, 4.2, 3.9, 4.3, 4.1), ncol=5) | J> x1=[4.0 4.2 3.9 4.3 4.1]'; | Calculating the covariance matrix |
of 3 random variables | of 3 random variables | ||||
M> x2 = [2.0000 2.1000 2.0000 2.1000 2.2000]' | P> x2 = np.array([ 2, 2.1, 2, 2.1, 2.2]) | R> x2 = matrix(c(2, 2.1, 2, 2.1, 2.2), ncol=5) | J> x2=[2. 2.1 2. 2.1 2.2]'; | ||
(here: covariances of the means | (here: covariances of the means | ||||
of x1, x2, and x3) | M> x3 = [0.60000 0.59000 0.58000 0.62000 0.63000]Õ | P> x3 = np.array([ 0.6, 0.59, 0.58, 0.62, 0.63]) | R> x3 = matrix(c(0.6, 0.59, 0.58, 0.62, 0.63), ncol=5) | J> x3=[0.6 .59 .58 .62 .63]'; | of x1, x2, and x3) |
M> cov( [x1,x2,x3] ) | P> np.cov([x1, x2, x3]) | R> cov(matrix(c(x1, x2, x3), ncol=3)) | J> cov([x1 x2 x3]) | ||
ans = | Array([[ 0.025 , 0.0075 , 0.00175], | [,1] [,2] [,3] | 3x3 Array{Float64,2}: | ||
2.5000e-02 7.5000e-03 1.7500e-03 | [ 0.0075 , 0.007 , 0.00135], | [1,] 0.02500 0.00750 0.00175 | 0.025 0.0075 0.00175 | ||
7.5000e-03 7.0000e-03 1.3500e-03 | [ 0.00175, 0.00135, 0.00043]]) | [2,] 0.00750 0.00700 0.00135 | 0.0075 0.007 0.00135 | ||
1.7500e-03 1.3500e-03 4.3000e-04 | [3,] 0.00175 0.00135 0.00043 | 0.00175 0.00135 0.00043 | |||
Calculating | M> A = [3 1; 1 3] | P> A = np.array([[3, 1], [1, 3]]) | R> A = matrix(c(3,1,1,3), ncol=2) | J> A=[3 1; 1 3] | Calculating |
eigenvectors and eigenvalues | A = | 2x2 Array{Int64,2}: | eigenvectors and eigenvalues | ||
3 1 | P> A | R> A | 3 1 | ||
1 3 | array([[3, 1], | [,1] [,2] | 1 3 | ||
[1, 3]]) | [1,] 3 1 | ||||
M> [eig_vec,eig_val] = eig(A) | [2,] 1 3 | J> (eig_vec,eig_val)=eig(a) | |||
eig_vec = | P> eig_val, eig_vec = np.linalg.eig(A) | ([2.0,4.0], | |||
-0.70711 0.70711 | R> eigen(A) | 2x2 Array{Float64,2}: | |||
0.70711 0.70711 | P> eig_val | $values | -0.707107 0.707107 | ||
eig_val = | array([ 4., 2.]) | [1] 4 2 | 0.707107 0.707107) | ||
Diagonal Matrix | |||||
2 0 | P> eig_vec | $vectors | |||
0 4 | Array([[ 0.70710678, -0.70710678], | [,1] [,2] | |||
[ 0.70710678, 0.70710678]]) | [1,] 0.7071068 -0.7071068 | ||||
[2,] 0.7071068 0.7071068 | |||||
Generating a Gaussian dataset: | % requires statistics toolbox package | P> mean = np.array([0,0]) | # requires the ÔmassÕ package | # requires the Distributions package from https://github.com/JuliaStats/Distributions.jl | Generating a Gaussian dataset: |
% how to install and load it in Octave: | |||||
creating random vectors from the multivariate normal | P> cov = np.array([[2,0],[0,2]]) | R> install.packages('MASS') | J> using Distributions | creating random vectors from the multivariate normal | |
distribution given mean and covariance matrix | % download the package from: | distribution given mean and covariance matrix | |||
% http://octave.sourceforge.net/packages.php | P> np.random.multivariate_normal(mean, cov, 5) | R> library(MASS) | J> mean=[0., 0.] | ||
(here: 5 random vectors with | % pkg install | 2-element Array{Float64,1}: | (here: 5 random vectors with | ||
mean 0, covariance = 0, variance = 2) | % ~/Desktop/io-2.0.2.tar.gz | Array([[ 1.55432624, -1.17972629], | R> mvrnorm(n=10, mean, cov) | 0 | mean 0, covariance = 0, variance = 2) |
% pkg install | [-2.01185294, 1.96081908], | [,1] [,2] | 0 | ||
% ~/Desktop/statistics-1.2.3.tar.gz | [-2.11810813, 1.45784216], | [1,] -0.8407830 -0.1882706 | |||
[-2.93207591, -0.07369322], | [2,] 0.8496822 -0.7889329 | J> cov=[2. 0.; 0. 2.] | |||
M> pkg load statistics | [-1.37031244, -1.18408792]]) | [3,] -0.1564171 0.8422177 | 2x2 Array{Float64,2}: | ||
[4,] -0.6288779 1.0618688 | 2.0 0.0 | ||||
M> mean = [0 0] | [5,] -0.5103879 0.1303697 | 0.0 2.0 | |||
[6,] 0.8413189 -0.1623758 | |||||
M> cov = [2 0; 0 2] | [7,] -1.0495466 -0.4161082 | J> rand( MvNormal(mean, cov), 5) | |||
cov = | [8,] -1.3236339 0.7755572 | 2x5 Array{Float64,2}: | |||
2 0 | [9,] 0.2771013 1.4900494 | -0.527634 0.370725 -0.761928 -3.91747 1.47516 | |||
0 2 | [10,] -1.3536268 0.2338913 | -0.448821 2.21904 2.24561 0.692063 0.390495 | |||
M> mvnrnd(mean,cov,5) | |||||
2.480150 -0.559906 | |||||
-2.933047 0.560212 | |||||
0.098206 3.055316 | |||||
-0.985215 -0.990936 | |||||
1.122528 0.686977 | |||||