查找多维结构(数组)中的位置列表

编程入门 行业动态 更新时间:2024-10-11 13:31:03
本文介绍了查找多维结构(数组)中的位置列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

假设我有一个简单的多维结构,像这样:

somestr<-array(sample.int(2, 120, replace=TRUE), dim=c(4,5,6))

我正在查找结构中的所有位置(在本例中为数组),该位置的值等于2,例如,对于我的示例,请注意.结构可能也包含字符或逻辑.就目前而言,它将能够找到等于给定值的所有值,但是将其扩展到可以应用于结构中每个项目的任何逻辑值函数(这将允许例如is.na被使用).

我想得到的是一个(integer)矩阵,其列的大小与somestr一样(在这种情况下为3),并且行数(取决于上面的sample.int调用)与有等于给定值(2)的值.新矩阵中的值是somestr中的坐标",其中值等于2.

我很抱歉将我的示例与说明混在一起,但我希望这样会更清楚.作为记录:我可以自己制作(甚至可以回答我自己的问题),但是我希望有一个标准化的解决方案(阅读:某些软件包中的现成函数),或者在学习过程中学习新的技巧. >

因此,简而言之,您可以编写一个函数

posOf<-function(somestr, valueToCompareTo)

返回等于valueToCompareTo的somestr中位置的矩阵,如果valueToCompareTo是一个函数,则应用此函数的somestr中的位置将返回TRUE.

解决方案

我认为which函数可以做到:

which(somestr==2, arr.ind=TRUE)

(如果我正确理解所有内容)

R> set.seed(123) R> somestr <- array(sample.int(2, 120, replace=TRUE), dim=c(4,5,6)) R> somestr , , 1 [,1] [,2] [,3] [,4] [,5] [1,] 1 2 2 2 1 [2,] 2 1 1 2 1 [3,] 1 2 2 1 1 [4,] 2 2 1 2 2 ... , , 6 [,1] [,2] [,3] [,4] [,5] [1,] 2 1 1 1 2 [2,] 1 2 1 2 2 [3,] 1 2 2 2 2 [4,] 2 2 1 1 1 R> which(somestr==2, arr.ind=TRUE) dim1 dim2 dim3 [1,] 2 1 1 [2,] 4 1 1 [3,] 1 2 1 [4,] 3 2 1 [5,] 4 2 1 ... [57,] 2 5 6 [58,] 3 5 6

Suppose I have a simple multidimensional structure, like this one:

somestr<-array(sample.int(2, 120, replace=TRUE), dim=c(4,5,6))

I'm looking for all positions in the structure (in this case, an array) where the value is equal to, say for my example, 2. Note that the structure might just as well hold characters or logicals. For now, it will do to just find all values equal to a given, but it would be nice to extend the idea to any logical-valued function that can be applied to each item in the structure (That would allow e.g. is.nato be used).

What I would like to get, is an (integer) matrix with as many columns as somestr has dimensions (in this case 3), and as many rows (depends on the sample.int call above) as there are values equal to the given value (2). The values in this new matrix are the 'coordinates' within somestr where the values are equal to 2.

I apologize for mixing my example with the explanation, but I was hoping it would be clearer that way. For the record: I'm able to produce this myself (may even answer my own question), but I was hoping for a standardized solution (read: a readymade function in some package), or learn new tricks along the way.

So, in short, can you write a function

posOf<-function(somestr, valueToCompareTo)

that returns a matrix of the positions in somestr equal to valueToCompareTo, and if valueToCompareTo is a function, the positions in somestr for which applying this function returns TRUE.

解决方案

I think the which function can do that:

which(somestr==2, arr.ind=TRUE)

(if I understood everything correctly)

R> set.seed(123) R> somestr <- array(sample.int(2, 120, replace=TRUE), dim=c(4,5,6)) R> somestr , , 1 [,1] [,2] [,3] [,4] [,5] [1,] 1 2 2 2 1 [2,] 2 1 1 2 1 [3,] 1 2 2 1 1 [4,] 2 2 1 2 2 ... , , 6 [,1] [,2] [,3] [,4] [,5] [1,] 2 1 1 1 2 [2,] 1 2 1 2 2 [3,] 1 2 2 2 2 [4,] 2 2 1 1 1 R> which(somestr==2, arr.ind=TRUE) dim1 dim2 dim3 [1,] 2 1 1 [2,] 4 1 1 [3,] 1 2 1 [4,] 3 2 1 [5,] 4 2 1 ... [57,] 2 5 6 [58,] 3 5 6

更多推荐

查找多维结构(数组)中的位置列表

本文发布于:2023-11-30 15:54:30,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1650592.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:多维   数组   位置   结构   列表

发布评论

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

>www.elefans.com

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