【问题标题】:Check length of matrix elements检查矩阵元素的长度
【发布时间】:2017-07-17 13:47:07
【问题描述】:
mat <- structure(list(c(1, 2, 3, 4, 5), 2, c(3, 2, 1), numeric(0), numeric(0), 
    numeric(0), c(1, 2, 3, 6), c(1, 2, 3, 4, 5), 1, numeric(0), 
    numeric(0), numeric(0), c(3, 4, 2), 3, c(1, 2, 3, 4, 5), 
    numeric(0), numeric(0), numeric(0), numeric(0), numeric(0), 
    numeric(0), 1.358, numeric(0), numeric(0), numeric(0), numeric(0), 
    numeric(0), numeric(0), 0.0223257970827299, numeric(0), numeric(0), 
    numeric(0), numeric(0), numeric(0), numeric(0), 1.493), .Dim = c(6L, 
6L))

> mat
     [,1]      [,2]      [,3]      [,4]      [,5]      [,6]     
[1,] Numeric,5 Numeric,4 Numeric,3 Numeric,0 Numeric,0 Numeric,0
[2,] 2         Numeric,5 3         Numeric,0 Numeric,0 Numeric,0
[3,] Numeric,3 1         Numeric,5 Numeric,0 Numeric,0 Numeric,0
[4,] Numeric,0 Numeric,0 Numeric,0 1.358     Numeric,0 Numeric,0
[5,] Numeric,0 Numeric,0 Numeric,0 Numeric,0 0.0223258 Numeric,0
[6,] Numeric,0 Numeric,0 Numeric,0 Numeric,0 Numeric,0 1.493 

我有一个 6x6 矩阵对象。每个单元格都包含一个数值列表。我想在mat 对象中找到索引i, j,它指示哪些单元格包含> 1 但

我尝试过使用which(length(mat) &lt; 5 &amp; length(mat) &gt; 1),但没有奏效。

【问题讨论】:

  • fwiw lengths(mat) 给你一个简单的长度向量
  • 所以我猜只是which(lengths(mat) &lt; 5 &amp; lengths(mat) &gt; 1, arr.ind = TRUE)
  • @Sotos lengths 只会返回一个向量。这两条线将起作用:lMat &lt;- matrix(lengths(mat), 6),然后是which(lMat &gt; 1 &amp; lMat &lt; 5, arr.ind=TRUE)
  • @Sotos arr.ind 对矢量无效
  • 所以我猜是版本问题。

标签: r matrix


【解决方案1】:

一个可能的解决方案是使用 base R 中的lengths。由于在这种情况下有各种关于输出什么的 cmets,这里是我的详细信息,

sessionInfo()
R version 3.4.1 (2017-06-30)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows >= 8 x64 (build 9200)

 mat
     [,1]      [,2]      [,3]      [,4]      [,5]      [,6]     
[1,] Numeric,5 Numeric,4 Numeric,3 Numeric,0 Numeric,0 Numeric,0
[2,] 2         Numeric,5 3         Numeric,0 Numeric,0 Numeric,0
[3,] Numeric,3 1         Numeric,5 Numeric,0 Numeric,0 Numeric,0
[4,] Numeric,0 Numeric,0 Numeric,0 1.358     Numeric,0 Numeric,0
[5,] Numeric,0 Numeric,0 Numeric,0 Numeric,0 0.0223258 Numeric,0
[6,] Numeric,0 Numeric,0 Numeric,0 Numeric,0 Numeric,0 1.493    

str(mat)
List of 36
 $ : num [1:5] 1 2 3 4 5
 $ : num 2
 $ : num [1:3] 3 2 1
 $ : num(0) 
 $ : num(0) 
 $ : num(0) 
 $ : num [1:4] 1 2 3 6
 $ : num [1:5] 1 2 3 4 5
 $ : num 1
 $ : num(0) 
 $ : num(0) 
 $ : num(0) 
 $ : num [1:3] 3 4 2
 $ : num 3
 $ : num [1:5] 1 2 3 4 5
 $ : num(0) 
 $ : num(0) 
 $ : num(0) 
 $ : num(0) 
 $ : num(0) 
 $ : num(0) 
 $ : num 1.36
 $ : num(0) 
 $ : num(0) 
 $ : num(0) 
 $ : num(0) 
 $ : num(0) 
 $ : num(0) 
 $ : num 0.0223
 $ : num(0) 
 $ : num(0) 
 $ : num(0) 
 $ : num(0) 
 $ : num(0) 
 $ : num(0) 
 $ : num 1.49
 - attr(*, "dim")= int [1:2] 6 6

那么,

m2 <- lengths(mat)
which(m2 < 5 & m2 > 1, arr.ind = TRUE)
#     row col
#[1,]   3   1
#[2,]   1   2
#[3,]   1   3

如果您使用的是

m1 <- matrix(lengths(mat), 6) 
which(m1 > 1 & m1 < 5, arr.ind=TRUE)

【讨论】:

    【解决方案2】:

    这是我的解决方案:

    lMat <- apply(mat, 2, function(x) sapply(x, length))
    which(lMat > 1 & lMat < 5, arr.ind = TRUE)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-08-30
      • 2020-04-08
      • 1970-01-01
      • 2015-12-29
      • 1970-01-01
      • 2016-09-06
      • 1970-01-01
      相关资源
      最近更新 更多