Here's an implementation using expl3.
The arguments to \xmat are
* (to denote infinite matrices)
- the type of matrix delimiters (optional, default
p)
- the letter for the entries
- the number of rows (can be symbolic or an explicit integer)
- the number of explicitly shown rows (optional, default 3)
- the number of columns (can be symbolic or an explicit integer)
- the number of explicitly shown columns (optional, default 3)
\documentclass{article}
\usepackage{amsmath}
\usepackage{xparse}
\ExplSyntaxOn
% #1 = star
% #2 = letter for the matrix delimiters (p,b,B,v,V), default = p
% #3 = letter for the entries
% #4 = number of rows
% #5 = number of explicit rows, default = 3
% #6 = number of columns
% #7 = number of explicit columns, default = 3
\NewDocumentCommand{\xmat}{sO{p}mmO{3}mO{3}}
{
\IfBooleanTF { #1 }
{% with \xmat* we want an infinite matrix
\bool_set_true:N \l__wang_xmat_infinite_bool
}
{
\bool_set_false:N \l__wang_xmat_infinite_bool
}
\wang_xmat:nnnnnn { #2 } { #3 } { #4 } { #5 } { #6 } { #7 }
% t l r mr c mc
}
\bool_new:N \l__wang_xmat_infinite_bool
\bool_new:N \l__wang_xmat_dotrow_bool
\bool_new:N \l__wang_xmat_dotcol_bool
\tl_new:N \l__wang_xmat_body_tl
\int_new:N \l__wang_xmat_exrows_int
\int_new:N \l__wang_xmat_excols_int
\cs_new_protected:Nn \wang_xmat:nnnnnn
{
% clear the variable containing the body of the matrix
\tl_clear:N \l__wang_xmat_body_tl
% set the tentative number of explicit rows
\int_set:Nn \l__wang_xmat_exrows_int { #4 }
\regex_match:nnTF { \A [[:digit:]]* \Z } { #3 }
{% number of rows is an integer
\int_compare:nTF { #3 <= #4 }
{% if #3 <= #4 we don't want a row of dots
\bool_set_false:N \l__wang_xmat_dotrow_bool
\int_set:Nn \l__wang_xmat_exrows_int { #3 }
}
{% we want a row of dots
\bool_set_true:N \l__wang_xmat_dotrow_bool
}
}
{% number of rows is symbolic, we want a row of dots
\bool_set_true:N \l__wang_xmat_dotrow_bool
}
% set the tentative number of explicit columns
\int_set:Nn \l__wang_xmat_excols_int { #6 }
\regex_match:nnTF { \A [[:digit:]]* \Z } { #5 }
{% number of cols is an integer
\int_compare:nTF { #5 <= #6 }
{% if #5 <= #6 we don't want a column of dots
\bool_set_false:N \l__wang_xmat_dotcol_bool
\int_set:Nn \l__wang_xmat_excols_int { #5 }
}
{% we want a column of dots
\bool_set_true:N \l__wang_xmat_dotcol_bool
}
}
{% number of columns is symbolic, we want a column of dots
\bool_set_true:N \l__wang_xmat_dotcol_bool
}
% loop through the rows
\int_step_inline:nn { \l__wang_xmat_exrows_int }
{
% add the first entry in the row
\tl_put_right:Nn \l__wang_xmat_body_tl { #2\sb{##1 1} }
% add the further entries in the explicit columns
\int_step_inline:nnn { 2 } { \l__wang_xmat_excols_int }
{
\tl_put_right:Nn \l__wang_xmat_body_tl { & #2\sb{##1 ####1} }
}
% if we have a column of dots, add \cdots and the last entry
\bool_if:NT \l__wang_xmat_dotcol_bool
{
\tl_put_right:Nn \l__wang_xmat_body_tl { & \cdots & #2\sb{##1 #5} }
}
% infinite matrix, add \cdots
\bool_if:NT \l__wang_xmat_infinite_bool
{
\tl_put_right:Nn \l__wang_xmat_body_tl { & \cdots }
}
% finish up the row
\tl_put_right:Nn \l__wang_xmat_body_tl { \\ }
}
% finish up the rows
\bool_if:NT \l__wang_xmat_dotrow_bool
{
% if we have a row of dots, fill it in
\tl_put_right:Nn \l__wang_xmat_body_tl { \vdots }
\prg_replicate:nn { \l__wang_xmat_excols_int - 1 }
{
\tl_put_right:Nn \l__wang_xmat_body_tl { & \vdots }
}
\bool_if:NT \l__wang_xmat_dotcol_bool
{
\tl_put_right:Nn \l__wang_xmat_body_tl { & \ddots & \vdots }
}
\tl_put_right:Nn \l__wang_xmat_body_tl { \\ }
% fill the last row
\tl_put_right:Nn \l__wang_xmat_body_tl { #2\sb{#3 1} }
\int_step_inline:nnn { 2 } { \l__wang_xmat_excols_int }
{
\tl_put_right:Nn \l__wang_xmat_body_tl { & #2\sb{#3 ##1} }
}
\bool_if:NT \l__wang_xmat_dotcol_bool
{
\tl_put_right:Nn \l__wang_xmat_body_tl { & \cdots & #2\sb{#3 #5} }
}
% if the matrix is infinite, add a further column with \cdots
\bool_if:NT \l__wang_xmat_infinite_bool
{
\tl_put_right:Nn \l__wang_xmat_body_tl { & \cdots }
}
% finish up the row
\tl_put_right:Nn \l__wang_xmat_body_tl { \\ }
}
% if the matrix is infinite, add a final row
\bool_if:NT \l__wang_xmat_infinite_bool
{
\tl_put_right:Nn \l__wang_xmat_body_tl { \vdots }
\prg_replicate:nn { \l__wang_xmat_excols_int - 1 }
{
\tl_put_right:Nn \l__wang_xmat_body_tl { & \vdots }
}
\bool_if:NT \l__wang_xmat_dotcol_bool
{
\tl_put_right:Nn \l__wang_xmat_body_tl { & & \vdots }
}
\tl_put_right:Nn \l__wang_xmat_body_tl { & \ddots }
}
% typeset the matrix
\begin{#1matrix}\l__wang_xmat_body_tl\end{#1matrix}
}
\ExplSyntaxOff
\begin{document}
\[
\xmat{M}{m}{n} \xmat*[b]{M}{m}{n}
\]
\[
\xmat{M}{m}[2]{n}[4] \xmat*[b]{M}{m}[4]{n}[2]
\]
\[
\xmat{v}{1}{n} \xmat{v}{m}{1} \xmat{v}{m}{2} \xmat{a}{2}{2} \xmat*[v]{a}{2}{2}
\]
\[
\xmat[]{M}{m}{n}
\]
\end{document}

If you want an undelimited matrix, call \xmat[]{M}{m}{n}.
I don't think there is a way to produce this using just tools in the physics package (which I find weird and awkward to use, so I never recommend it).
$$should not be used. – Apr 23 '19 at 11:27physicspackage. Its code quality is very poor. – Henri Menke Apr 24 '19 at 02:32