我如何有效地构建这个矩阵?
How do I construct this matrix in an efficient way?
我有一个维度为 (T,k)
的矩阵 A
。我想为正整数 m
和 t1, t2 < T
构建以下分块矩阵:
![\begin{align*} \begin{bmatrix} A[t_2,1] I_m & A[{t_2 -1},1] I_m & \dots & A[{t_1},1] I_m\ A[t_2,2] I_m & A[{t_2 -1},2] I_m & \dots & A[{t_1},2] I_m\ \vdots\ A[t_2,k] I_m & A[{t_2 -1},k] I_m & \dots & A[{t_1},k] I_m \end{bmatrix} \end{align*}](https://latex.codecogs.com/gif.latex?%5Cbegin%7Balign*%7D&space;%5Cbegin%7Bbmatrix%7D&space;A[t_2,1]&space;I_m&space;&&space;A[%7Bt_2&space;-1%7D,1]&space;I_m&space;&&space;%5Cdots&space;&&space;A[%7Bt_1%7D,1]&space;I_m%5C%5C&space;A[t_2,2]&space;I_m&space;&&space;A[%7Bt_2&space;-1%7D,2]&space;I_m&space;&&space;%5Cdots&space;&&space;A[%7Bt_1%7D,2]&space;I_m%5C%5C&space;%5Cvdots%5C%5C&space;A[t_2,k]&space;I_m&space;&&space;A[%7Bt_2&space;-1%7D,k]&space;I_m&space;&&space;%5Cdots&space;&&space;A[%7Bt_1%7D,k]&space;I_m&space;%5Cend%7Bbmatrix%7D&space;%5Cend%7Balign*%7D)
这里,Im
是维度m
的单位矩阵,A[t,i]Im
是所有对角线元素都等于A[t,i]
的对角矩阵。有没有一种有效的方法来编写这个,也许没有任何循环?这是我当前的代码(设置 t2 = t, t1 = 0
)但效率很低
B = np.zeros([k*m,t*m])
for i in range(filter_count):
for j in range(t):
B[i*m:(i+1)*m,j*m:(j+1)*m] = np.diag(np.repeat([A[t-j,i]],m))
尝试将 fliplr(A)
的克罗内克积与 eye(m)
结合使用,例如:
import numpy as np
A = np.array([[1,2,3],[4,5,6]])
Im = np.eye(3)
R = np.kron(np.fliplr(A), Im)
print('A:\n', A)
print('Im:\n', Im)
print('R:\n', R)
打印
A:
[[1 2 3]
[4 5 6]]
Im:
[[1. 0. 0.]
[0. 1. 0.]
[0. 0. 1.]]
R:
[[3. 0. 0. 2. 0. 0. 1. 0. 0.]
[0. 3. 0. 0. 2. 0. 0. 1. 0.]
[0. 0. 3. 0. 0. 2. 0. 0. 1.]
[6. 0. 0. 5. 0. 0. 4. 0. 0.]
[0. 6. 0. 0. 5. 0. 0. 4. 0.]
[0. 0. 6. 0. 0. 5. 0. 0. 4.]]
我有一个维度为 (T,k)
的矩阵 A
。我想为正整数 m
和 t1, t2 < T
构建以下分块矩阵:
这里,Im
是维度m
的单位矩阵,A[t,i]Im
是所有对角线元素都等于A[t,i]
的对角矩阵。有没有一种有效的方法来编写这个,也许没有任何循环?这是我当前的代码(设置 t2 = t, t1 = 0
)但效率很低
B = np.zeros([k*m,t*m])
for i in range(filter_count):
for j in range(t):
B[i*m:(i+1)*m,j*m:(j+1)*m] = np.diag(np.repeat([A[t-j,i]],m))
尝试将 fliplr(A)
的克罗内克积与 eye(m)
结合使用,例如:
import numpy as np
A = np.array([[1,2,3],[4,5,6]])
Im = np.eye(3)
R = np.kron(np.fliplr(A), Im)
print('A:\n', A)
print('Im:\n', Im)
print('R:\n', R)
打印
A:
[[1 2 3]
[4 5 6]]
Im:
[[1. 0. 0.]
[0. 1. 0.]
[0. 0. 1.]]
R:
[[3. 0. 0. 2. 0. 0. 1. 0. 0.]
[0. 3. 0. 0. 2. 0. 0. 1. 0.]
[0. 0. 3. 0. 0. 2. 0. 0. 1.]
[6. 0. 0. 5. 0. 0. 4. 0. 0.]
[0. 6. 0. 0. 5. 0. 0. 4. 0.]
[0. 0. 6. 0. 0. 5. 0. 0. 4.]]