检查矩阵元素的长度(Check length of matrix elements)

编程入门 行业动态 更新时间:2024-10-26 16:21:35
检查矩阵元素的长度(Check length of matrix elements) 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但<5个元素长的向量。 换句话说,我希望看到指数:(1,2),(1,3)和(3,1)。

我已经尝试使用which(length(mat) < 5 & length(mat) > 1)但这不起作用。

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

I have a 6x6 matrix object. Each cell contains a list of numeric values. I would like to find the indices i, j in the mat object that indicate which cells contain a vector that is > 1 but < 5 elements long. In other words, I would like to see the indices: (1, 2), (1, 3), and (3, 1).

I've tried using which(length(mat) < 5 & length(mat) > 1) but that didn't work.

最满意答案

一个可能的解决方案是使用基数R的lengths 。由于在这种情况下有什么输出什么的各种评论,这里是我的细节,

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

如果您使用的是版本<3.4.1,那么两步法(如@lmo提出的那样)将起作用,即

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

A possible solution is to use lengths from base R. As there are various comments about what outputs what in this case, here are my details,

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

Then,

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

In case you are using a version < 3.4.1, then a two step approach (as the one proposed by @lmo) will work, i.e.

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

更多推荐

本文发布于:2023-08-08 00:18:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1466731.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:矩阵   长度   元素   Check   matrix

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!