To answer your question all you need is just a slight modification of what is said here:
\documentclass{article}
\usepackage{amsmath,amssymb,mathtools}
\usepackage{ifthen}
\newcommand{\forLoop}[5][1]
{%
\setcounter{#4}{#2}%
\ifthenelse{ \value{#4} < #3 }%
{%
#5%
\addtocounter{#4}{#1}%
\forLoop[#1]{\value{#4}}{#3}{#4}{#5}%
}%
% Else
{%
\ifthenelse{\value{#4} = #3}%
{%
#5%
}%
% Else
{}%
}%
}
\newcounter{identRow}
\newcounter{identCol}
\newcommand{\idmatrixn}[1]
{
\begin{bmatrix}
\forLoop{1}{#1}{identRow}
{
\forLoop{1}{#1}{identCol}
{
\ifthenelse{\equal{\value{identRow}}{\value{identCol}}}{1}{0}
\ifthenelse{\equal{\value{identCol}}{#1}}{}{&}
}
\\
}
\end{bmatrix}
}
\begin{document}
\idmatrixn{10}
\end{document}
Be careful since I noticed that the
bmatrix
environment does not support matrices bigger than
10x m
or m x 10
where 0<m<11
.
If you need bigger matrices you may use instead array
in the following way:
\newcounter{identRow}
\newcounter{identCol}
\newcommand{\idmatrixn}[1]
{
\left[\begin{array}{*{#1}c}
\forLoop{1}{#1}{identRow}
{
\forLoop{1}{#1}{identCol}
{
\ifthenelse{\equal{\value{identRow}}{\value{identCol}}}{1}{0}
\ifthenelse{\equal{\value{identCol}}{#1}}{}{&}
}
\\
}
\end{array}\right]
}