multiply numpy ndarray with 1d array along a given axis

前端 未结 5 1895
死守一世寂寞
死守一世寂寞 2021-01-04 06:40

It seems I am getting lost in something potentially silly. I have an n-dimensional numpy array, and I want to multiply it with a vector (1d array) along some dimension (whi

5条回答
  •  轻奢々
    轻奢々 (楼主)
    2021-01-04 07:17

    You could also use a simple matrices trick

    c = np.matmul(a,diag(b))
    

    basically just doing matrix multiplication between a and a matrix whose diagonals are the elements of b. Maybe not as efficient but it's a nice single line solution

提交回复
热议问题