Construct the Identity Matrix

The challenge is very simple. Given an integer input n, output the n x n identity matrix. The identity matrix is one that has 1s spanning from the top left down to the bottom right. You will write a program or a function that will return or output the identity matrix you constructed. Your output may be a 2D array, or numbers separated by spaces/tabs and newlines.

Example input and output

1: [[1]]
2: [[1, 0], [0, 1]]
3: [[1, 0, 0], [0, 1, 0], [0, 0, 1]]
4: [[1, 0, 0, 0], [0, 1, 0, 0], [0, 0, 1, 0], [0, 0, 0, 1]]
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]]

1
===
1

2
===
1 0
0 1

3
===
1 0 0
0 1 0
0 0 1

etc.


This is , so the shortest code in bytes wins.

• Given an integer input n ... -- I assume you mean a natural number? Aug 28, 2018 at 17:00

Common Lisp, 96 94 92 bytes

(lambda(n)(set'a(make-array(list n n):initial-element 0))(dotimes(i n)(setf(aref a i i)1))a)


Try it online!

PHP 88 Bytes

Try it online

Code

<?php for($i=0;$i++<$argv;){$a=array_fill(1,$argv,0);$a[$i]=1;echo implode(",",$a)."
";}


Explanation

<?php
for($i=0;$i++<$argv;){ // loop up to the argument$a=array_fill(1,$argv,0); // create an array fill with 0 with length =$argv value
$a[$i]=1;                   // use the current iteration to place the "1"
echo implode(",",$a)." // implode to output the matrix ";}  Output $argv=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


Forth (gforth), 48 bytes

: f dup 0 do dup 0 do 0 i j = - . loop cr loop ;


Try it online!

Explanation

uses nested loops to output the matrix. For each element it outputs 1 if the indexes are equal, and 0 otherwise.

Code Explanation

:f           \ start a new word definition
dup        \ duplicate input
0 do       \ outer counted loop from 0 to input - 1
dup      \ duplicate input again
0 do     \ inner counted loop from 0 to input - 1
0      \ put a 0 on the stack (used to convert -1 to 1)
i j =  \ -1 if i equals j, 0 otherwise
- .    \ subtract from 0 to convert -1 to 1, and output
loop     \ end inner loop
cr       \ output a newline
loop       \ end outer loop
;            \ end word definition


D, 114 bytes

void f(int n){import std.stdio;int N=n*n;while(N--){if(N%(n+1))write("0 ");else write("1 ");if(N%n==0)writeln();}}


Try it online!

PARI/GP, 14 bytes

n->matrix(n)^0

Attempt This Online!

Takes the zeroth power of the zero matrix.

V, 16, 11 bytes

Ài0 ÀÄòjl


Try it online!

Pip, 3 bytes

EYa


Try It Online!

Builtin.

• What's the shortest non-builtin solution you can find? Dec 2, 2022 at 16:31
• @DLosc I found a non-builtin solution of 6 bytes. Dec 3, 2022 at 7:29

Nibbles, 6 5 bytes (10 nibbles)

.;,$.@==$_

.               # map across each value of
,$# 1..input ; # (saving this range) . # map across each value of @ # the saved range (1..input again) ==$_      #     are the two values equal?


Pyt, 5 bytes

řĐɐ=Ɩ


Try it online!

ř              implicit input; create řange [1,2,...,n]
Đ             Đuplicate
ɐ=           for ɐll pairs: are elements equal?
Ɩ          cast to Ɩnteger; implicit print


Nekomata, 3 bytes

ᵒ-¬


Attempt This Online!

ᵒ-     Generate a 2d table with subtraction
¬    Logical not (converts 0 to 1 and other numbers to 0)


Thunno 2, 4 bytes

RD€=


Attempt This Online!

Explanation

RD€=  # Implicit input
R     # Push the range [1..n]
€   # For each number,
=  # Compare it with
D    # The range [1..n]
# Implicit output