[英]How do I calculate the sum of products of entries of a 2D and 3D array?
Let $X \in \mathbb{R}^{M\times N}$ be a 2-dimensional array.设 $X \in \mathbb{R}^{M\times N}$ 为二维数组。 Let $Y \in \mathbb{R}^{M \times N \times K$ be a 3-dimensional array.
设 $Y \in \mathbb{R}^{M \times N \times K$ 是一个 3 维数组。 I have these data arrays in Julia.
我在 Julia 中有这些数据 arrays。
I want to calculate the number $$ \sum_{k = 1}^{K} \sum_{n = 1}^N \sum_{m = 1}^M X_{m,n} Y_{m, n, k}.我想计算数字 $$ \sum_{k = 1}^{K} \sum_{n = 1}^N \sum_{m = 1}^M X_{m,n} Y_{m, n, k }. $$ How can I calculate it using matrix/array product in Julia?
$$ 如何使用 Julia 中的矩阵/数组积计算它? This will be my objective function in an optimisation program.
这将是我在优化程序中的目标 function。
I know how to calculate products of vectors and matrices (2D arrays).我知道如何计算向量和矩阵(二维数组)的乘积。 However, I am not sure how the products work with 3D arrays in Julia.
但是,我不确定产品如何与 Julia 中的 3D arrays 配合使用。
You just need sum(X.*Y)
.你只需要
sum(X.*Y)
。 The .*
operator automatically will multiply things element by element, but will repeat if these is an dimension along which one of the tensors has size one and one doesn't (the third dimension in this case), ie it does exactly what you're asking for. .*
运算符会自动将元素逐个元素相乘,但如果这些是一个维度,其中一个张量的大小为 1 而另一个没有(在这种情况下为第三维),即它会完全按照您的要求进行操作'重新要求。
Assuming we have these values for the dimensions假设我们有这些尺寸值
M = 3
N = 4
K = 2
makes some X
matrix data生成一些
X
矩阵数据
X = 10 .* ones( M , N )
then make your 3D data matrix data Y
然后让你的 3D 数据矩阵数据
Y
Y = reshape( 1:M*N*K , (M , N, K) )
3×4×2 reshape(::UnitRange{Int64}, 3, 4, 2) with eltype Int64:
[:, :, 1] =
1 4 7 10
2 5 8 11
3 6 9 12
[:, :, 2] =
13 16 19 22
14 17 20 23
15 18 21 24
we can get the length of the 3rd dimension via size(Y)[3]
which produces value 2
.我们可以通过生成值
2
的size(Y)[3]
获得第 3 个维度的长度。 We can find the product of X
and the second 3d slice of Y
via我们可以通过以下方式找到
X
和Y
的第二个 3d 切片的乘积
X .* Y[:,:,2]
3×4 Matrix{Float64}:
130.0 160.0 190.0 220.0
140.0 170.0 200.0 230.0
150.0 180.0 210.0 240.0
we can get the product of X
with each 3d slice of Y
via我们可以通过
Y
的每个 3d 切片获得X
的乘积
X .* Y
3×4×2 Array{Float64, 3}:
[:, :, 1] =
10.0 40.0 70.0 100.0
20.0 50.0 80.0 110.0
30.0 60.0 90.0 120.0
[:, :, 2] =
130.0 160.0 190.0 220.0
140.0 170.0 200.0 230.0
150.0 180.0 210.0 240.0
if you want you can use a map
function to explicitly multiply X
by each of the 3rd dimension values of Y
by如果你愿意,你可以使用
map
function 明确地将X
乘以Y
的每个第三维值
XprodY1 = map( k -> X .* Y[:,:,k] , 1:size(Y)[3] )
2-element Vector{Matrix{Float64}}:
[10.0 40.0 70.0 100.0; 20.0 50.0 80.0 110.0; 30.0 60.0 90.0 120.0]
[130.0 160.0 190.0 220.0; 140.0 170.0 200.0 230.0; 150.0 180.0 210.0 240.0]
and that produces the same 2 matrices but this time each XY_k
slice is a component of a vector and not part of a 3d array but in this way you can do more manual control of the multiplications.这会产生相同的 2 个矩阵,但这次每个
XY_k
切片都是向量的一个组成部分,而不是 3d 数组的一部分,但通过这种方式,您可以对乘法进行更多的手动控制。
To get the sum of the matrix multiplications across dimension K
(3rd dim of Y
),要获得跨维度
K
( Y
的第 3 个维度)的矩阵乘法之和,
sum( X .* Y )
3000
An alternative is sum( X.* Y, dims=3 )
produces a MxN
matrix where each entry is the sum of (m,n) over k.另一种方法是
sum( X.* Y, dims=3 )
生成MxN
矩阵,其中每个条目是 (m,n) 在 k 上的总和。 sum( sum( X.* Y, dims=(1,2) ) )
also gives the total sum from the XY_k
products. sum( sum( X.* Y, dims=(1,2) ) )
还给出XY_k
乘积的总和。 You could do this in the expanded form with all the for loops if you want如果需要,您可以使用所有 for 循环以扩展形式执行此操作
sumXY = 0
for k in 1:size(Y)[3]
for j in 1:size(Y)[2]
for i in 1:size(Y)[1]
sumXY += X[i,j] * Y[i,j,k]
end
end
end
sumXY
3000.0
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.