R矩阵运算

时间:2015-12-26 08:27:08

标签: r matrix-multiplication

我有一个矩阵(15000 x 3000)。目标是根据原始矩阵和初始值生成新矩阵。例如,我想要实现的标准是:

这就是我现在的代码设置方式。

self.contentView.translatesAutoresizingMaskIntoConstraints = NO;

DF是原始矩阵。

DF1是新形成的矩阵

我的问题:有没有其他方法可以做到这一点?更快的方式?

由于嵌套循环不能正常工作,我尝试使用apply,但我不知道如何编写函数,因为它与两个矩阵有关。

一个例子

DF1[1,]=1

for( i in 2:2000 ) {
    for( j in 1:15000 ) {

              if(DF[j,i] == 1 && DF1[j-1,i] == 0)
                DF1[j,i] = 1
              else if(DF[j,i] == 0 && DF1[j-1,i] == 1)
                DF1[j,i] = 0
              else DF[j,i,1] = DF1[j-1,i]

    }
}

循环(不起作用)

x <- structure(c(1L, 0L, 0L, NA, NA, 0L, NA, 0L, 1L, 0L, 1L, 0L, 0L, 
NA, 0L, NA, 1L, NA, 1L, 0L, 1L, 0L, 1L, 0L), .Dim = c(4L, 6L), .Dimnames = list(
    NULL, NULL))
x
#     [,1] [,2] [,3] [,4] [,5] [,6]
#[1,]    1   NA    1    0    1    1
#[2,]    0    0    0   NA   NA    0
#[3,]    0   NA    1    0    1    1
#[4,]   NA    0    0   NA    0    0

1 个答案:

答案 0 :(得分:0)

函数f1使用嵌套循环。 (为了摆脱与NA进行比较导致非逻辑值NA的问题,我将NA替换为Inf。) 仔细阅读由循环表示的算法会导致替代f2

f1 <- function( x, initialValues = 1 )
{
  x[which(is.na(x))] <- Inf
  y <- matrix(NA,nrow(x),ncol(x))
  y[1,] <- initialValues

  for( i in 1:ncol(x) ) { 
    for( j in 2:nrow(x) ) { 
      if(x[j,i] == 1 && y[j-1,i] == 0) { 
        y[j,i] = 1 
      }else{
        if(x[j,i] == 0 && y[j-1,i] == 1) {
          y[j,i] = 0 
        }else{ 
          y[j,i] = y[j-1,i]
        }
      }
    }
  }
  return(y)
}

f2 <- function( x, initialValues = 1 )
{  
  g <- function(v)
  {
    m <- cumsum(!is.na(v))
    v[which(!is.na(v))[m]]
  }

  x[which(!(x %in% 0:1))] <- NA
  x[1,] <- initialValues
  return( apply(x,2,g) )
}

函数g填充NA - 向量v中的差距:g(v)[i]等于v[j],其中j是最大的索引这样j<=iv[j]!=NA。 (归纳证明:v[which(!is.na(v))]包含NA中的非v值。如果v[i]==NAm[i]==m[i-1]g(v)[i]==v[which(!is.na(v))[m[i]]]==v[which(!is.na(v))[m[i-1]]==g(v)[i-1]。否则{{1} }},因此m[i]==m[i-1]+1g(v)[i-1]==v[which(!is.na(v))[m[i-1]]]==v[which(!is.na(v))][m[i-1]],下一个非g(v)[i]==v[which(!is.na(v))[m[i]]]==v[which(!is.na(v))][m[i]]==v[which(!is.na(v))][m[i-1]+1]值。)

NAf2快,特别是对于大型矩阵。 来自问题的小矩阵:

f1

更大的矩阵:

> library(microbenchmark)

> x <- structure(c(1L, 0L, 0L, NA, NA, 0L, NA, 0L, 1L, 0L, 1L, 0L, 0L, 
+                  NA, 0L, NA, 1L, NA, 1L, 0L, 1L, 0L, 1L, 0L), .Dim = c(4L, 6 .... [TRUNCATED] 

> microbenchmark( f1(x), f2(x) )
Unit: microseconds
  expr     min       lq     mean   median      uq     max neval
 f1(x) 433.864 461.2645 482.9120 471.6805 480.059 920.716   100
 f2(x) 379.518 387.6700 402.9235 391.7465 414.617 620.453   100

> all(f1(x)==f2(x))
[1] TRUE

更大:

> set.seed(1)

> n <- 200

> m <- 300

> big_x <- matrix(sample(0:10,n*m,replace=TRUE),n,m)

> big_x[sample(1:(n*m),floor((n*m)/3))] <- NA

> microbenchmark( f1(big_x), f2(big_x) )
Unit: milliseconds
      expr       min        lq      mean    median        uq      max neval
 f1(big_x) 360.42174 495.63713 662.54576 772.42981 778.18100 890.0092   100
 f2(big_x)  33.54202  38.65849  62.25661  67.82429  72.42288 188.2729   100

> all(f1(big_x)==f2(big_x))
[1] TRUE
> 

问题中提到的大小为15000乘以3000的矩阵:

> set.seed(1)

> n <- 800

> m <- 1000

> huge_x <- matrix(sample(0:10,n*m,replace=TRUE),n,m)

> huge_x[sample(1:(n*m),floor((n*m)/3))] <- NA

> microbenchmark( f1(huge_x), f2(huge_x) )
Unit: milliseconds
       expr       min        lq     mean    median       uq       max neval
 f1(huge_x) 4002.4121 7759.2438 8149.821 8466.4698 8950.172 10087.251   100
 f2(huge_x)  311.4259  520.5374  751.874  774.2699 1010.188  1228.504   100

> all(f1(huge_x)==f2(huge_x))
[1] TRUE
> 
相关问题