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