47
\$\begingroup\$

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.

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

101 Answers 101

1 2 3
4
0
\$\begingroup\$

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!

\$\endgroup\$
0
\$\begingroup\$

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
\$\endgroup\$
0
\$\begingroup\$

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
\$\endgroup\$
0
\$\begingroup\$

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!

\$\endgroup\$
0
\$\begingroup\$

PARI/GP, 14 bytes

n->matrix(n)^0

Attempt This Online!

Takes the zeroth power of the zero matrix.

See also Charles's answer,

\$\endgroup\$
0
\$\begingroup\$

V, 16, 11 bytes

Ài0 ÀÄòjl

Try it online!

\$\endgroup\$
0
0
\$\begingroup\$

Pip, 3 bytes

EYa

Try It Online!

Builtin.

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

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?         

enter image description here

\$\endgroup\$
0
\$\begingroup\$

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
\$\endgroup\$
0
\$\begingroup\$

Nekomata, 3 bytes

ᵒ-¬

Attempt This Online!

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

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
\$\endgroup\$
1 2 3
4

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.