问题描述
我想创建一个toeplitz矩阵的toeplitz矩阵.H1,H2和H3已经是Toeplitz矩阵.我的结果应如下所示:H1 0 0H2 H1 0H3 H2 H10 H3 H20 0 H3
I want to create a toeplitz matrix of toeplitz matrix.H1, H2 and H3 are toeplitz matrices already. My result should look like that:H1 0 0H2 H1 0H3 H2 H10 H3 H20 0 H3
现有的toeplitz函数仅接受向量,因此我不能将其用于矩阵.目前,我正在使用vstack
创建第一列,然后创建第二列,等等,然后使用hstack
合并所有列.这需要花费很多精力,因为我必须在某些位置专门添加np.zeros
矩阵.我想不出一种更好的方法来连接numpy数组,因为该函数只有几个函数,而这些函数都不适合我的问题.
The existing toeplitz-function only accepts vector, so I can't use it for matrix. Currently I'm using vstack
to create the first column, then second column etc. and then I use hstack
to merge all columns. This takes a lot of effort, since I have to specifically add np.zeros
matrices at certain places. I can't think of a better way to concatenate numpy arrays, since there are only a few functions for that and none of them really fits my problem.
推荐答案
代替对vstack
和hstack
的嵌套调用,预分配最终数组然后使用嵌套循环填充将更有效.数组.最初,您可以使用更高维的数组来保持代码的干净.
Instead of nested calls to vstack
and hstack
, it will be more efficient to preallocate the final array, and then use a nested loop to fill in the array. You can initially use a higher dimensional array to keep the code clean.
例如,该脚本
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)
打印
[[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]]
(请注意,结果不是Toeplitz矩阵;它是阻止Toeplitz矩阵.)
(Note that the result is not a Toeplitz matrix; it is a block Toeplitz matrix.)
这篇关于Toeplitz矩阵的Toeplitz矩阵的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!