Hi,
I think you can construct a transformation matrix, and use a matrix multiplication to implement it. I can show you an example of how to insert 0 in rows and columns. Suppose your K=2, weights are ‘A, B, C, D’, the transformations are like (hopefully you can get my point)

c5f4a304da01aae02937c690a5624651608×1080 62.8 KB

And you can use the combination of them for your task.
Correct me if I’m wrong