R 統計軟體 -- 矩陣運算

# 範例一

``````> d <- seq(1,25,1)
> matrix(d, nrow=5, ncol=5, byrow=T)
[,1] [,2] [,3] [,4] [,5]
[1,]    1    2    3    4    5
[2,]    6    7    8    9   10
[3,]   11   12   13   14   15
[4,]   16   17   18   19   20
[5,]   21   22   23   24   25
> d
[1]  1  2  3  4  5  6  7  8  9 10 11 12 13 14
[15] 15 16 17 18 19 20 21 22 23 24 25
> m = matrix(d, nrow=5, ncol=5, byrow=T)
> m
[,1] [,2] [,3] [,4] [,5]
[1,]    1    2    3    4    5
[2,]    6    7    8    9   10
[3,]   11   12   13   14   15
[4,]   16   17   18   19   20
[5,]   21   22   23   24   25
> m[1,]
[1] 1 2 3 4 5
> m[2,2]
[1] 7
> m[2,1:5]
[1]  6  7  8  9 10
> m[1:5, 2]
[1]  2  7 12 17 22
> m[2, c(1,3,5)]
[1]  6  8 10
> m[c(1,3,5), 2]
[1]  2 12 22
> m[2, -c(1,3,5)]
[1] 7 9
>```
```

``````> x = c(1,2,3,4,5,6)
> attr(x, "dim")<-c(2,3)
> x
[,1] [,2] [,3]
[1,]    1    3    5
[2,]    2    4    6
>
> y = c(1,2,3,4)
> dim(y) = c(2,2) # 矩陣其實是二維陣列，向量只有在
> y
[,1] [,2]
[1,]    1    3
[2,]    2    4
>```
```

# 範例三

``````> x <- array(1:20, dim=c(4,5))
> x
[,1] [,2] [,3] [,4] [,5]
[1,] 1 5 9 13 17
[2,] 2 6 10 14 18
[3,] 3 7 11 15 19
[4,] 4 8 12 16 20
> i <- array(c(1:3,3:1), dim=c(3,2))
> i
[,1] [,2]
[1,] 1 3
[2,] 2 2
[3,] 3 1
> x[i]
[1] 9 6 3
> x[i] <- 0
> x
[,1] [,2] [,3] [,4] [,5]
[1,] 1 5 0 13 17
[2,] 2 0 10 14 18
[3,] 0 7 11 15 19
[4,] 4 8 12 16 20
>```
```

# 範例四：

``````> matrix(rep(3,8), nrow=4)
[,1] [,2]
[1,]    3    3
[2,]    3    3
[3,]    3    3
[4,]    3    3
> matrix(3, nrow=4, ncol=3)
[,1] [,2] [,3]
[1,]    3    3    3
[2,]    3    3    3
[3,]    3    3    3
[4,]    3    3    3

> w=matrix(0, nrow=3, ncol=4)
> w
[,1] [,2] [,3] [,4]
[1,]    0    0    0    0
[2,]    0    0    0    0
[3,]    0    0    0    0
> w[2,3]=5
> w
[,1] [,2] [,3] [,4]
[1,]    0    0    0    0
[2,]    0    0    5    0
[3,]    0    0    0    0
>
> x=matrix(1:12, nrow=3, ncol=4)
> x
[,1] [,2] [,3] [,4]
[1,]    1    4    7   10
[2,]    2    5    8   11
[3,]    3    6    9   12
> dim(x)
[1] 3 4
> y = 1:12
> y %*% x

> y = 1:3
> y %*% x
[,1] [,2] [,3] [,4]
[1,]   14   32   50   68
> t(x)
[,1] [,2] [,3]
[1,]    1    2    3
[2,]    4    5    6
[3,]    7    8    9
[4,]   10   11   12
> diag(c(3,7,2))
[,1] [,2] [,3]
[1,]    3    0    0
[2,]    0    7    0
[3,]    0    0    2
> diag(x)
[1] 1 5 9
> det(x)

'x' must be a square matrix
> z = matrix(1:9, nrow=3, ncol=3)
> det(z)
[1] 0
> z
[,1] [,2] [,3]
[1,]    1    4    7
[2,]    2    5    8
[3,]    3    6    9
> z[2,1]=10
> z
[,1] [,2] [,3]
[1,]    1    4    7
[2,]   10    5    8
[3,]    3    6    9
> det(z)
[1] 48
> eigen(z)
\$values
[1] 17.936765+0.000000i -1.468383+0.721055i -1.468383-0.721055i

\$vectors
[,1]                  [,2]                  [,3]
[1,] 0.4100596+0i  0.2162169-0.1118877i  0.2162169+0.1118877i
[2,] 0.6875542+0i -0.8668747+0.0000000i -0.8668747+0.0000000i
[3,] 0.5992665+0i  0.4306386+0.0617265i  0.4306386-0.0617265i

> x
[,1] [,2] [,3] [,4]
[1,]    1    4    7   10
[2,]    2    5    8   11
[3,]    3    6    9   12
> rownames(x) = paste("r", 1:3, sep="_")
> x
[,1] [,2] [,3] [,4]
r_1    1    4    7   10
r_2    2    5    8   11
r_3    3    6    9   12
> colnames(x) = paste("c", 1:4, sep="")
> x
c1 c2 c3 c4
r_1  1  4  7 10
r_2  2  5  8 11
r_3  3  6  9 12
>```
```

# 相關套件

page revision: 7, last edited: 26 Oct 2011 01:18