繁体   English   中英

如何获得矩阵中两列中值的绝对差值

[英]How to get the absolute difference between values in two columns in a matrix

我有一个如下的矩阵

      i j value
 [1,] 3 6 0.194201129
 [2,] 3 5 0.164547043
 [3,] 3 4 0.107149279
 [4,] 4 3 0.004927017
 [5,] 3 1 0.080454448
 [6,] 1 2 0.003220612
 [7,] 2 6 0.162313646
 [8,] 3 3 0.114992628
 [9,] 4 1 0.015337253
[10,] 1 6 0.026550051
[11,] 3 2 0.057004116
[12,] 4 2 0.006441224
[13,] 4 5 0.025641026
[14,] 2 4 0.004885993
[15,] 1 1 0.036552785
[16,] 1 5 0.048249186
[17,] 1 4 0.006053565
[18,] 1 3 0.004970296

正如你可以看到的一些i, j对有一个逆对。 例如,对于i = 3, j = 1 ,存在i = 1, j = 3

这就是我想要实现的目标。

对于每个i, j对,减去其倒数值并得到减法的绝对值。 对于那些没有逆对的对,从它们中减去0。

以下是几个例子:

对于i = 3, j = 5 ,没有逆对(i = 5,j = 3),因此计算变为:

abs(0.164547043 - 0)

对于i = 3, j = 1 ,矩阵上有一个逆对,其中i = 1, j = 3 ,因此计算将是:

abs(0.004970296 - 0.080454448)

我通过编写一堆完整的for循环代码(65行)来解决这个问题,这很难阅读和编辑。

所以我想知道是否有更高效的方法来做这样的事情,通过使用更紧凑的功能。

由前一篇文章推动,其答案非常简单(通过使用aggregate()函数)并通过在线搜索这些函数,我试图在这里使用mapply(),但事实是我无法处理逆对。

编辑:

dput()
    memMatrix <- structure(c(3, 3, 3, 4, 3, 1, 2, 3, 4, 1, 3, 4, 4, 2, 1, 1, 1, 
        1, 6, 5, 4, 3, 1, 2, 6, 3, 1, 6, 2, 2, 5, 4, 1, 5, 4, 3, 0.194201128983738, 
        0.164547043451226, 0.107149278958536, 0.00492701677834917, 0.0804544476798398, 
        0.00322061191626409, 0.162313646044361, 0.114992627755601, 0.0153372534398016, 
        0.0265500506171091, 0.0570041160347523, 0.00644122383252818, 
        0.0256410256410256, 0.00488599348534202, 0.0365527853282693, 
        0.0482491856677524, 0.0060535654765406, 0.00497029586494912), .Dim = c(18L, 
        3L), .Dimnames = list(NULL, c("i", "j", "value")))

此外,这里的代码到目前为止工作,但它更复杂

memMatrix是帖子顶部给出的矩阵。 在这里你可以看到一点点不同,我将absolut值与一个名为probability_distribution的变量相乘,但这并不重要。 我从最初的帖子中通过它(multiplcation)使其更简单。

subFunc <- function( memMatrix , probability_distribution )
{

  # Node specific edge relevance matrix
  node_edgeRelm <- matrix(ncol = 3)
  colnames(node_edgeRelm) <- c("i","j","rel")
  node_edgeRelm <- na.omit(node_edgeRelm)

  for ( row in 1:nrow( memMatrix ) )
  {
    pair_i <- memMatrix[row,"i"]
    pair_j <- memMatrix[row,"j"]

    # If already this pair of i and j has been calculated continue with the next pair
    # At the end of a new calculation, we store the i,j (verse) values in order from lower to higher
    # and then we check here for the inverse j,i values (if exists).
    if( pair_i < pair_j )
      if( any(node_edgeRelm[,"i"] == pair_i & node_edgeRelm[,"j"] == pair_j) ) next
    if( pair_j < pair_i )
      if( any(node_edgeRelm[,"i"] == pair_j & node_edgeRelm[,"j"] == pair_i) ) next

    # Verse i,j
    mepm_ij <- as.numeric( memMatrix[which( memMatrix[,"i"] == pair_i & memMatrix[,"j"] == pair_j ), "mep"] )
    if( length(mepm_ij) == 0 )
      mepm_ij <- 0
    # Inverse j,i
    mepm_ji <- as.numeric( memMatrix[which( memMatrix[,"i"] == pair_j & memMatrix[,"j"] == pair_i ), "mep"] )
    if( length(mepm_ji) == 0 )
      mepm_ji <- 0

    # Calculate the edge relevance for that specific initial node x and pair i,j
    edge_relevance <- probability_distribution * abs( mepm_ij - mepm_ji )

    # Store that specific edge relevance with an order from lower to higher node
    if ( pair_i < pair_j)
      node_edgeRelm <- rbind( node_edgeRelm, c( as.numeric(pair_i), as.numeric(pair_j), as.numeric(edge_relevance) ) )
    else
      node_edgeRelm <- rbind( node_edgeRelm, c( as.numeric(pair_j), as.numeric(pair_i), as.numeric(edge_relevance) ) )
  }

  na.omit(node_edgeRelm)
}

