Generation

generate functionTue, 10 Jan 2023

Create a diagonal matrix with elements from N to 0. Count the sum of its values on the diagonal.

def create_diag_matrix(n): #create a list of lists first return [[0 for _ in range(n)] for _ in range(n)] def create_diag_matrix_2(n): return [[i*0+1 if i==j else i*0 for i in range(n)] for j in range(n)] def create_diag_matrix_3(n): return [[1 if i==j else 0 for i in range(n)] for j in range(n)]

Questions about programming?Chat with your personal AI assistant