Dimensions

Dimensions[expr]

gives a list of the dimensions of expr.

Dimensions[expr,n]

gives a list of the dimensions of expr down to level n.

Details and Options

  • expr must be a full array, with all the pieces of expr at a particular level having the same length. (The elements of expr can then be thought of as filling up a hyperrectangular region.)
  • For special array objects like SparseArray, NumericArray and QuantityArray, Dimensions yields the dimensions of the corresponding ordinary lists.
  • The following options can be given:
  • AllowedHeadsAutomaticwhat heads to allow at each level of the array
  • Settings for the AllowedHeads option include:
  • Automaticdefault settings
    Inheritedallow only Head[expr] at any level
    hallow only head h at any level
    {h1,}allow any of h1, at different levels
    h1|h2|allow the hi at the same or different levels
    Allallow any head at different levels
    "ListLike"allow List and related heads
    Fullallow any head at any level
  • The setting AllowedHeads"ListLike" allows all heads recognized by ArrayQ, which include List, SparseArray and structured array heads like QuantityArray and SymmetrizedArray.
  • With the default setting AllowedHeads->Automatic, successive levels in expr must have the same head, except that ordinary lists, sparse arrays and structured arrays are considered the same head.

Examples

open allclose all

Basic Examples  (1)

Find the dimensions of a matrix:

Scope  (5)

Dimensions counts only dimensions at which an expression is not "ragged":

Dimensions works with arrays of any depth:

Give dimensions only down to level 2:

Dimensions works with special array types, including SparseArray objects:

SymmetrizedArray objects:

NumericArray objects:

Find the dimensions of a matrix that is a mixture of List and different special array heads:

Dimensions works with any head, not just List:

Options  (4)

AllowedHeads  (4)

Only consider nested expressions with head List:

By default, rectangular nested expressions with any head are considered arrays:

Allow lists and numeric arrays at different levels:

Different heads cannot be mixed at the same level:

Allow a mixture of both heads at the same level:

Allow List as well as other general array representations:

Only allow the head List:

Both settings disallow general heads:

Allow any head at each level of the array:

An array has a consistent head at each level:

Completely ignore heads, looking only at the tree structure of the expression:

Applications  (2)

Create a version of MatrixQ that only accepts list matrices:

Create several expressions to test:

The function matrixQ gives True only for listMat, which both is a matrix and has head List:

MatrixQ gives True for matrices of any known array type:

Find dimensions of regions filled by 10 steps of cellular automaton evolution:

Properties & Relations  (7)

Dimensions always returns a list:

The default value of the option AllowedHeads allows mixtures of list, sparse and structured arrays:

It also allows any single head at successive levels:

Use AllowedHeads"ListLike" to allow any list-like array while excluding other heads:

VectorQ, MatrixQ and ArrayQ effectively use AllowedHeads"ListLike":

The setting AllowedHeads"ListLike" does not include ByteArray and NumericArray:

Combine "ListLike" with the additional heads to allow the combination:

Length[Dimensions[expr]] equals ArrayDepth[expr]:

Dimensions[Array[h,dims]] gives dims:

If Dimensions[expr] is non-empty, its first element equals Length[expr]:

Possible Issues  (1)

Numeric expressions report dimensions based of the structure of their FullForm:

To treat all numeric expressions as scalars, use the setting List or "ListLike" for AllowedHeads:

Introduced in 1988
 (1.0)
 |
Updated in 2003
 (5.0)
2012
 (9.0)
2019
 (12.0)
2020
 (12.1)