【发布时间】:2021-01-29 19:41:49
【问题描述】:
我做了矩阵的串联,如下图所示。
但这不是很有效,因为我的代码是针对这两个矩阵的。是否有可能让它更高效,这样我就不必一直重写代码中的矩阵,而是让它自动工作?
我的意思是我不必在代码中编写我使用的矩阵,但它会自动传递
我想将它放在一个循环中,其中包含将要通过的矩阵的不同变体
import numpy as np
arr1=np.array([[11, 21,31], [12, 22,32], [13, 23,32], [14, 24,34]])
arr2=np.array([1,2,3,4])
a = np.zeros((arr1.flatten().shape[0],2)) #init the new array
a[:,[0]] = arr1.T.flatten()[:,None] #fill the first column with the values
a[:,[1]] = np.tile(arr2,3)[:,None] # fill the second column with values
结果
array([[11., 1.],
[12., 2.],
[13., 3.],
[14., 4.],
[21., 1.],
[22., 2.],
[23., 3.],
[24., 4.],
[31., 1.],
[32., 2.],
[32., 3.],
[34., 4.]])
【问题讨论】:
-
自动运行是什么意思?
标签: numpy loops python-3.7 numpy-ndarray