Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Python by (16.4k points)
closed by

I just need to create a function identity which return n identity matrix.

Example:

identity(3)

output's [[1,0,0][0,1,0][0,0,1]]

I also tried the below code:

def identity(n):

matrix=[[0]*n]*n

i=0

while i<n:

    matrix[i][i]=1

    i+=1

return matrix

Also tried with the range function, but it doesn't work.

def identity(n):

    matrix=[[0]*n]*n

    k=matrix[:]

    i=0

    for i in range(1,n):

        matrix[i][i]=1

        i+=1

    return k

print(identity(5))

For n=5, it outputs:

[[1, 1, 1, 1, 1], [1, 1, 1, 1, 1], [1, 1, 1, 1, 1], [1, 1, 1, 1, 1], [1, 1, 1, 1, 1]]

closed

4 Answers

0 votes
by (19k points)
selected by
 
Best answer
To create an identity matrix, you can use the following code:

def identity(n):

    return [[1 if i == j else 0 for j in range(n)] for i in range(n)]

print(identity(5))

This code will generate the desired identity matrix for n=5:

[[1, 0, 0, 0, 0],

 [0, 1, 0, 0, 0],

 [0, 0, 1, 0, 0],

 [0, 0, 0, 1, 0],

 [0, 0, 0, 0, 1]]
0 votes
by (26.4k points)

Try the below code:

def identity(n):

    m=[[0 for x in range(n)] for y in range(n)]

    for i in range(0,n):

        m[i][i] = 1

    return m

Interested to learn python in detail? Come and Join the python course.

0 votes
by (25.7k points)
You are experiencing an issue with your code because the way you are creating the identity matrix using [[0]*n]*n leads to incorrect references. Modifying one element affects the entire column in the matrix. To fix this, you can use a nested list comprehension or a loop to generate the identity matrix correctly. Here's an updated version of your code:

def identity(n):

    matrix = [[0] * n for _ in range(n)]  # Create an empty matrix

    for i in range(n):

        matrix[i][i] = 1  # Set diagonal elements to 1

    return matrix

print(identity(5))

This code will generate the correct identity matrix for the given input n=5:

[[1, 0, 0, 0, 0],

 [0, 1, 0, 0, 0],

 [0, 0, 1, 0, 0],

 [0, 0, 0, 1, 0],

 [0, 0, 0, 0, 1]]
0 votes
by (15.4k points)
You're encountering an issue with your code when trying to create an identity matrix using the approach [[0]*n]*n. The problem arises because this method creates references to the same row, resulting in modifications affecting the entire column. To resolve this, you can use either a nested list comprehension or a loop to construct the identity matrix correctly. Here's an improved version of your code:

def identity(n):

    matrix = [[0] * n for _ in range(n)]  # Create an empty matrix

    for i in range(n):

        matrix[i][i] = 1  # Set diagonal elements to 1

    return matrix

print(identity(5))

By utilizing this code, you'll obtain the desired identity matrix for n=5:

[[1, 0, 0, 0, 0],

 [0, 1, 0, 0, 0],

 [0, 0, 1, 0, 0],

 [0, 0, 0, 1, 0],

 [0, 0, 0, 0, 1]]

Related questions

0 votes
4 answers
0 votes
1 answer
asked Jul 3, 2019 in Python by Sammy (47.6k points)
Welcome to Intellipaat Community. Get your technical queries answered by top developers!

30.5k questions

32.6k answers

500 comments

108k users

Browse Categories

...