Write a program to create identity matrix with dimension provided


Topic: Write a program to create identity matrix with dimension provided

Solution

dim = 3
M = []
while len(M) < dim:
    M.append([])
    while len(M[-1]) < dim:
        M[-1].append(0.0)
for i in range(dim):
    M[i][i] = 1.0
print('Identity Matrix')
for i in range(dim):
    row = '|'
    for b in range(dim):
        row = row + ' ' + str(M[i][b])
    print(row + ' ' + '|')



List all Python Programs