3D阵列->申请-> 3D阵列 [英] 3D array -> apply -> 3D array

查看:57
本文介绍了3D阵列->申请-> 3D阵列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

仅在一个空白处操作,看来apply不会重新组装3D阵列.考虑:

It seems apply will not re-assemble 3D arrays when operating on just one margin. Consider:

 arr <- array(
  runif(2*4*3), 
  dim=c(2, 4, 3), 
  dimnames=list(a=paste0("a", 1:2), b=paste0("b", 1:4), c=paste0("c", 1:3))
)
# , , c = c1
# 
#     b
# a           b1        b2        b3        b4
#   a1 0.7321399 0.8851802 0.2469866 0.9307044
#   a2 0.5896138 0.6183046 0.7732842 0.6652637
# 
# , , c = c2
#     b
# a           b1        b2        b3         b4
#   a1 0.5894680 0.7839048 0.3854357 0.56555024
#   a2 0.6158995 0.6530224 0.8401427 0.04044974
# 
# , , c = c3
#     b
# a           b1        b2         b3        b4
#   a1 0.3500653 0.7052743 0.42487635 0.5689287
#   a2 0.4097346 0.4527939 0.07192528 0.8638655

现在,制作一个4 x 4的矩阵以随机排列arr[, , i]中的各列,并使用apply矩阵乘以arr中的每个a*b子矩阵以重新排列其列.重要的是,每个apply迭代的结果都是一个矩阵

Now, make a 4 x 4 matrix to shuffle columns around in each of arr[, , i], and use apply to matrix multiply each a*b sub-matrix in arr to re-order their columns. The important point is that the result of each apply iteration is a matrix

cols.shuf.mx <- matrix(c(0,1,0,0,1,0,0,0,0,0,0,1,0,0,1,0), ncol=4)
apply(arr, 3, `%*%`, cols.shuf.mx)
#         c
#             c1         c2         c3
# [1,] 0.8851802 0.78390483 0.70527431
# [2,] 0.6183046 0.65302236 0.45279387
# [3,] 0.7321399 0.58946800 0.35006532
# [4,] 0.5896138 0.61589947 0.40973463
# [5,] 0.9307044 0.56555024 0.56892870
# [6,] 0.6652637 0.04044974 0.86386552
# [7,] 0.2469866 0.38543569 0.42487635
# [8,] 0.7732842 0.84014275 0.07192528

我希望结果是:

# , , c = c1
#    
# a            1         2         3         4
#   a1 0.8851802 0.7321399 0.9307044 0.2469866
#   a2 0.6183046 0.5896138 0.6652637 0.7732842
#
# , , c = c2
#    
# a            1         2          3         4
#   a1 0.7839048 0.5894680 0.56555024 0.3854357
#   a2 0.6530224 0.6158995 0.04044974 0.8401427
#
# , , c = c3
#   
# a            1         2         3          4
#   a1 0.7052743 0.3500653 0.5689287 0.42487635
#   a2 0.4527939 0.4097346 0.8638655 0.07192528

我可以使用plyr::aaply通过以下方式获得预期的结果:

I can get the expected result with plyr::aaply with:

aperm(aaply(arr, 3, `%*%`, cols.shuf.mx), c(2, 3, 1))

但想知道是否有简单基本方法来实现此结果(即,我是否在这里缺少明显的东西以获得期望的结果).

but was wondering if there is a simple base way to achieve this result (i.e. am I missing something obvious here to get the desired outcome).

我意识到这里发生的是记录在案的内容(If each call to FUN returns a vector of length n, then apply returns an array of dimension c(n, dim(X)[MARGIN]) if n > 1),但对我来说仍然很奇怪,如果函数返回具有尺寸的对象,则基本上将它们忽略.

I realize what occurs here is what is documented (If each call to FUN returns a vector of length n, then apply returns an array of dimension c(n, dim(X)[MARGIN]) if n > 1), but it still seems weird to me that if a function returns an object with dimensions they are basically ignored.

推荐答案

这是一个不太理想的解决方案,需要预先了解函数结果矩阵的维数:

Here is a less than fantastic solution that requires foreknowledge of the dimensions of the function result matrix:

vapply(
  1:dim(arr)[3], 
  function(x, y) arr[,,x] %*% y, 
  FUN.VALUE=arr[,,1], 
  y=cols.shuf.mx
) 

这篇关于3D阵列-&gt;申请-&gt; 3D阵列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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