How can I create a matrix by permutating a vector in Python

Question:

I have matrix two matrixes:

A = [1,2,3,4]
B = np.zeros((4,8))

So, how can I have a matrix C, with a format like this:

C=[[1,2,3,4,0,0,0,0],[0,0,1,2,3,4,0,0],[0,0,0,0,1,2,3,4],[3,4,0,0,0,0,1,2]]
Asked By: Công Minh Đặng

||

Answers:

You can use following solution without numpy, but i appreciate if you have smarter version using numpy then feel free to highlight

A = [1, 2, 3, 4, 0, 0, 0, 0]

matrix = [A]
for shift in range(3):
    A = A[-2:] + A[:-2]
    matrix.append(A)

print(matrix)

[[1, 2, 3, 4, 0, 0, 0, 0], [0, 0, 1, 2, 3, 4, 0, 0], [0, 0, 0, 0, 1,
2, 3, 4], [3, 4, 0, 0, 0, 0, 1, 2]]

Answered By: Zain Ul Abidin

Silly Friday answer;

from collections import deque

A = [1,2,3,4]
B = [0,0,0,0]
AB = deque(A+B)

C = [list(AB) for i in range(4) if not AB.rotate(2*bool(i))]
Answered By: bn_ln
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.