[英]Component-wise Product of all Column Combinations of two Matrices
As the title says I want to calculate the component-wise product of all column combinations of two matrices. 如标题所述,我想计算两个矩阵的所有列组合的按分量乘积。 I already found a solution using
numpy.einsum
and numpy.hstack
. 我已经找到了使用
numpy.einsum
和numpy.hstack
的解决方案。 I wonder if there is a solution without hstack
. 我想知道是否有没有
hstack
的解决方案。
Let a = [a_1, a_2, ..., a_n]
be a dxn
matrix and b = [b_1, b_2, ..., b_m]
a dxm
matrix. 令
a = [a_1, a_2, ..., a_n]
为dxn
矩阵, b = [b_1, b_2, ..., b_m]
为dxm
矩阵。 I want to calculate 我想计算
[a_1b_1, a_1b_2, ..., a_nb_{n-1}, a_nb_n]
, [a_1b_1, a_1b_2, ..., a_nb_{n-1}, a_nb_n]
,
where a_kb_l
is the component wise product, ie a_kb_l = [a_{1,k}*b{1,l}, ..., a_{d,k}*b{d,l}].T
. 其中
a_kb_l
是明智的乘积,即a_kb_l = [a_{1,k}*b{1,l}, ..., a_{d,k}*b{d,l}].T
。
My solution is the following. 我的解决方案如下。
np.hstack(np.einsum('...j,...l -> j...l', a, b))
Can I go without the h_stack
? 我可以不用
h_stack
吗?
The following improvement replaces the hstack
with a call of reshape
. 下面的改进将
hstack
替换为reshape
。 This releases quite a bit memory pressure when d
is high. 当
d
高时,这会释放相当大的内存压力。
np.einsum('...j,...l -> ...jl', a, b).reshape(d, -1)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.