Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading