pyt*_*end 4 python numpy concatenation matrix
我想创建一个toeplitz矩阵的toeplitz矩阵。H1,H2和H3已经是Toeplitz矩阵。我的结果应如下所示:
H1 0 0
H2 H1 0
H3 H2 H1
0 H3 H2
0 0 H3
现有的toeplitz函数仅接受向量,因此无法将其用于矩阵。目前,我正在使用vstack创建第一列,然后创建第二列,等等,然后hstack用于合并所有列。这需要很多工作,因为我必须np.zeros在某些位置专门添加矩阵。我想不出一种更好的方法来连接numpy数组,因为该函数只有几个函数,而这些函数都不适合我的问题。
代替对vstack和的嵌套调用hstack,预分配最终数组然后使用嵌套循环填充数组会更有效。您最初可以使用更高维的数组来保持代码的干净。
例如,此脚本
import numpy as np
H1 = np.array([[11, 11], [11, 11]])
H2 = np.array([[22, 22], [22, 22]])
H3 = np.array([[33, 33], [33, 33]])
inputs = (H1, H2, H3)
# This assumes all the arrays in `inputs` have the same shape,
# and that the data type of all the arrays is the same as H1.dtype.
nh = len(inputs)
nrows = 2*nh - 1
m, n = H1.shape
# T is a 4D array. For a given i and j, T[i, :, j, :] is a 2D array
# with shape (m, n). T can be intepreted as a 2D array of 2D arrays.
T = np.zeros((nrows, m, nh, n), dtype=H1.dtype)
for i, H in enumerate(inputs):
for j in range(nh):
T[i + j, :, j, :] = H
# Partially flatten the 4D array to a 2D array that has the desired
# block structure.
T.shape = (nrows*m, nh*n)
print(T)
Run Code Online (Sandbox Code Playgroud)
版画
[[11 11 0 0 0 0]
[11 11 0 0 0 0]
[22 22 11 11 0 0]
[22 22 11 11 0 0]
[33 33 22 22 11 11]
[33 33 22 22 11 11]
[ 0 0 33 33 22 22]
[ 0 0 33 33 22 22]
[ 0 0 0 0 33 33]
[ 0 0 0 0 33 33]]
Run Code Online (Sandbox Code Playgroud)
(请注意,结果不是Toeplitz矩阵;它是块Toeplitz矩阵。)
| 归档时间: |
|
| 查看次数: |
729 次 |
| 最近记录: |