你可以把它作为subFunc(memMatrix, 1/3)运行subFunc(memMatrix, 1/3)

假设输入是矩阵m组,则value元素由具有相同i,j或j,i的那些元素组成。 每个这样的组中将有1或2个value元素,因此对于任何特定组,将1或2长度向量附加零并取前2个元素,将得到的2个元素向量的元素区分开并取绝对值。 此过程不会更改行顺序。 它给出了一个数据框,但如果需要使用as.matrix它可以转换回矩阵。 没有使用包裹。

absdiff <- function(x) abs(diff(c(x, 0)[1:2]))
transform(m, value = ave(value, pmin(i, j), pmax(i, j), FUN = absdiff))

赠送:

   i j       value
1  3 6 0.194201129
2  3 5 0.164547043
3  3 4 0.102222262
4  4 3 0.102222262
5  3 1 0.075484152
6  1 2 0.003220612
7  2 6 0.162313646
8  3 3 0.114992628
9  4 1 0.009283688
10 1 6 0.026550051
11 3 2 0.057004116
12 4 2 0.001555230
13 4 5 0.025641026
14 2 4 0.001555230
15 1 1 0.036552785
16 1 5 0.048249186
17 1 4 0.009283688
18 1 3 0.075484152

这是一个使用library(purr)的解决方案,使match()在列表上工作

library(purrr)

创建一个在列表上运行的match

match2 = as_mapper(match)

创建一个列表,其中包含长度为2且包含两个值的向量,然后是第二个列表,其值为reverse,然后匹配这两个列表

i = match2(L <- map2(df[,1], df[,2], c),
                map(L, rev))

提取匹配索引的第三列

 v = df[i,3]

替换NA /不匹配0,做减法然后abs()

cbind(df, abs(df[,3]-replace(v, is.na(v), 0)))

您可以尝试一个整合的解决方案:

library(tidyverse)
df %>% as.tibble() %>% 
  rowwise() %>% 
  mutate(id=paste(sort(c(i,j)), collapse = "_"))  %>% 
  group_by(id) %>% 
  mutate(n=paste0("n", 1:n())) %>% 
  select(-1,-2) %>% 
  spread(n, value, fill = 0) %>% 
  mutate(result=abs(n1-n2))
# A tibble: 14 x 4
# Groups:   id [14]
      id          n1          n2      result
   <chr>       <dbl>       <dbl>       <dbl>
 1   1_1 0.036552785 0.000000000 0.036552785
 2   1_2 0.003220612 0.000000000 0.003220612
 3   1_3 0.080454448 0.004970296 0.075484152
 4   1_4 0.015337253 0.006053565 0.009283688
 5   1_5 0.048249186 0.000000000 0.048249186
 6   1_6 0.026550051 0.000000000 0.026550051
 7   2_3 0.057004116 0.000000000 0.057004116
 8   2_4 0.006441224 0.004885993 0.001555230
 9   2_6 0.162313646 0.000000000 0.162313646
10   3_3 0.114992628 0.000000000 0.114992628
11   3_4 0.107149279 0.004927017 0.102222262
12   3_5 0.164547043 0.000000000 0.164547043
13   3_6 0.194201129 0.000000000 0.194201129
14   4_5 0.025641026 0.000000000 0.025641026

这个想法是:

  1. 按行ij排序并在新列id粘贴在一起。
  2. id分组并添加出现次数n
  3. n传播
  4. 计算绝对差值。

Base R:Lte说矩阵的名称是mat

> B=matrix(0,max(mat[,1:2]),max(mat[,1:2]))
> B[mat[,1:2]]=mat[,3]
> A=cbind(which(upper.tri(B,T),T),abs(`diag<-`(B,0)[upper.tri(B,T)]-t(B)[upper.tri(B,T)]))
> A[A[,3]>0,]
      row col            
 [1,]   1   1 0.036552785
 [2,]   1   2 0.003220612
 [3,]   1   3 0.075484152
 [4,]   2   3 0.057004116
 [5,]   3   3 0.114992628
 [6,]   1   4 0.009283688
 [7,]   2   4 0.001555230
 [8,]   3   4 0.102222262
 [9,]   1   5 0.048249186
[10,]   3   5 0.164547043
[11,]   4   5 0.025641026
[12,]   1   6 0.026550051
[13,]   2   6 0.162313646
[14,]   3   6 0.194201129

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM