Let's say I have a multi-dimensional array called pi
, and its number of dimensions isn't known until the runtime:
假设我有一个多维数组叫pi,它的维数直到运行时才知道:
dims <- rep(3, dim_count)
pi <- array(0, dims)
As you can see the dimension count depends on dim_count
. How do I retrieve a value from the array when I have a vector of the indexes? For example when I have:
可以看到,维度计数取决于dim_count。当我有一个索引向量时,如何从数组中检索值?例如当我有:
dim_count <- 5
indexes <- c(1, 2, 3, 3, 3)
I want to retrieve
我想检索
pi[1, 2, 3, 3, 3]
Is there a short, effective and hopefully elegant way of doing this?
是否有一种简短、有效、有希望的优雅的方法来实现这一目标?
3 个解决方案
#1
13
Making use of a little known usage of [
:
利用一些已知的用法[:
When indexing arrays by
[
a single argumenti
can be a matrix with as many columns as there are dimensions ofx
; the result is then a vector with elements corresponding to the sets of indices in each row ofi
.当通过[单个参数对数组进行索引时,我可以是一个矩阵,其列数与x的维数相等;结果是一个向量,其元素对应于i的每一行中的索引集。
you can simply do:
你可以做的:
pi[matrix(indexes, 1)]
#2
9
do.call("[",...)
seems to work.
do.call(“(”,…)似乎工作。
indexes <- c(1,2,3,3,3)
pi[1,2,3,3,3] <- 17 ## so we know if we succeeded or not
do.call("[",c(list(pi),as.list(indexes)))
Note that your example wouldn't work -- your dimensions were all 3, but some of your index elements were >3 ...
注意,您的示例不起作用——您的维度都是3,但是您的一些索引元素是>3……
#3
4
do.call()
is an option:
do.call()是一个选项:
dim_count <- 5
indexes <- c(1, 2, 2, 2, 3)
dims <- rep(3, dim_count)
pi <- array(seq_len(prod(dims)), dims)
do.call(`[`, c(list(x = pi), as.list(indexes)))
Which gives:
这使:
> do.call(`[`, c(list(x = pi), as.list(indexes)))
[1] 202
> pi[1, 2, 2, 2, 3]
[1] 202
The tricky bit is getting the list of arguments in the right format. pi
should be the first argument to "["
(or named as argument x
, see ?"["
), whilst we want each element of indexes
itself to be a component of the supplied list, not a vector within that list. Hence the convoluted c(list(x = pi), as.list(indexes))
.
棘手的一点是要以正确的格式获取参数列表。pi应该是第一个到"["(或命名为参数x,参见?"[])的参数,同时我们希望索引本身的每个元素都是所提供列表的组件,而不是列表中的向量。因此出现了卷积c(list(x = pi), as.list(indexes))。
An alternative way to construct the argument list which might be easier to follow is:
另一种构建参数列表的方法是:
ARGS <- vector("list", length = dim_count + 1)
ARGS[[1]] <- pi
ARGS[2:length(ARGS)] <- indexes
do.call("[", ARGS)
which gives
这给了
> do.call("[", ARGS)
[1] 202
> pi[1, 2, 2, 2, 3]
[1] 202
#1
13
Making use of a little known usage of [
:
利用一些已知的用法[:
When indexing arrays by
[
a single argumenti
can be a matrix with as many columns as there are dimensions ofx
; the result is then a vector with elements corresponding to the sets of indices in each row ofi
.当通过[单个参数对数组进行索引时,我可以是一个矩阵,其列数与x的维数相等;结果是一个向量,其元素对应于i的每一行中的索引集。
you can simply do:
你可以做的:
pi[matrix(indexes, 1)]
#2
9
do.call("[",...)
seems to work.
do.call(“(”,…)似乎工作。
indexes <- c(1,2,3,3,3)
pi[1,2,3,3,3] <- 17 ## so we know if we succeeded or not
do.call("[",c(list(pi),as.list(indexes)))
Note that your example wouldn't work -- your dimensions were all 3, but some of your index elements were >3 ...
注意,您的示例不起作用——您的维度都是3,但是您的一些索引元素是>3……
#3
4
do.call()
is an option:
do.call()是一个选项:
dim_count <- 5
indexes <- c(1, 2, 2, 2, 3)
dims <- rep(3, dim_count)
pi <- array(seq_len(prod(dims)), dims)
do.call(`[`, c(list(x = pi), as.list(indexes)))
Which gives:
这使:
> do.call(`[`, c(list(x = pi), as.list(indexes)))
[1] 202
> pi[1, 2, 2, 2, 3]
[1] 202
The tricky bit is getting the list of arguments in the right format. pi
should be the first argument to "["
(or named as argument x
, see ?"["
), whilst we want each element of indexes
itself to be a component of the supplied list, not a vector within that list. Hence the convoluted c(list(x = pi), as.list(indexes))
.
棘手的一点是要以正确的格式获取参数列表。pi应该是第一个到"["(或命名为参数x,参见?"[])的参数,同时我们希望索引本身的每个元素都是所提供列表的组件,而不是列表中的向量。因此出现了卷积c(list(x = pi), as.list(indexes))。
An alternative way to construct the argument list which might be easier to follow is:
另一种构建参数列表的方法是:
ARGS <- vector("list", length = dim_count + 1)
ARGS[[1]] <- pi
ARGS[2:length(ARGS)] <- indexes
do.call("[", ARGS)
which gives
这给了
> do.call("[", ARGS)
[1] 202
> pi[1, 2, 2, 2, 3]
[1] 202