You could use the xparse
package which offers great possibilities for defining user commands:
My First attempt:
\usepackage{xparse}
\DeclareDocumentCommand{\mymat}{o m o}
{%
\IfNoValueTF{#1}
{\IfNoValueTF{#3}{\mathbf{#2}}{\mathbf{#2}^{#3}}}
{\IfNoValueTF{#3}{\mathbf{#2}_{#1}}{\mathbf{#2}_{#1}^{#3}}}
}
Egregs simpler solution with IfValueT
\usepackage{xparse}
\DeclareDocumentCommand{\mymat}{o m o}
{%
\mathbf{#2}\IfValueT{#1}{_{#1}}\IfValueT{#3}{^{#3}}
}
m
stands for mandatory argument, o
for optional.
IfValueT{argument}{true code}
checks if the argument was given and calls either true code
or does nothing.
IfNoValueTF{argument}{true code}{false code}
checks if the argument was not given and calls either {true code} or {false code}
Result (You should maybe add a negative math space \!
if you have an A matrix.:
\mymat{A}[\!\top]
):
\begin{document}
\begin{equation}
\mymat{A}
\mymat[1]{A}
\mymat{A}[\top]
\mymat[1]{A}[\top]
\end{equation}
\end{document}

twoopt
, from theoberdiek
bundle`, does that.\newcommand{\mymat}[1]{\mathbf{#1}}
and\mymat{A}^{T}_{i}
is clearer and not more difficult to type.^{}
and_{}
affect the spacing so it is better to use a markup that does not put in null sub and superscript atoms.