May

24

# matrix calculation in R and Python

- Useful packages:
- R: library(matrixcalc)
- Python: numpy

- Matrix generation:
- R: matrix(1:6, 2, 3)
- Python: np.arange(1,7).reshape(2,3)

- construct a diagnal matrix:
- R: advanced-R
- diag(vector, nrow, ncol) #construct a matrix which diagonal elements is equal to the vector
- diag(matrix) # a vector which elements is the diagonal elements of the matrix

- Python: np.diag(vector)
- np.diag(vector, k) # like the diag(vector) in R, k means the shift of the diagnoal
- np.diag(matrix) #return a vector which values are the diagonal elements of the matrix

- R: advanced-R
- singular value decomposition:
- R:
- svd(matrix)

- Python:
- np.linalg.svd(matrix, full_matrices=True) #if full_matrices=False, the result will be the same as with R

- R:
- resolve Ax=b
- R: solve(A,b)
- Python: np.linalg.solve(A,b)

Trackback from your site.