[Question]
  [
Given a positive integer `n`, output the `N`-dimensional identity "matrix", which is the `N^N` array with `1` where all the components of the indices are equal and `0` otherwise. `N^N` means N-by-N-by-N-by-...
```
1 -> [1]
2 -> [[1,0],[0,1]]
3 -> [[[1,0,0],[0,0,0],[0,0,0]],[[0,0,0],[0,1,0],[0,0,0]],[[0,0,0],[0,0,0],[0,0,1]]]
4 -> [[[[1,0,0,0],[0,0,0,0],[0,0,0,0],[0,0,0,0]],[[0,0,0,0],[0,0,0,0],[0,0,0,0],[0,0,0,0]],[[0,0,0,0],[0,0,0,0],[0,0,0,0],[0,0,0,0]],[[0,0,0,0],[0,0,0,0],[0,0,0,0],[0,0,0,0]]],[[[0,0,0,0],[0,0,0,0],[0,0,0,0],[0,0,0,0]],[[0,0,0,0],[0,1,0,0],[0,0,0,0],[0,0,0,0]],[[0,0,0,0],[0,0,0,0],[0,0,0,0],[0,0,0,0]],[[0,0,0,0],[0,0,0,0],[0,0,0,0],[0,0,0,0]]],[[[0,0,0,0],[0,0,0,0],[0,0,0,0],[0,0,0,0]],[[0,0,0,0],[0,0,0,0],[0,0,0,0],[0,0,0,0]],[[0,0,0,0],[0,0,0,0],[0,0,1,0],[0,0,0,0]],[[0,0,0,0],[0,0,0,0],[0,0,0,0],[0,0,0,0]]],[[[0,0,0,0],[0,0,0,0],[0,0,0,0],[0,0,0,0]],[[0,0,0,0],[0,0,0,0],[0,0,0,0],[0,0,0,0]],[[0,0,0,0],[0,0,0,0],[0,0,0,0],[0,0,0,0]],[[0,0,0,0],[0,0,0,0],[0,0,0,0],[0,0,0,1]]]]
```
For example, if `a` is the `4`-dimensional identity "matrix", then the only entries with `1` would be `a[0][0][0][0]`, `a[1][1][1][1]`, `a[2][2][2][2]`, and `a[3][3][3][3]`.
This is [code-golf](/questions/tagged/code-golf "show questions tagged 'code-golf'"). Shortest answer in bytes wins. [Standard loopholes](https://codegolf.meta.stackexchange.com/questions/1061/loopholes-that-are-forbidden-by-default) apply.
  
[Answer]
# Octave, 29 bytes
```
@(n)accumarray((x=1:n)'+!x,1)
```
[Try it online!](https://tio.run/##y08uSSxL/f/fQSNPMzE5uTQ3sagosVJDo8LW0CpPU11bsULHUPN/Yl6xhrHmfwA "Octave – Try It Online")
[Answer]
# [Jelly](https://github.com/DennisMitchell/jelly), 8 bytes
```
×=¥þ’¡`Ṡ
```
[Try it online!](https://tio.run/##ARsA5P9qZWxsef//w5c9wqXDvuKAmcKhYOG5oP///zM "Jelly – Try It Online")
Ooh, looks like I get to outgolf @Dennis in his own language again :-)
This is a 1-argument function (because Jelly's default output format for nested lists is a little ambiguous, meaning that it arguably doesn't fulfil the spec as a full program).
## Explanation
```
×=¥þ’¡`Ṡ
     ¡    Repeatedly apply the following operation,
    ’     {input-1} times in total:
   þ        For each element of the current value {perhaps made into a range}
      `     and of {the range from 1 to the} {input}:
 =            Compare corresponding elements, giving 0 for equal or 1 for unequal
× ¥           then multiply by one of the elements
       Ṡ  then replace each element with its sign
```
In order to understand this, it helps to look at the intermediate steps. For an input of 3, we get the following intermediate steps:
1. `[1,2,3]` (input, made into a range implicitly by the `þ`)
2. `[[1,0,0],[0,2,0],[0,0,3]]` (make a table with `[1,2,3]`, compare for equality to get `[[1,0,0],[0,1,0],[0,0,1]]`, then multiply by one of the values we compared)
3. `[[[1,0,0],[0,0,0],[0,0,0]],[[0,0,0],[0,2,0],[0,0,0]],[[0,0,0],[0,0,0],[0,0,3]]]` (the same idea again)
4. `[[[1,0,0],[0,0,0],[0,0,0]],[[0,0,0],[0,1,0],[0,0,0]],[[0,0,0],[0,0,0],[0,0,1]]]` (replace each element with its sign using `Ṡ`)
Note the fact that the input starts out 1-dimensional means that we have to loop (input-1) times in order to add (input-1) dimensions, producing an input-dimensional list.
Fun fact: this program contains five quicks in a row, `¥þ’¡``. (A quick is a modifier to a "link", or builtin, used to modify its behaviour or combine it with another link.)
[Answer]
## Mathematica, 30 bytes
```
Array[Boole@*Equal,#~Table~#]&
```
[Answer]
# [APL (Dyalog)](https://www.dyalog.com/), 10 bytes
```
1=≢∘∪¨⍳⍴⍨⎕
```
[Try it online!](https://tio.run/##SyzI0U2pTMzJT///qKPdMyU1r@S/oe2jzkWPOmY86lh1aMWj3s2Perc86l3xqG8qSMl/BTAAq@Qy5ELmGaHwjFF4JgA "APL (Dyalog Unicode) – Try It Online")
`1=` [is] 1 equal to
`≢` the number
`∘` of
`∪` unique elements
`¨` in each of
`⍳` the indices in an array with the dimensions of
`⍴⍨` the self-reshape (*N* copies of *N*) of
`⎕` the input (*N*) [?]
[Answer]
# [Jelly](https://github.com/DennisMitchell/jelly), 9 bytes
```
ðṗE€ṁ+þ’¡
```
[Try it online!](https://tio.run/##AR8A4P9qZWxsef//w7DhuZdF4oKs4bmBK8O@4oCZwqH///80 "Jelly – Try It Online")
### How it works
Achieving the task directly seems to be difficult (I haven't found a way), but contructing arrays of the same numbers and arrays of the same shape is quite easy.
`ð` makes the chain dyadic, and the integer input **n** serves as both left and right argument for the chain. It's possible to use a monadic chain instead, but the parsing rules for dyadic ones save three bytes here (two after accouting for `ð`).
The *Cartesian power* atom `ṗ`, with left and right argument equal to **n**, constructs the array of all vectors of length **n** that consist of elements of **[1, ..., n]**, sorted lexicographically.
When **n = 3**, this yields
```
[[1, 1, 1], [1, 1, 2], [1, 1, 3], [1, 2, 1], [1, 2, 2], [1, 2, 3], [1, 3, 1], [1, 3, 2], [1, 3, 3], [2, 1, 1], [2, 1, 2], [2, 1, 3], [2, 2, 1], [2, 2, 2], [2, 2, 3], [2, 3, 1], [2, 3, 2], [2, 3, 3], [3, 1, 1], [3, 1, 2], [3, 1, 3], [3, 2, 1], [3, 2, 2], [3, 2, 3], [3, 3, 1], [3, 3, 2], [3, 3, 3]]
```
The *equal each* quicklink `E€` tests the elements of all constructed vectors for equality.
When **n = 3**, we get
```
[1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1]
```
which are the elements of the 3-dimensional identity matrix, in a flat array.
The dyadic quicklink `+þ’¡` is called with left argument and right argument **n**. The quick `¡` calls the decrement atom `’`, which yields **n-1**, then calls the *add table* quicklink `+þ` **n-1** times. 
Initially, the arguments of `+þ` are both **n**. After each call, the right argument is replaced by the left one, and the left one is replaced by the return value of the call.
The *table* quick calls the *add* atom `+` for each elements of its left argument and each element of its right argument, constructing a table/matrix of the return value. The initial integer arguments **n** are promoted to the ranges **[1, ... n]**.
When **n = 3**, after promotion but before the first iteration, both arguments are
```
[1, 2, 3]
```
Adding each integer in this array to each integer in this array yields
```
[[2, 3, 4], [3, 4, 5], [4, 5, 6]]
```
In the next invocation, we add each of these arrays to the integers in **[1, 2, 3]**. Addition vectorizes (adding an integer to an array adds it to each element), so we get
```
[[[3, 4, 5], [4, 5, 6], [5, 6, 7]],
 [[4, 5, 6], [5, 6, 7], [6, 7, 8]],
 [[5, 6, 7], [6, 7, 8], [7, 8, 9]]]
```
This array has the same shape as the 3-dimensional identity matrix, but not the correct elements.
Finally, the *mold* atom `ṁ` discards the integer entries of the result to the right and replaces them in order with the elements in the result to the left.
[Answer]
# [Python](https://docs.python.org/2/), 70 bytes
```
f=lambda n,l=[]:[f(n,l+[i])for i in(len(l)<n)*range(n)]or+(l==l[:1]*n)
```
[Try it online!](https://tio.run/##DcYxDoAgDAXQqzC26qJuRk5CGDCKNqkfQnDw9OjwkpffeiVMrUWr4d72YDCodX5xkf71TjzHVIwYAenx4xXclYDzILBPpSe1Vt0y@g7cchFUE0mQn0rMbf4A "Python 2 – Try It Online")
A recursive solution. Thinking of the matrix as a list of matrices one dimension smaller, it iterates over that list to go down the tree. It remembers the indices picked in `l`, and when `n` indices have been picked, we assign a `1` or `0` depending on whether they are all the same. 
---
# [Python 2](https://docs.python.org/2/), 73 bytes
```
n=input();r=0
exec'r=eval(`[r]*n`);'*n+('n-=1;r'+'[n]'*n+'=1;')*n
print r
```
[Try it online!](https://tio.run/##FchLCoAgFAXQuato9vwQ9BmKKxHBCCEhbvKwqNVbDc8pT90OTK3BZZSzSmXZDSLdaSV26Vp2GT0HjagsaRhJ6N1omQx5hH/oIykNUTijdtza/AI "Python 2 – Try It Online")
An improvement on [totallyhuman's method](https://codegolf.stackexchange.com/a/127921/20260) of making a matrix of zeroes and then assigning ones to the diagonal.
---
# [Python 2](https://docs.python.org/2/), 88 bytes
```
n=input()
t=tuple(range(n))
print eval('['*n+'+(i0'+'==i%d'*n%t+')'+'for i%d in t]'*n%t)
```
[Try it online!](https://tio.run/##HcgxCoAwDADA3Vd0kSRmEZ37EnEQrBoosZRU8PW1ON6l165bp1rVi6ZiSJ15KykGzJueAZWoS1nUXHi2iLDAoAyMMgKD99LvLXpjoObjzq6NE3W2/k@1zh8 "Python 2 – Try It Online")
Some nonsense with `eval`, generating a nested list, and string-format substitution. The string to be evaluated looks like:
```
[[[+(i0==i0==i1==i2)for i0 in t]for i1 in t]for i2 in t]
```
[Answer]
# Python 2 + [NumPy](http://www.numpy.org/), ~~80~~ ~~72~~ 70 bytes
Now tied with the top Python answer!
```
from numpy import*
n=input()
a=zeros((n,)*n)
a[[range(n)]*n]=1
print a
```
[**Try it online**](https://tio.run/##DcctEoAgEAbQzimIwFjUzEkYAsEfAh876xLw8quvPZpyd2yqJ/dmMRpNWxt1lmAQK2iI86bE9@D@OIfFB/xPiQuuw8HngBxXQ1whtqjuHw)
*Saved 8 bytes thanks to Andras Deak, and 2 by officialaimm*
[Answer]
# [Python 2](https://docs.python.org/2/), ~~99~~ ~~93~~ 90 bytes
*Thanks to Rod for ~~some~~ even more help that got it working and also shaved 6 bytes off! -3 bytes thanks to xnor.*
```
n=input()
r=eval(`eval('['*n+'0'+']*n'*n)`)
for i in range(n):exec'r'+`[i]`*n+'=1'
print r
```
[Try it online!](https://tio.run/##HctBCsMgEEbhvadwNxo3aciq4ElEMIRpMhD@yGBLe3qbdvP4Nq9@2n5i6h1RUJ/NeaORX8vhyr@UaECgkQLlAZd98eZxqhUrsLpgYwd/5zevpBRKklx@Q7yRqSpoVnufvw "Python 2 – Try It Online")
[Answer]
## JavaScript (ES6), 67 bytes
```
f=(n,d=n-1,i)=>[...Array(n)].map((_,j)=>d?f(n,d-1,j-i?n:j):j-i?0:1)
```
Explanation: `i` is used to keep track of whether the cell is on the main diagonal or not. Initially it's undefined, so on the first recursive call we always pass the first dimension, while on subsequent recursive calls we only pass it on if the current dimension index is equal to all previous dimensions, otherwise we pass an index of `n` which indicates that all of the recursive cells should be zero.
[Answer]
# [Brainfuck](https://github.com/TryItOnline/tio-transpilers), 61 bytes
```
>,[->+>+<<]>[-<<+>>]>-[->+.-<<<<[->+>+<<]>[-<+>]>[->>.<<]>]+.
```
## Ungolfed
The numbers after the angle brackets indicate the cell the head is over.
```
>,                   read n to 1
[->+>+<<]            move 1 to 2 and 3
>2[-<<+>>]>3         move 2 to 0 
                     (tape: n 0 0 n 0)
-[                   while cell 3 {
    -                  dec 3
    >4+.-<3            print \x1
    <<<0[->+>+<<]      move 0 to 1 and 2
    >1[-<+>]>2         move 1 to 0
                       (tape: 0 0 n rows_left 0)
    [                  while cell 2 {
        -                dec 2
        >>4.<<           print \x0
    ]>3                }
]                    }
+.                   print \x1
```
[Try it online!](https://tio.run/##SypKzMxLK03O/v/fTida107bTtvGJtYuWtfGRtvOLtZOFySmB@TZ2KDIatuBKDs7PRA/Vlvv/39mAA "brainfuck – Try It Online")
Input is a binary number. Output is a matrix stored in row-major order.
[Answer]
# [R](https://www.r-project.org/), ~~64~~ 49 bytes
*-15 bytes thanks to Jarko Dubbeldam*
```
x=array(0,rep(n<-scan(),n));x[seq(1,n^n,l=n)]=1;x
```
Reads from stdin and returns an array, printing as matrices. `seq` generates a sequence evenly spaced from `1` to `n^n` with length `l=n`, which does the trick quite nicely to index where the 1s go.
[Try it online!](https://tio.run/##K/r/v8I2sagosVLDQKcotUAjz0a3ODkxT0NTJ09T07oiuji1UMNQJy8uTyfHNk8z1tbQuuK/yX8A "R – Try It Online")
### old version:
```
n=scan();x=rep(0,n^n);x=array(x,rep(n,n));x[matrix(1:n,n,n)]=1;x
```
Reads `n` from stdin; returns an array, printing the results as matrices. I struggled with this for a while until I read the docs for `[`, which indicate that a matrix can be used to index an array, where each row of the matrix represents the set of indices. Neat!
[Try it online!](https://tio.run/##K/r/P8@2ODkxT0PTusK2KLVAw0AnLy4PxEksKkqs1KjQAQnm6eRpAsWicxNLijIrNAytgAJAoVhbQ@uK/yb/AQ "R – Try It Online")
[Answer]
# [J](http://jsoftware.com/), ~~16~~ 15 bytes
```
i.@#~=i.*]#.#&1
```
[Try it online!](https://tio.run/##y/r/P03B1kohU89Buc42U08rVllPWc2Qiys1OSNfIU3B@P9/AA "J – Try It Online")
[Answer]
## Python 3 + numpy, ~~81~~ 77 bytes
```
from numpy import*
f=lambda n:all([a==range(n)for a in indices((n,)*n)],0)+0
```
I'm not *entirely* sure that the above fits all guidelines: it returns an ndarray with the given properties. I know anonymous functions are usually fine, but an interactive shell will actually print
```
>>> f(2)
array([[1, 0],
       [0, 1]])
```
If the array fluff makes the above invalid, I have to throw in a `print()` for something like 7 additional bytes.
[Try it online!](https://tio.run/##DcI9DsMgDAbQPafwaJMMkdgqcZIqg/NDiwQfyKVDTk/79Nrd3xV@jGi1EL6l3ZRKq9bdFEPWsp9KeGjO/NQQTPG6GBKrkVLC/5mO68OMRRxkW1aZ19EsoXNkLzJ@)
[Answer]
# Pyth, 14 bytes
```
ucGQtQms!t{d^U
```
[Test suite](https://pyth.herokuapp.com/?code=ucGQtQms%21t%7Bd%5EU&test_suite=1&test_suite_input=1%0A2%0A3%0A4&debug=0)
Explanation: `^U`, which is implicitly `^UQQ`, where `Q` is the input, calculates all possible Q element lists of the range `0 ... n-1`. `ms!t{d` maps the ones with all elements equal to 1, and the rest to 0. This gives the flattened output
`ucGQtQ` executes the following, Q - 1 times: Chop the input into lists of size Q.
[Answer]
# [C# (.NET Core)](https://www.microsoft.com/net/core/platform), 166 bytes
```
n=>{var c=new int[n];int i=0,d;for(;i<n;c[i++]=n);var m=System.Array.CreateInstance(typeof(int),c);for(i=0;i<n;i++){for(d=0;d<n;c[d++]=i);m.SetValue(1,c);};return m;}
```
[Try it online!](https://tio.run/##hVFda8IwFH33V1wKg4R2pXW@xQhD2NjYYCBsD9KHkKYu0CaSRkWkv90l1foxp7uUNr333HPOzeX1PddGbMEFL1ldw4fRM8Oqns9s2reP2jIrOSy1zOGdSYXwoXQE@Zisayuq@Gmh@FAqG3WJR2PYegQF0N5W0dFmyQxwqsQKHGqqMuI@IGkS5aTQBhE5VIRPZRhmVGHi0RU9pYrHRjArXpRzprhAdj0XukCOBUcctxyOraVxJHjjE7lL5C1v7nklJlU8EfaTlQuBUt/WECPswiioSLMlvbPJvAfmpYFCgQaYXFTnuna1bqYMNpBE3QPNOX71LUsByLVMkwyGMMBn5fNLPbnYsVa1LkX8ZaQVb1IJFEwDCN2CjFSz@FW73QRREHkz2OWDzFnygNZ6/NyN68u/RvDh/Tz4FrQ/hZBiuIPBJVQWBxClkOALwOUMnUL/oNC/pXCq0r@mcl2pU0sPaul/aqeK6V4RdjsKw79bmt7tzPFvd2q2Pw "C# (.NET Core) – Try It Online")
At first I thought it could not be done with a lambda expression in C#... ^\_\_^U
[Answer]
# Common Lisp, ~~147~~ 133 bytes
```
(defun i(n)(flet((m(x)(fill(make-list n)x)))(let((a(make-array(m n):initial-element 0)))(dotimes(i n)(incf(apply #'aref a(m i))))a)))
```
[Try it online!](https://tio.run/##JcxBCgMxCAXQqwhd9LsYKHTX28iMAamxIZPCzOlT2y4E9T9d3fY2JzYt7yBDMIrrACqObM0dVZ66JBsUfDAzfrn899K7nKgZPSxsmPiirlVj0O1rt9ewqjssBSzWAmnNT7pcpWuh/EKWjiVrorVueZn6nuMH)
The usual super-lengthy lisp. Reduced 12 bytes thank to @ceilingcat!
Explanation:
```
(defun i (n)
  (flet ((m (x) (fill (make-list n) x)))            ; function to build a list of n values x
    (let ((a (make-array (m n) :initial-element 0))); build the array with all 0
      (dotimes (i n)                                ; for i from 0 to n-1
        (incf (apply #'aref a (m i))))              ; add 1 to a[i]..[i] 
      a)))                                          ; return the array
```
[Answer]
# [Vyxal](https://github.com/Vyxal/Vyxal), 6 bytes
```
ẋÞm⁽≈€
```
[Try it Online!](https://vyxal.pythonanywhere.com/#WyJQIiwiIiwi4bqLw55t4oG94omI4oKsIiwiIiwiMiJd)
My first submission, thanks to emanresu for help with the lambda.
[Answer]
# [SOGL V0.12](https://github.com/dzaima/SOGL), 22 [bytes](https://github.com/dzaima/SOGL/blob/master/chartable.md)
```
.^κ.H/ 0* 1.H≤Οčr.H{.n
```
[Try it Here!](https://dzaima.github.io/SOGLOnline/?code=LiU1RSV1MDNCQS5ILyUyMDAqJTIwMS5IJXUyMjY0JXUwMzlGMW5yLkglN0Iubg__,inputs=NA__)  
[Leaves the output on the stack](https://codegolf.meta.stackexchange.com/a/8507/59183), viewable in the console. If the numbers in the output were allowed to be strings, then the `r` could be removed.  
Just as a fun test of how SOGL does in challenges it was completely not made for :p
```
input: x
.^                      push  x^x
  κ                     subtract x    (x^x)-x
   .H/                  divide by x   ((x^x) - x)/x
       0*               get that many zeroes
          1             push "1"
           .H           push x-1
             ≤          pull the first item from the stack to the top
              Ο         encase (x-1 times the zeroes, separated, started and ended with 1s)
               č        chop to a char-array
                r       convert each character to a number
                 .H{    repeat x-1 times:
                    .n    in the top array, for each group of x contents, encase that in an array
```
[Answer]
## Clojure, 92 bytes
```
#(reduce(fn[v i](assoc-in v(repeat % i)1))(nth(iterate(fn[v](vec(repeat % v)))0)%)(range %))
```
Nice that [assoc-in](https://clojuredocs.org/clojure.core/assoc-in) works also with vectors, not only with hash-maps.
]