【问题标题】:Multiplying by a one element matrix乘以一个元素矩阵
【发布时间】:2017-05-10 18:57:30
【问题描述】:

我想将 1x1 矩阵乘以 8x8 矩阵。显然我知道你不能将这两种形状的矩阵相乘。我的问题是如何从 1x1 矩阵中“提取”值,使其相当于将 8x8 矩阵乘以标量。换句话说,有没有办法将 1x1 矩阵变成标量?

到目前为止,这是我的代码,其中n 是我的 1x1 矩阵,flux 是我的 8x8 矩阵:

n=0
for i in range(delta_E.shape[0]):
    n+= 100/(210*(Sig_f_cell[i])*flux[i]*delta_E[i]*(1.6022e-13)*V_core)

flux = (np.linalg.inv(L))*G

目标:将通量乘以 n 的值

看起来 n 是一个标量,但是当我将它们相乘时,我得到了这个错误:

ValueError                                Traceback (most recent call last)
<ipython-input-26-0df98fb5a138> in <module>()
----> 1 Design_Data (1.34,.037,90)

<ipython-input-25-5ef77d3433bc> in Design_Data(pitch, Pu_fraction,     FE_length)
201     print('Number of fuel elements : ',N_FE)
202 
--> 203     return  n*flux
204 

C:\Users\Katey\Anaconda3\lib\site-packages\numpy\matrixlib\defmatrix.py  in __mul__(self, other)
341         if isinstance(other, (N.ndarray, list, tuple)) :
342             # This promotes 1-D vectors to row vectors
--> 343             return N.dot(self, asmatrix(other))
344         if isscalar(other) or not hasattr(other, '__rmul__') :
345             return N.dot(self, other)

ValueError: shapes (1,1) and (8,1) not aligned: 1 (dim 1) != 8 (dim 0)

我也尝试过将n[0]*flux 相乘,但我得到了同样的错误。

【问题讨论】:

  • n 似乎是标量而不是矩阵。是什么让你认为它是一个矩阵?
  • @Divakar 因为当我将它们相乘时,它给了我这个
  • @Divakar(见上文)
  • 无法在我的最后重现,也许可以尝试:n[0]*fluxn[0,0]*flux
  • @Divakar 谢谢我用另一种方式,谢谢你的帮助

标签: python python-3.x numpy matrix scalar


【解决方案1】:

您可以使用numpy.multiply 函数。给定一个 1x1 数组和一个 8x8 数组,此函数将 8x8 数组中的每个元素乘以 1x1 数组中的元素。如果我正确理解你的问题,这就是你要找的。这是文档中的一个使用示例

 >>> x1 = np.arange(9.0).reshape((3, 3))
 >>> x2 = np.arange(3.0)
 >>> np.multiply(x1, x2)
     array([[  0.,   1.,   4.],
            [  0.,   4.,  10.],
            [  0.,   7.,  16.]])

你可以在这里找到这个函数的文档https://docs.scipy.org/doc/numpy/reference/generated/numpy.multiply.html

【讨论】:

  • 是的,谢谢,这正是我想要的!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-01-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-06-04
  • 2016-08-11
相关资源
最近更新 更多