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 to find the mirror of an identity matrix without using numpy?

Here I have an identity matrix that goes from top left to bottom right. I’m trying to flip it so I can get a row of 1’s going from top right to bottom left but I don’t want to use numpy. But I just cant work out how to do it…

num = int(input("enter your number"))
for i in range(0, num):
    for j in range(0, num):
        if (i == j):
            print(1, sep=" ", end=" ")
        else:
            print(0, sep=" ", end=" ")
    print()

Example:
Input: 4
Output:

1 0 0 0
0 1 0 0
0 0 1 0
0 0 0 1

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 :

There is simple relation between column, row and num

if i + j + 1 == num:

Full code:

num = int(input("enter your number"))
for i in range(0, num):
    for j in range(0, num):
        if i + j + 1 == num:
            print(1, sep=" ", end=" ")
        else:
            print(0, sep=" ", end=" ")
    print()

EDIT:

Other idea is to revers one range

for j in range(num-1, -1, -1):

Full code:

num = int(input("enter your number"))
for i in range(0, num):
    for j in range(num-1, -1, -1):
        if i == j:
            print(1, sep=" ", end=" ")
        else:
            print(0, sep=" ", end=" ")
    print()
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