[英]element-wise calculation with arrays (tensors)
I have a 3-dimensional array x
and want to calculate the share a
of each element w.r.t.我有一个 3 维数组
x
并想计算每个元素 w.r.t 的份额a
。 the 3rd dimension:第三维度:
I tried the following in R but this does not work due to "non-conformable arrays" (which makes sense of course).我在 R 中尝试了以下操作,但这由于“不一致的数组”而不起作用(这当然是有道理的)。
x <- array(
data = 1:8,
dim = c(2, 2, 2),
dimnames = list(i = c("i1", "i2"), j = c("j1", "j2"), k = c("k1", "k2")))
a <- x / apply(x, c(1, 2), sum)
So I am looking for a way to perform tensor calculations where I can specify the dimensions on which to perform the division.所以我正在寻找一种方法来执行张量计算,我可以在其中指定执行除法的维度。 This is
x
and the sum as I already have it and a
as I want it ( a
shown with fractions here):这是
x
和我已经拥有的总和以及我想要a
a
此处显示为小数):
> x
, , k = k1
j
i j1 j2
i1 1 3
i2 2 4
, , k = k2
j
i j1 j2
i1 5 7
i2 6 8
> apply(x, c(1, 2), sum)
j
i j1 j2
i1 6 10
i2 8 12
> a
, , k = k1
j
i j1 j2
i1 1/6 3/10
i2 2/8 4/12
, , k = k2
j
i j1 j2
i1 5/6 7/10
i2 6/8 8/12
I'm grateful for any hints!我很感激任何提示!
Try proportions
试试
proportions
> proportions(x,1:2)
, , k = k1
j
i j1 j2
i1 0.1666667 0.3000000
i2 0.2500000 0.3333333
, , k = k2
j
i j1 j2
i1 0.8333333 0.7000000
i2 0.7500000 0.6666667
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.