I am new to python but have experience in MatLab. I am trying to write a module to create an identity matrix (i.e. I = [[1,0],[0,1]]) of a given size without using numpy for a piece of an assignment. I have attempted to do so with the following code:
Q = []
b = []
row = 2
col = 2
for i in range(row):
b.append(0)
for j in range(row):
Q.append(b)
for n in range(col):
Q[n][n] = 1
My idea here was first to create a matrix of zeros "Q" and then assign ones to the diagonal entries. However, when I run this code, it creates a matrix Q = [[1,1],[1,1]]. I have also tried to be a bit more specific by writing:
for m in range(row):
for n in range(col):
if m == n:
Q[m][n] = 1
else:
Q[m][n] = 0
Which gives a different matrix Q = [[0,1],[0,1]]. I'm assuming my issue is coming from the code used to create Q, but I don't entirely understand how matrices work in python. Any help would be greatly appreciated! Thank you.