0

假设我在 3D 空间的不同位置进行了一组测量。测量的位置具有坐标向量

x <- c(0,1,2,3)
y <- c(4,5,6)
z <- c(7,8)

因此,例如,最接近原点的测量是在 location= 完成的(0,4,7)。从上面的坐标向量中,我想创建一个 3D 数组——而且只有一个数组。@bnaul:这些是体素中心的坐标,我想为其赋值。我的意图是,在 pcode 中,

arr <- magic( c(0,1,2,3) , c(4,5,6) , c(7,8) )
# arr is now a 3D array filled with NAs
value1 -> arr[0, 4, 7]
value2 -> arr[3, 5, 7]
# and so on, but if one does
valueBad -> arr[4,3,2] # one should get an error, as should, e.g.,
valueBad2 -> arr[3,4,5]

但我怀疑我已经“在 NetCDF 中思考”太久了:基本上我上面想做的是将坐标分配给一个数组,我不相信在 R 中可以做到这一点。

4

3 回答 3

1
# starting data
x <- c(0,1,2,3)
y <- c(4,5,6)
z <- c(7,8)

# find every combo
w <- expand.grid( x , y , z )

# convert to a matrix
v <- as.matrix( w )

# view your result
v
于 2013-02-06T23:17:34.237 回答
1

或者,这也可能有所帮助。请澄清您想要的结果:)

# starting data
x <- c(0,1,2,3)
y <- c(4,5,6)
z <- c(7,8)

# create a 4 x 3 x 2 array
v <- 
    array( 
        # start out everything as missing..
        NA , 
        # ..and make the lengths of the dimensions the three lengths.
        dim = 
            c( length( x ) , length( y ) , length( z ) ) 
    )

# view your result
v

# now populate it with something..
# for now, just populate it with 1:24
v[ , , ] <- 1:length(v)

# view your result again
v
于 2013-02-06T23:32:05.750 回答
0
 array(NA, dim=c(4,3,2), 
   dimnames=list( x = c(0,1,2,3),
     y = c(4,5,6),
     z = c(7,8) ) )
, , z = 7

   y
x    4  5  6
  0 NA NA NA
  1 NA NA NA
  2 NA NA NA
  3 NA NA NA

, , z = 8

   y
x    4  5  6
  0 NA NA NA
  1 NA NA NA
  2 NA NA NA
  3 NA NA NA
于 2013-02-07T00:28:26.900 回答