[英]numpy sliding dot matrix operation
I'm using numpy to do some matrix operations and I'm unsure on how to achieve a particular operation on the following two matrices: 我正在使用numpy进行一些矩阵运算,但不确定如何在以下两个矩阵上实现特定运算:
(a) (b) (c)
[1 [1 -1 0 [2
1 (some operation) 1 -1 0 = -3
1 0 -1 0 0
1] 0 0 0] 0]
So essentially I want to have an equivalently shaped matrix as (a), but with every entry being the entry in (a), "dotted" with the column in (b) it corresponds to. 所以从本质上讲,我想要一个与(a)形状相等的矩阵,但是每个条目都是(a)中的条目,并用(b)中的列“点缀”它对应。
I'm currently using the following but it seems rather hacky and only gets me scalars and I feel like there must be a better way: 我目前正在使用以下内容,但似乎很hacky,只能让我成为标量,而且我觉得必须有更好的方法:
np.dot(a , np.atleast_2d(b[0].T[0]))[0].sum() = 2
etc...
Any suggestions? 有什么建议么?
If you initialise your data like this: 如果您像这样初始化数据:
b=np.zeros((4,3))
a=np.ones((4,1))
b[0:2,0]=1
b[0:3,1]=-1
You can use dot
: 您可以使用
dot
:
np.dot(a.T,b)
giving 给予
array([[ 2., -3., 0.]])
Almost what you want. 几乎是您想要的。 Your problem is that you have no 4th column in
b
corresponding to the 4th element in a
. 你的问题是,你有没有在第4列
b
对应于4元a
。 You could append a 0
to the result or append a column of zeros to b
to get around this. 您可以在结果后附加一个
0
或在b
后面附加一列零以解决此问题。 Or: 要么:
c=np.zeros_like(a)
c[0:3]=np.dot(a.T,b).T
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.