向Numpy数组添加新列的最佳方法 [英] Best way to add a new column to a Numpy array
问题描述
我想将向量添加到矩阵.
I want to add a vector to a matrix.
向量当前是一个列表(尽管很容易转换为一维Numpy数组).
The vector is currently a list (although easily converted to a 1D Numpy array).
矩阵当前是一个Numpy数组.
And the matrix is currently a Numpy array.
我当时想将矩阵重塑为矩阵,然后遍历最后一列,添加所需的值.但是,我不确定如何以这种方式重塑矩阵(即添加一列).我也希望我不必使用for循环.
I was thinking I could reshape the matrix to a matrix and then loop through the last column adding the desired values. However, I wasn't sure how I could reshape a matrix this way (i.e. adding a column). I was also hoping I didn't have to use a for loop.
我调查了使用np.concatenate
,np.hstack
和np.append
的情况.但是,我相信我需要将我的原始矩阵创建为矩阵与列全部为None
.这对我不起作用,因为在将最后一个向量添加到该矩阵之前,我将其用于许多计算.
I looked into using np.concatenate
, np.hstack
, and np.append
. However, I believe I need to create my original matrix as a matrix with the column all None
. This will not work for me because I use this matrix for many calculations before I need to add this last vector to it.