ArrayRules
✖
ArrayRules
gives the rules {pos1val1,pos2val2,…} specifying elements in a sparse array.
Details

- The last element of ArrayRules[s] is always {_,_,…}->def, where def is the default value for unspecified elements in the sparse array. »
- ArrayRules[list,val] takes the default value to be val. »
- ArrayRules[list] assumes a default value of 0. »
Examples
open allclose allBasic Examples (1)Summary of the most common use cases
Get the explicit elements in a SparseArray:

https://wolfram.com/xid/0j44o1ma-bvqr8k


https://wolfram.com/xid/0j44o1ma-iq7mwu

These rules are sufficient to efficiently construct an identical SparseArray:

https://wolfram.com/xid/0j44o1ma-ltb0ja


https://wolfram.com/xid/0j44o1ma-en2bfo

Scope (2)Survey of the scope of standard use cases
The last element of ArrayRules[s] is always {_,_,…}->def:

https://wolfram.com/xid/0j44o1ma-gt8x8y

A SparseArray with a default value of 2:

https://wolfram.com/xid/0j44o1ma-cnfepg


https://wolfram.com/xid/0j44o1ma-wuxvl

You can override this by explicitly specifying what default you would like:

https://wolfram.com/xid/0j44o1ma-i7pyet

These will construct a SparseArray identical to SparseArray[m,Automatic,1]:

https://wolfram.com/xid/0j44o1ma-btklew

Positions of 1 in an explicit array with the default taken to be 0:

https://wolfram.com/xid/0j44o1ma-dk94dr


https://wolfram.com/xid/0j44o1ma-lf57ns

These will construct a SparseArray identical to SparseArray[a]:

https://wolfram.com/xid/0j44o1ma-ou5kpu

Positions of 0 with 1 taken as default:

https://wolfram.com/xid/0j44o1ma-j0l435

These will construct a SparseArray identical to SparseArray[a,Automatic,1]:

https://wolfram.com/xid/0j44o1ma-h2x8j3

Applications (4)Sample problems that can be solved with this function
Get the number of explicit elements in a SparseArray:

https://wolfram.com/xid/0j44o1ma-bi090n


https://wolfram.com/xid/0j44o1ma-eu5al

Get the explicit elements of a sparse array satisfying a condition:

https://wolfram.com/xid/0j44o1ma-b8bs49

Note the more complicated pattern is needed since Cases has special behavior for Rule:

https://wolfram.com/xid/0j44o1ma-dzyqmj

SparseArray objects with positive and negative values:

https://wolfram.com/xid/0j44o1ma-f430qw


https://wolfram.com/xid/0j44o1ma-88bfs

Get the upper and lower triangular parts of a sparse matrix:

https://wolfram.com/xid/0j44o1ma-gblxqw


https://wolfram.com/xid/0j44o1ma-3x8ox


https://wolfram.com/xid/0j44o1ma-c782cp

Lower triangular part with 1s on the diagonal:

https://wolfram.com/xid/0j44o1ma-mu08uc

This just happens to be the LU decomposition of a tridiagonal matrix:

https://wolfram.com/xid/0j44o1ma-c0jwh4

Make a plot showing the positions of the explicit elements of a SparseArray with tooltips:

https://wolfram.com/xid/0j44o1ma-jn6ls1


https://wolfram.com/xid/0j44o1ma-d1e6ux

MatrixPlot generally makes a visually better plot:

https://wolfram.com/xid/0j44o1ma-gjq5e0

Properties & Relations (2)Properties of the function, and connections to other functions
For a SparseArray s, SparseArray[ArrayRules[s],Dimensions[s]] is identical to s:

https://wolfram.com/xid/0j44o1ma-omo3n


https://wolfram.com/xid/0j44o1ma-bk7t3p


https://wolfram.com/xid/0j44o1ma-d628gd

Specifying the dimensions is needed since they would be inferred from explicit elements:

https://wolfram.com/xid/0j44o1ma-b5app4


https://wolfram.com/xid/0j44o1ma-d5ybct

For an explicit array ArrayRules can be written in terms of Position:

https://wolfram.com/xid/0j44o1ma-h1ufr

https://wolfram.com/xid/0j44o1ma-boyj8a


https://wolfram.com/xid/0j44o1ma-jx9cl0


https://wolfram.com/xid/0j44o1ma-epoa97


https://wolfram.com/xid/0j44o1ma-b38as2

This will not work for SparseArray objects because pattern matching works on the FullForm:

https://wolfram.com/xid/0j44o1ma-g4nvla


https://wolfram.com/xid/0j44o1ma-lkd12g

Wolfram Research (2003), ArrayRules, Wolfram Language function, https://reference.wolfram.com/language/ref/ArrayRules.html.
Text
Wolfram Research (2003), ArrayRules, Wolfram Language function, https://reference.wolfram.com/language/ref/ArrayRules.html.
Wolfram Research (2003), ArrayRules, Wolfram Language function, https://reference.wolfram.com/language/ref/ArrayRules.html.
CMS
Wolfram Language. 2003. "ArrayRules." Wolfram Language & System Documentation Center. Wolfram Research. https://reference.wolfram.com/language/ref/ArrayRules.html.
Wolfram Language. 2003. "ArrayRules." Wolfram Language & System Documentation Center. Wolfram Research. https://reference.wolfram.com/language/ref/ArrayRules.html.
APA
Wolfram Language. (2003). ArrayRules. Wolfram Language & System Documentation Center. Retrieved from https://reference.wolfram.com/language/ref/ArrayRules.html
Wolfram Language. (2003). ArrayRules. Wolfram Language & System Documentation Center. Retrieved from https://reference.wolfram.com/language/ref/ArrayRules.html
BibTeX
@misc{reference.wolfram_2025_arrayrules, author="Wolfram Research", title="{ArrayRules}", year="2003", howpublished="\url{https://reference.wolfram.com/language/ref/ArrayRules.html}", note=[Accessed: 29-March-2025
]}
BibLaTeX
@online{reference.wolfram_2025_arrayrules, organization={Wolfram Research}, title={ArrayRules}, year={2003}, url={https://reference.wolfram.com/language/ref/ArrayRules.html}, note=[Accessed: 29-March-2025
]}