如何在R中创建多维函数数组?

时间:2022-02-17 21:35:50

I have a function in 3 dimensions and I want to make a 2D table of pre-computed linear interpolations for the third variable. Ie, given the values for the first two variables, look up the linear interpolation for the third variable. How can I build a multi-dimensional (in this case 2) data structure that will hold those functions and can be indexed.

我有3个维度的函数,我想为第三个变量制作预先计算的线性插值的2D表。即,给定前两个变量的值,查找第三个变量的线性插值。如何构建一个多维(在这种情况下为2)数据结构,该数据结构将保存这些函数并可以编制索引。

For example, Id like to interact with the structure as so:

例如,Id喜欢与结构交互,如下所示:

a_func <-function (a, b, c) {...}

for(x in 1:n) {
    for(y in 1:m) {
        some_structure[x][y] <- approxfun(seq(0,q,by=0.01), sapply(seq(0,q,by=0.01), FUN=a_func, a=y, b=x))
    }
}

,

some_structure[_x][_y](z) #and then index it like this 

1 个解决方案

#1


1  

This is not the most space efficient way to go, but this is how I get around holding weird variables in multiple dimensions: This will return a list (holding nothing) with the dimensions that you want it to hold

这不是最节省空间的方法,但这就是我如何在多个维度中保持奇怪的变量:这将返回一个列表(不保留任何内容),其中包含您希望它保留的维度

  list.arr <- function(dimensions) {
    result <- list()
    length(result) <- prod(dimensions)
    dim(result) <- dimensions
    return(result)
  }

to access: lst <- list.arr(c(4,4)) lst[[x,y]] #you need to use double braces and you can assigned with: lst[[x,y]] <- ANY_R_OBJECT

访问:lst < - list.arr(c(4,4))lst [[x,y]]#你需要使用双括号,你可以分配:lst [[x,y]] < - ANY_R_OBJECT

EDIT: This is actually just a regular 1-D list with a dimension attribute on it meaning that when you call lst[[x]] it will return just the xth variable and not the row. If you want the row you need to call lst[[x,]]

编辑:这实际上只是一个带有维度属性的常规1-D列表,这意味着当你调用lst [[x]]时,它将只返回第x个变量而不是行。如果你想要行你需要调用lst [[x,]]

Additionally, you can now name the object with names(lst), dimnames(lst), col.names(lst), row.names(lst).

此外,您现在可以使用名称(lst),dimnames(lst),col.names(lst),row.names(lst)命名对象。

#1


1  

This is not the most space efficient way to go, but this is how I get around holding weird variables in multiple dimensions: This will return a list (holding nothing) with the dimensions that you want it to hold

这不是最节省空间的方法,但这就是我如何在多个维度中保持奇怪的变量:这将返回一个列表(不保留任何内容),其中包含您希望它保留的维度

  list.arr <- function(dimensions) {
    result <- list()
    length(result) <- prod(dimensions)
    dim(result) <- dimensions
    return(result)
  }

to access: lst <- list.arr(c(4,4)) lst[[x,y]] #you need to use double braces and you can assigned with: lst[[x,y]] <- ANY_R_OBJECT

访问:lst < - list.arr(c(4,4))lst [[x,y]]#你需要使用双括号,你可以分配:lst [[x,y]] < - ANY_R_OBJECT

EDIT: This is actually just a regular 1-D list with a dimension attribute on it meaning that when you call lst[[x]] it will return just the xth variable and not the row. If you want the row you need to call lst[[x,]]

编辑:这实际上只是一个带有维度属性的常规1-D列表,这意味着当你调用lst [[x]]时,它将只返回第x个变量而不是行。如果你想要行你需要调用lst [[x,]]

Additionally, you can now name the object with names(lst), dimnames(lst), col.names(lst), row.names(lst).

此外,您现在可以使用名称(lst),dimnames(lst),col.names(lst),row.names(lst)命名对象。