2012-09-06

# 多维数组和矩阵

## 数组（array）和矩阵(matrix)

```一组值只有定义了维数向量（dim属性）后才能被看作是数组。比如：
> z <- 1:1500
> dim(z) <- c(3, 5, 100)

```

> dim(z) <- 1500

```> z <- array(1:1500, dim=c(3,5,100))
函数matrix()用来定义最常用的一种数组：二维数组，即矩阵。其完全格式为
matrix(data = NA, nrow = 1, ncol = 1, byrow = FALSE, dimnames = NULL)

```

```> b <- matrix(1:12, ncol=4, byrow=T)
> b
[,1] [,2] [,3] [,4]
[1,]    1    2    3    4
[2,]    5    6    7    8
[3,]    9   10   11   12

```

```> b <- matrix(0, nrow=3, ncol=4)
```

## 数组下标

```a[] <- 0
```

## 不规则数组下标

```> b <- matrix(c(1,1,1, 2,2,3, 1,3,4, 2,1,4), ncol=3, byrow=T)
> b
[,1] [,2] [,3]
[1,]    1    1    1
[2,]    2    2    3
[3,]    1    3    4
[4,]    2    1    4
> a[b]
[1]  1 16 23 20
```

```> a[b] <- c(101, 102, 103, 104)
> a

> a[b] <- 0
> a

```

## 数组四则运算

> D <- C + 2*A/B

```> x1 <- c(100, 200)
> x2 <- 1:6
> x1+x2
[1] 101 202 103 204 105 206
> x3 <- matrix(1:6, nrow=3)
> x3
[,1] [,2]
[1,]    1    4
[2,]    2    5
[3,]    3    6
> x1+x3
[,1] [,2]
[1,]  101  204
[2,]  202  105
[3,]  103  206
```

## 矩阵运算

```> A <- matrix(1:12, nrow=4, ncol=3, byrow=T)
> B <- matrix(c(1,0), nrow=3, ncol=2, byrow=T)
> A
[,1] [,2] [,3]
[1,]    1    2    3
[2,]    4    5    6
[3,]    7    8    9
[4,]   10   11   12
> B
[,1] [,2]
[1,]    1    0
[2,]    1    0
[3,]    1    0
> A %*% B
[,1] [,2]
[1,]    6    0
[2,]   15    0
[3,]   24    0
[4,]   33    0
>

```

> ?qr

## 矩阵合并与拉直

```> x1 <- rbind(c(1,2), c(3,4))
> x1
[,1] [,2]
[1,]    1    2
[2,]    3    4
> x2 <- 10+x1
> x3 <- cbind(x1, x2)
> x3
[,1] [,2] [,3] [,4]
[1,]    1    2   11   12
[2,]    3    4   13   14
> x4 <- rbind(x1, x2)
> x4
[,1] [,2]
[1,]    1    2
[2,]    3    4
[3,]   11   12
[4,]   13   14
> cbind(1, x1)
[,1] [,2] [,3]
[1,]    1    1    2
[2,]    1    3    4

```

## 数组的维名字

```> x <- matrix(1:6, ncol=2,
+ dimnames=list(c("one", "two", "three"), c("First", "Second")),
+ byrow=T)
> x
First Second
one       1      2
two       3      4
three     5      6

> x <- matrix(1:6, ncol=2,byrow=T)
> colnames(x) <- c("First", "Second")
> rownames(x) <- c("one", "two", "three")

> x[c("one","three"),]
First Second
one       1      2
three     5      6

```

## 数组的外积

a和b的外积记作 a %o% b。如
```> d <- a %o% b
```

```> d <- outer(a, b, '*')
```

```> x <- seq(-2, 2, length=20)
> y <- seq(-pi, pi, length=20)
> f <- function(x, y) cos(y)/(1+x^2)
> z <- outer(x, y, f)
```

> d <- outer(0:9, 0:9)

> d2 <- outer(d, d, "-")

> fr <- table(d2)

```> plot(as.numeric(names(fr)), fr, type="h",
+  xlab="行列式", ylab="频数")

```

## 数组的广义转置

```> a <- array(1:24, dim=c(2,3,4))
> b <- aperm(a, c(2, 3, 1))

```

## apply函数

apply(X, MARGIN, FUN, ...)

```> a <- cbind(c(4,9,1), c(3,7,2))
> a
[,1] [,2]
[1,]    4    3
[2,]    9    7
[3,]    1    2
> apply(a, 2, sort)
[,1] [,2]
[1,]    1    2
[2,]    4    3
[3,]    9    7
>

```

```> apply(a, 1, sort)
[,1] [,2] [,3]
[1,]    3    7    1
[2,]    4    9    2
> t(apply(a,1,sort))
[,1] [,2]
[1,]    3    4
[2,]    7    9
[3,]    1    2

```

0条评论

×

¥.00

• 电子书免费读
• 全站无广告
• 全屏阅读
• 高品质朗读
• 批量上传文档
• 购书5折
• 5千个文件夹
• 专属客服