简体   繁体   中英

How do I construct this matrix in an efficient way?

I have a matrix A with dimension (T,k) . I want to construct the following block matrix for positive integers m and t1, t2 < T :

Here, Im is the identity matrix of dimension m and A[t,i]Im is a diagonal matrix with all diagonal elements equal to A[t,i] . Is there an efficient way to write this, perhaps without any loops? Here is my current code (setting t2 = t, t1 = 0 ) but it is very inefficient

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))

Try using the Kronecker product of fliplr(A) with eye(m) , for example:

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)

which prints

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.]]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM