For natural numbers$n$ and $m$ and a set $X$, an $n\times m$matrix of elements of $X$ is a function $M:[n]\times[m]\rightarrow X$ from the Cartesian product $[n]\times[m]$ to $X$.

Often one uses the term in a context where one can add and multiply matrices using matrix calculus. Addition of matrices of the same dimension requires $X$ to have an “addition” operation; multiplication of matrices requires $X$ to also have a “multiplication” operation. Usually $X$ is at least a rig and often a ring or a field.

More generally, for arbitrary sets $A$ and $B$ we can define an $A\times B$-matrix to be a function $A\times B\to X$. If $X$ has some kind of “infinitary sums” as well as finite “products”, then we can also multiply matrices of this sort: e.g. if $X$ is the set of objects of a monoidal category with arbitrary coproducts.

Note that if the structure on X is such that matrix multiplication is associative and there exist identity matrices, then matrices over X may be taken as the morphisms of a category $Mat_X$ whose composition is matrix multiplication. This is in particular the case if X is a field, and so many basic theorems of linear algebra may be understood as concerning functors from $Vect_X$ into $Mat_X$ and natural transformations between such functors.