查找多维结构(数组)中的位置列表 [英] Finding list of positions in multidimensional structure (array)

查看:207
本文介绍了查找多维结构(数组)中的位置列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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)

返回等于valueToCompareTosomestr中位置的矩阵,如果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

这篇关于查找多维结构(数组)中的位置列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