Julia 中的二元积

问题描述 投票:0回答:1

如何在 Julia 中有效地执行两个向量之间的二进积?

a = rand(100,2)
b = rand(100,2)
c = # should be the dyadic of a,b -> (100 2,2) Array{Float64, 3}
julia
1个回答
0
投票

简短回答:

stack(eachrow(a).*transpose.(eachrow(b)))

演示:

julia> a = rand(3,2)
3×2 Matrix{Float64}:
 0.445565  0.28906
 0.730971  0.609956
 0.767486  0.819149

julia> b = rand(3,2)
3×2 Matrix{Float64}:
 0.822466  0.17486
 0.557388  0.0830537
 0.280613  0.698774

julia> stack(eachrow(a).*transpose.(eachrow(b)))
2×2×3 Array{Float64, 3}:
[:, :, 1] =
 0.366463  0.0779114
 0.237742  0.0505449

[:, :, 2] =
 0.407434  0.0607098
 0.339982  0.0506591

[:, :, 3] =
 0.215366  0.536299
 0.229864  0.5724

输入 (n,2) 和 (n,2) 的结果大小为 (2,2,n)。

© www.soinside.com 2019 - 2024. All rights reserved.