4

我有一个矩阵,我想在其中将某些特定元素归零。

例如,假设我的矩阵是:

m <- matrix(1:100, ncol=10)

然后我有两个向量指示要保留哪些元素

m.from <- c(2, 5, 4, 4, 6, 3, 1, 4, 2, 5)
m.to   <- c(7, 9, 6, 8, 9, 5, 6, 8, 4, 8)

因此,例如,我将在第 1 行中保留元素 3:6,并将元素 1:2 和 7:10 设置为 0。对于第 2 行,我将保留 6:8 并将其余元素设为零,依此类推。

现在,我可以轻松做到:

for (line in 1:nrow(m))
    {
    m[line, 1:m.from[line]] <- 0
    m[line, m.to[line]:ncol(m)] <- 0
    }

这给出了正确的结果。

然而,在我的具体情况下,我在一个 ~15000 x 3000 矩阵上运行,这使得使用这种循环的时间非常长。

如何加快此代码的速度?我虽然使用apply,但是如何访问 m.from 和 m.to 的正确索引?

4

4 回答 4

8

这是一个简单的面向矩阵的解决方案:

m[col(m) <= m.from] <- 0
m[col(m) >= m.to] <- 0
m
      [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
 [1,]    0    0   21   31   41   51    0    0    0     0
 [2,]    0    0    0    0    0   52   62   72    0     0
 [3,]    0    0    0    0   43    0    0    0    0     0
 [4,]    0    0    0    0   44   54   64    0    0     0
 [5,]    0    0    0    0    0    0   65   75    0     0
 [6,]    0    0    0   36    0    0    0    0    0     0
 [7,]    0   17   27   37   47    0    0    0    0     0
 [8,]    0    0    0    0   48   58   68    0    0     0
 [9,]    0    0   29    0    0    0    0    0    0     0
[10,]    0    0    0    0    0   60   70    0    0     0

(我想我也可能在这个上赢得 R 高尔夫奖。)我的参赛作品是:

m[col(m)<=m.from|col(m)>= m.to]<-0 
于 2012-09-20T22:38:27.493 回答
4

最好的解决方案是预先计算所有要替换的索引,然后用单个赋值操作替换它们。

由于 R 以列优先顺序存储矩阵,我发现更容易考虑要在矩阵的转置版本中替换的元素序列。这就是我在下面使用的。但是,如果这两个调用t()成本太高,我相信您可以找到一种巧妙的方法来计算未转置矩阵的索引——也许使用包含行和列索引的两列矩阵。

## Your example
m <- matrix(1:100, ncol=10)
m.from <- c(2, 5, 4, 4, 6, 3, 1, 4, 2, 5)
m.to   <- c(7, 9, 6, 8, 9, 5, 6, 8, 4, 8)

## Let's work with a transposed version of your matrix
tm <- t(m)

## Calculate indices of cells to be replaced
i <- (seq_len(ncol(tm)) - 1) * nrow(tm)
m.to   <- c(1, m.to + i)
m.from <- c(m.from + i, length(m))
ii <- unlist(mapply(seq, from = m.to, to = m.from))

## Perform replacement and transpose back results
tm[ii] <- 0
m <- t(tm)
#       [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
#  [1,]    0    0   21   31   41   51    0    0    0     0
#  [2,]    0    0    0    0    0   52   62   72    0     0
#  [3,]    0    0    0    0   43    0    0    0    0     0
#  [4,]    0    0    0    0   44   54   64    0    0     0
#  [5,]    0    0    0    0    0    0   65   75    0     0
#  [6,]    0    0    0   36    0    0    0    0    0     0
#  [7,]    0   17   27   37   47    0    0    0    0     0
#  [8,]    0    0    0    0   48   58   68    0    0     0
#  [9,]    0    0   29    0    0    0    0    0    0     0
# [10,]    0    0    0    0    0   60   70    0    0     0
于 2012-09-20T16:18:38.220 回答
2

一个sapply版本。

m <- matrix(1:100, ncol=10)
m.from <- c(2, 5, 4, 4, 6, 3, 1, 4, 2, 5)
m.to   <- c(7, 9, 6, 8, 9, 5, 6, 8, 4, 8)

t(sapply(1:nrow(m), function(i) replace(m[i,], c(1:m.from[i], m.to[i]:ncol(m)), 0 )))   



     [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
 [1,]    0    0   21   31   41   51    0    0    0     0
 [2,]    0    0    0    0    0   52   62   72    0     0
 [3,]    0    0    0    0   43    0    0    0    0     0
 [4,]    0    0    0    0   44   54   64    0    0     0
 [5,]    0    0    0    0    0    0   65   75    0     0
 [6,]    0    0    0   36    0    0    0    0    0     0
 [7,]    0   17   27   37   47    0    0    0    0     0
 [8,]    0    0    0    0   48   58   68    0    0     0
 [9,]    0    0   29    0    0    0    0    0    0     0
[10,]    0    0    0    0    0   60   70    0    0     0

经过时间尚未测试

于 2012-09-20T17:39:09.787 回答
1

这个选项构造了一个两列矩阵索引元素被替换,并且不需要矩阵转置,所以应该很难被击败,speedwise

## Your data
m <- matrix(1:100, ncol=10)
m.from <- c(2, 5, 4, 4, 6, 3, 1, 4, 2, 5)
m.to   <- c(7, 9, 6, 8, 9, 5, 6, 8, 4, 8)

## Construct a two column matrix with row (ii) and column (jj) indices
## of cells to be replaced
ii <- rep.int(1:ncol(m), times = (m.from + (ncol(m) - m.to + 1)))
jj <- mapply(seq, from = m.from + 1, to = m.to - 1)
jj <- unlist(sapply(jj, function(X) setdiff(1:10,X)))
ij <- cbind(ii, jj)

## Replace cells
m[ij] <- 0
#       [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
#  [1,]    0    0   21   31   41   51    0    0    0     0
#  [2,]    0    0    0    0    0   52   62   72    0     0
#  [3,]    0    0    0    0   43    0    0    0    0     0
#  [4,]    0    0    0    0   44   54   64    0    0     0
#  [5,]    0    0    0    0    0    0   65   75    0     0
#  [6,]    0    0    0   36    0    0    0    0    0     0
#  [7,]    0   17   27   37   47    0    0    0    0     0
#  [8,]    0    0    0    0   48   58   68    0    0     0
#  [9,]    0    0   29    0    0    0    0    0    0     0
# [10,]    0    0    0    0    0   60   70    0    0     0
于 2012-09-20T20:51:52.697 回答