Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/77.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R组合矩阵_R_Math_Matrix - Fatal编程技术网

R组合矩阵

R组合矩阵,r,math,matrix,R,Math,Matrix,我有两个矩阵,一个是二进制的(零或一),另一个是相同维数的整数矩阵,它们是平方矩阵 我想要一种有效的方法,以一种特定的方式将它们结合起来,而不需要沿着每个元素进行迭代 我想把它们结合起来的方法是,从矩阵a和矩阵B中得到一个结果矩阵,对于元素,它取的是不是零的最小数 有人能想出R中的一个技巧来实现这一点吗?我曾尝试用数学方法来实现这一点,但一直都很简短,我想知道是否有一种方法可以用条件语句覆盖矩阵 我的猜测是: ifelse(A == 0, B, pmin(A, B)) 或许 ifelse(A

我有两个矩阵,一个是二进制的(零或一),另一个是相同维数的整数矩阵,它们是平方矩阵

我想要一种有效的方法,以一种特定的方式将它们结合起来,而不需要沿着每个元素进行迭代

我想把它们结合起来的方法是,从矩阵a和矩阵B中得到一个结果矩阵,对于元素,它取的是不是零的最小数

有人能想出R中的一个技巧来实现这一点吗?我曾尝试用数学方法来实现这一点,但一直都很简短,我想知道是否有一种方法可以用条件语句覆盖矩阵

我的猜测是:

ifelse(A == 0, B, pmin(A, B))
或许

ifelse(A == 0, B, ifelse(B == 0, A, pmin(A, B)))
如果这不是你想要的,请澄清(并提供一个例子)

mnel的方法产生:

> ifelse(matB == 0, matB, pmin(matA, matB))
     [,1] [,2] [,3] [,4]
[1,]    0   -4    0    1
[2,]   -7    0    0    1
[3,]    0   -2    1    0
[4,]   -5   -1    0    1
> (matB * !matA) + matA
     [,1] [,2] [,3] [,4]
[1,]   -8   -4    1    4
[2,]   -7   -3    1    5
[3,]   -6   -2    2    6
[4,]   -5   -1    3    7

从@A_Skeleton对缩放的评论中,您可以将矩阵分解为块:

mnel <- function(matA, matB) {
  (matB * !matA) + matA
}

# method takes a function as the argument
mcombine <- function(matA, matB, method) {
  chunkSize <- 10000
  matC <- matrix(0, nrow(matA), ncol(matA))
  for (i in 1:floor(nrow(matA) / chunkSize)) {
    curRange <- (chunkSize * (i-1) + 1):(i * chunkSize)
    matC[curRange,] <- method(matA[curRange,], matB[curRange,])
  }
  # handle case where dimensions don't divide exactly into chunks
  lastRange <- i*chunkSize:nrow(matA)
  matC[lastRange,] <- method(matA[lastRange,], matB[lastRange,])
  matC
}

# Using mnel's method:
matC <- mcombine(matA, matB, mnel)

mnel如果A和B都为0会发生什么?我想你们欠我们一个例子,说明预期的结果。@flodel Dwin的解决方案就是我的目标。上述解决方案中matC[1,3]中的0,0实例将是一个错误的结果,但在理论上不应出现。我想给大家举一个更全面的例子,但当我本周整理出它背后的理论时,这个过程在我头脑中还不足以做到这一点。我很乐意在一周左右的时间内跟进。从广义上讲,这将是用于构建基因相互作用网络的东西……但是
matC[1,3]
不是
(0,0)
实例,它是
(0,1)
实例。所以我希望结果是
1
。感谢您的解决方案,但不幸的是,虽然这正是我在较小的数据集上的目标,但当我将其放大到较大的数据集时,我发现了向量错误:(matA和matB都是
0
,因此确实没有其他选择。不,如果我运行您的代码
matA[1,3]
0
matB[1,3]
1
,而
matC[1,3]
0
。嗯。我不是在一台有R的机器上,但我的wetware执行符合您的逻辑。可能[]中的条件应该是
matA==0 | matB==0
。未测试。
mnel <- function(matA, matB) {
  (matB * !matA) + matA
}

# method takes a function as the argument
mcombine <- function(matA, matB, method) {
  chunkSize <- 10000
  matC <- matrix(0, nrow(matA), ncol(matA))
  for (i in 1:floor(nrow(matA) / chunkSize)) {
    curRange <- (chunkSize * (i-1) + 1):(i * chunkSize)
    matC[curRange,] <- method(matA[curRange,], matB[curRange,])
  }
  # handle case where dimensions don't divide exactly into chunks
  lastRange <- i*chunkSize:nrow(matA)
  matC[lastRange,] <- method(matA[lastRange,], matB[lastRange,])
  matC
}

# Using mnel's method:
matC <- mcombine(matA, matB, mnel)