v9.1.1
Menu

Build Identity Matrix

easyFREENotationLinear AlgebraCV: Introduction to Computer Vision

Implement a function to generate an n×nn \times n identity matrix, a fundamental concept in linear algebra. The identity matrix is crucial in various mathematical operations, including matrix multiplication and transformation.

The identity matrix II is defined such that when it multiplies another matrix AA, the result is the original matrix AA. This property makes the identity matrix the multiplicative identity for matrices. The elements of the identity matrix can be defined using the Kronecker delta δij\delta_{ij}, where δij=1\delta_{ij} = 1 if i=ji = j and δij=0\delta_{ij} = 0 otherwise.

Return the matrix as a list of nn rows, each a list of nn integers (1 on the main diagonal, 0 everywhere else):

Iij=δijI_{ij} = \delta_{ij}

This technique is widely used in computer vision for image and signal processing.

Example

Input
identity_matrix(3)
Output
[[1,0,0],[0,1,0],[0,0,1]]
Reasoning
  • The input n=3n = 3 is the side length, so the result is a square grid with 3 rows and 3 columns: 9 entries in total, each indexed by a row ii and a column jj from 0 to 2.
  • Each entry follows the Kronecker delta: Iij=δijI_{ij} = \delta_{ij}. Only the pairs where row and column match, (0,0)(0,0), (1,1)(1,1) and (2,2)(2,2), get a 1. The other 6 pairs, such as (0,1)(0,1) or (2,0)(2,0), get a 0.
  • Reading row by row gives [1, 0, 0], then [0, 1, 0], then [0, 0, 1]: the 1 moves one column to the right on each new row, tracing the main diagonal.
  • Check the defining property on a row vector: [4,7,2][4, 7, 2] times this matrix gives 4⋅1+7⋅0+2⋅0=44 \cdot 1 + 7 \cdot 0 + 2 \cdot 0 = 4 in the first slot, and likewise 77 and 22, so the vector comes back unchanged.
  • Notice the entries are the integers 1 and 0, not decimals, and that n=1n = 1 collapses to the single entry [[1]].

Constraints

  • n is a positive integer, 1 ≤ n ≤ 100

More from CV: Introduction to Computer Vision

solution.py

Test results

Sign in to run your code against the test cases.