12 Matrix & Array
-
Generate p-dimensional array
-
Matrix is the 2-dimensional array
-
In Python, we need to load
numpy
library
12.1 R
X = matrix(c(1, 2, 3, 4), nrow = 2, ncol = 2)
Y = matrix(c(5, 6, 7, 8), nrow = 2, ncol = 2)
Function | Explanation | Example. |
---|---|---|
dim |
Dimension of a matrix (row, col) | dim(X) |
nrow |
Number of rows of a matrix | nrow(X) |
ncol |
Number of columns of a matrix | ncol(X) |
rbind |
Row bind two matrices | rbind(X, Y) |
cbind |
Column bind two matrices | cbind(X, Y) |
rowSums |
Rowwise sum of elements of a matrix | rowSums(X) |
colSums |
Columnwise sum of elements of a matrix | colSums(X) |
rank |
Rank of a matrix | rank(X) |
+ |
Addition of two matrices | X + Y |
- |
Subtraction of two matrices | X - Y |
* |
Elementwise multiplication of two matrices | X * Y |
%*% |
Multiplication of two matrices | X %*% Y |
outer |
Outer product of two matrices | outer(X, Y) |
cross |
Cross product of two matrices | cross(X, Y) |
t |
Tranpose of a matrix | t(X) |
det |
Determinant of a matrix | det(X) |
solve |
Inverse of a matrix | solve(X) |
12.2 R Matrix Indexing
R Matrix is indexed by row and column
MatrixObj[row, col]
Example:
MatrixObj[2, 1]
gets the element at the second row and first columnKeeping it blank in the first (second) position will return all rows (all columns)
Example:
MatrixObj[, 1]
returns all rows of the first columnExample:
MatrixObj[2, ]
returns all columns of the second rowRemember the R indexing starts at 1
12.3 Python
import numpy as np
X = np.array([[1,2], [3,4]])
Y = np.array([[5,6], [7,8]])
Method | Explanation | Example. |
---|---|---|
ndim |
Dimension of an array | X.ndim |
shape |
Shape of a matrix or array (row, col) | X.shape |
shape |
Number of rows of a matrix | X.shape[0] |
shape |
Number of columns of a matrix | X.shape[1] |
vstack |
Row bind two matrices | np.vstack((X, Y)) |
hstack |
Column bind two matrices | np.hstack((X, Y)) |
sum, axis = 0 |
Rowwise sum of elements of a matrix | X.sum(axis = 0); np.sum(X, axis = 0) |
sum, axis = 1 |
Columnwise sum of elements of a matrix | X.sum(axis = 1); np.sum(X, axis = 1) |
rank |
Rank of a matrix | np.linalg.matrix_rank(X) |
+ |
Addition of two matrices | X + Y |
- |
Subtraction of two matrices | X - Y |
* |
Elementwise multiplication of two matrices | X * Y; np.multiply(X, Y) |
%*% |
Multiplication of two matrices | np.matmul(X, Y) |
outer |
Outer product of two matrices | np.outer(X, Y) |
cross |
Cross product of two matrices | np.cross(X, Y) |
t |
Tranpose of a matrix | np.transpose(X) |
det |
Determinant of a matrix | np.linalg.det(X) |
solve |
Inverse of a matrix | np.linalg.inv(X) |
12.4 Python Matrix Indexing
Indexing
NumPy
matrices is fundamentally the same as Python list indexing, althoughNumPy
array can be n-dimensional.Slicing and indexing on matrix is same as lists:
MatrixObj[start:stop:step]
- Indexing in one dimension will default to the first dimension (i.e. row):
X[0] = np.array([1,2])
- Specifying the second dimension index will return an element of the matrix:
X[0,1] = 2
or X[0][1] = 2
- To specifiy all elements of one dimension use
:
.
X[:, 1] = np.array([2,4])
- In the case of
ndim > 2
the ellipsis...
inserts as many full slices:
to extend the multi-dimensional slice to all dimensions.
X[:,:,:,0]
is equivalent to X[...,0]
- Remember the Python indexing starts at 0