-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathArrayMatrices.R
61 lines (50 loc) · 918 Bytes
/
ArrayMatrices.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
##### Array & Matrices #####
## cbind and rbind
a<-array(c(1:10),dim=c(5,2))
a
b<-array(c(11:20),dim=c(5,2))
b
cbind(a,b)
rbind(a,b)
cbind(c(a,b))
## sliced view
arr=array(c(1:20),dim=c(4,5))
arr
i=cbind(1:3,3:1)
i
arr[i]
arr[i]<-0
arr
## inner product & outer product
a*b ## element-wise multiplication
t(a)%*%b ## matrix multiplication
crossprod(a,b) ## equivalent to t(a)%*%b
crossprod(b,a)
outer(a,b)
outer(b,a)
## transpose & permutation: only for 2D matrices
t(a)
aperm(a,c(2,1))
## diagonal
d<-diag(c(1)*3) ## equivalent to diag(3)
d
diag(5)
e<-array(1:9,c(3,3))
e
diag(e)
## Linear Equation & Inverse
A<-array(c(1,2,2,1),dim=c(2,2))
solve(A)
## Eigenvalues & Eigenvectors
eigen(A)
## Singular Value Decomposition
svd(A)
absdet<-prod(svd(A)$d)
absdet
det(A)
D<-diag(svd(a)$d)
D
U<-svd(a)$u
V<-svd(a)$v
U%*%D%*%t(V)
svd(b)