'How to create a matrix with zeros and column position equal to row is 1

How can I create an array of any dimension where it's all zeros except for the column position where if it's the same as the row position it's equal to 1. For example, a 4x4 matrix:

1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1


Solution 1:[1]

That's called an identity matrix in Linear Algebra. You can use numpy:

import numpy as np
np.identity(4) # use this
np.eye(4) # or use this

As for the difference between them, you can check this great answer

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Mohamed Yasser