MatrixQ
Details
- MatrixQ[expr] gives True only if expr is a list and each of its elements is a list of the same length, containing no elements that are themselves lists, or a representation of such an expression.
- A matrix is also known as a rectangular list of lists, a depth-2 array or a rank-2 array.
- Possible representations of a matrix include: »
-
{{…},{…},…} or List[List[…],…] an ordinary list of lists SparseArray[…] a sparse matrix CauchyMatrix[…], PermutationMatrix[…], QuantityArray[…], SymmetrizedArray[…], etc. a structured matrix {v1,v2,…}, where VectorQ[vi] is True a list of ordinary, sparse and/or structured vectors » - The empty list {} is a zero-length vector and thus not a matrix. »
- Common values of test in MatrixQ[expr,test] for use with numeric matrices include:
-
NumberQ test if expr is a matrix of explicit numbers NumericQ test if expr is a matrix of numeric expressions Positive test if expr is a matrix of positive numbers RealValuedNumberQ test if expr is a matrix of numbers with head Integer, Rational or Real RealValuedNumericQ test if expr is a matrix of numeric expressions with real values - Structural tests commonly used include:
-
IntegerQ test if expr is a matrix of integers PolynomialQ[#,x]& test if expr is a matrix of polynomials in x QuantityQ test if expr is a matrix of quantities StringQ test if expr is a matrix of strings
Examples
open all close allBasic Examples (3)
Test of whether an object is a matrix:
MatrixQ[{{a, b}, {3, 4}}]This is not a matrix because the rows have different lengths:
MatrixQ[{{a, b}, {3}}]This is not a matrix because the elements are not lists:
MatrixQ[{1, 2, 3}]This is not a matrix because the lists are too deeply nested:
MatrixQ[Array[a, {2, 2, 2}]]Use tests to restrict particular kinds of numbers:
MatrixQ[RandomReal[1, {3, 3}], NumberQ]MatrixQ[RandomReal[1, {3, 3}], IntegerQ]Scope (8)
Many different heads can represent a matrix:
MatrixQ[SparseArray[{{1, 3} -> a, {5, 1} -> b}]]MatrixQ[PermutationMatrix[{1, 3, 2}]]MatrixQ[Quantity[{{1.3, 2.2, 3.3}, {3.1, 3.3, 2.2}}, "Meters"]]A matrix can have rows in different representations of a list:
m = {{1, 2, 3}, SparseArray[Automatic, {3}, 0, {1, {{0, 1}, {{3}}}, {a}}], SymmetrizedArray[StructuredArray`StructuredData[{3}, {{{1} -> c}, {}}]]};
MatrixQ[m]m//MatrixFormTest for a matrix with numeric entries:
MatrixQ[{{E, 1}, {π, 2}, {Sin[1], Cos[2]}}, NumericQ]Test for a matrix with explicit numbers:
MatrixQ[{{E, 1}, {π, 2}, {Sin[1], Cos[2]}}, NumberQ]Test if a matrix has real numeric entries:
MatrixQ[{{Pi, Sin[1]}, {Cos[2], E}}, RealValuedNumericQ]Faster test for real-valued numbers:
MatrixQ[Im[{{1, 2.}, {3 / 4, 5`20}}], RealValuedNumberQ]Test for a matrix of integers:
MatrixQ[{{1, 2}, {3, 4}}, IntegerQ]Test for a matrix of numbers that are integers when rounded to two decimal places:
MatrixQ[{{1, 1.005}, {1.997, 4.002}}, Abs[# - Round[#]] < 0.005&]MatrixQ[{{"a", "b"}, {"c", "d"}}, StringQ]MatrixQ[{{"a", "b"}, {c, d}}, StringQ]Test for a matrix of polynomials in x:
MatrixQ[{{1, x}, {1 + x, x ^ 2 + 2x + 1}}, PolynomialQ[#, x]&]MatrixQ[{{1, x}, {Sin[x], Exp[x]}}, PolynomialQ[#, x]&]Generalize MatrixQ using a test:
MatrixQ[Array[a, {2, 2, 2}], ListQ]The tested expression is not a matrix by the standard definition:
MatrixQ[Array[a, {2, 2, 2}]]Applications (1)
Define a function that only evaluates for explicit matrices:
hpart[m_ ? MatrixQ] := (m + ConjugateTranspose[m]) / 2This represents the Hermitian part of a matrix symbolically:
hpart[a]This gets the Hermitian part explicitly:
hpart[{{1, 2I}, {3I, 4}}]HermitianMatrixQ[%]Properties & Relations (6)
A matrix is made up of vectors of equal length:
m = RandomReal[1, {3, 2}];vecq = Map[VectorQ, m]lens = Map[Length, m]Apply[And, Append[vecq, Apply[Equal, lens]]]Confirm with the built-in function:
MatrixQ[m]An empty list is not a matrix:
MatrixQ[{}]It is instead a zero-length vector:
VectorQ[{}]A list of empty lists is considered an
matrix:
MatrixQ[{{}, {}, {}}]Dimensions[{{}, {}, {}}]MatrixQ[expr] is equivalent to ArrayQ[expr,2]:
m = RandomReal[1, {2, 2}]{ArrayQ[m, 2], MatrixQ[m]}MatrixQ[expr] is equivalent to ArrayQ[expr,2]:
{ArrayQ[m, 2, MachineNumberQ], MatrixQ[m, MachineNumberQ]}{ArrayQ[m, 2, IntegerQ], MatrixQ[m, IntegerQ]}For a depth-4 array arr, MatrixQ[arr,MatrixQ] gives True:
arr = Array[a, {2, 3, 4, 5}];
{ArrayQ[arr, 4], MatrixQ[arr, MatrixQ]}The converse of this statement is False:
narr = {Array[a, {3, 4, 5}], Array[a, {3, 4, 6}]};
{ArrayQ[narr, 4], MatrixQ[narr, MatrixQ]}MatrixQ effectively uses AllowedHeads"ListLike":
mat = {SparseArray[{1, 2}], QuantityArray[{1, 2}, "Meters"]};
expr = f[f[1, 2], f[3, 4]];{MatrixQ[mat], MatrixQ[expr]}{ArrayDepth[mat, AllowedHeads -> "ListLike"], ArrayDepth[expr, AllowedHeads -> "ListLike"]}{Dimensions[mat, AllowedHeads -> "ListLike"], Dimensions[expr, AllowedHeads -> "ListLike"]}Tech Notes
History
Introduced in 1988 (1.0) | Updated in 2003 (5.0)
Text
Wolfram Research (1988), MatrixQ, Wolfram Language function, https://reference.wolfram.com/language/ref/MatrixQ.html (updated 2003).
CMS
Wolfram Language. 1988. "MatrixQ." Wolfram Language & System Documentation Center. Wolfram Research. Last Modified 2003. https://reference.wolfram.com/language/ref/MatrixQ.html.
APA
Wolfram Language. (1988). MatrixQ. Wolfram Language & System Documentation Center. Retrieved from https://reference.wolfram.com/language/ref/MatrixQ.html
BibTeX
@misc{reference.wolfram_2026_matrixq, author="Wolfram Research", title="{MatrixQ}", year="2003", howpublished="\url{https://reference.wolfram.com/language/ref/MatrixQ.html}", note=[Accessed: 13-June-2026]}
BibLaTeX
@online{reference.wolfram_2026_matrixq, organization={Wolfram Research}, title={MatrixQ}, year={2003}, url={https://reference.wolfram.com/language/ref/MatrixQ.html}, note=[Accessed: 13-June-2026]}