> For the complete documentation index, see [llms.txt](https://trizen.gitbook.io/sidef-lang/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://trizen.gitbook.io/sidef-lang/programming_tasks/i/identity_matrix.md).

# Identity matrix

```ruby
func identity_matrix(n) {
    n.of { |i|
        n.of { |j|
            i == j ? 1 : 0
        }
    }
}

for n (ARGV ? ARGV.map{.to_i} : [4, 5, 6]) {
  say "\n#{n}:"
  for row (identity_matrix(n)) {
    say row.join(' ')
  }
}
```

#### Output:

```
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

6:
1 0 0 0 0 0
0 1 0 0 0 0
0 0 1 0 0 0
0 0 0 1 0 0
0 0 0 0 1 0
0 0 0 0 0 1
```
