ArrayDepth
ArrayDepth[expr]
gives the depth to which expr is a full array, with all the parts at a particular level having the same length.
Details and Options
- ArrayDepth[list] is equivalent to Length[Dimensions[list]].
- For special array objects like SparseArray, NumericArray and QuantityArray, ArrayDepth yields the depth of the corresponding ordinary lists.
- The following option can be given:
-
AllowedHeads Automatic what heads to allow at each level of the array - Settings for the AllowedHeads option include:
-
Automatic default settings Inherited allow only Head[expr] at any level h allow only head h at any level {h1,…} allow any of h1,… at different levels h1h2… allow the hi at the same or different levels "ListLike" allow List and related heads All allow any head at different levels Full allow 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 allScope (4)
ArrayDepth counts only dimensions at which an expression is not "ragged":
ArrayDepth works with special array types, including SparseArray objects:
SymmetrizedArray objects:
NumericArray objects:
Find the depth of a matrix that is a mixture of List and different special array heads:
ArrayDepth 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:
Properties & Relations (8)
Length[Dimensions[expr]] equals ArrayDepth[expr]:
ArrayDepth always returns a non-negative integer:
The default value of the option AllowedHeads allows mixtures of list, sparse and structured arrays:
It also allows any single head at successive levels:
The following setting of AllowedHeads imitates the default behavior with respect to list-like arrays:
However, it excludes arbitrary 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:
ArrayDepth[Array[h,dims]] gives Length[dims]:
ArrayDepth only considers the levels to which the expression is completely rectangular:
Depth considers the deepest part of an expression:
For completely rectangular expressions, ArrayDepth gives a result one smaller than Depth:
Possible Issues (1)
Numeric expressions report array depth based of the structure of their FullForm:
To treat all numeric expressions as scalars, use the setting List or "ListLike" for AllowedHeads:
Text
Wolfram Research (2003), ArrayDepth, Wolfram Language function, https://reference.wolfram.com/language/ref/ArrayDepth.html (updated 2020).
CMS
Wolfram Language. 2003. "ArrayDepth." Wolfram Language & System Documentation Center. Wolfram Research. Last Modified 2020. https://reference.wolfram.com/language/ref/ArrayDepth.html.
APA
Wolfram Language. (2003). ArrayDepth. Wolfram Language & System Documentation Center. Retrieved from https://reference.wolfram.com/language/ref/ArrayDepth.html