This is documentation for Mathematica 8, which was
based on an earlier version of the Wolfram Language.
View current documentation (Version 11.1)

ArrayRules

ArrayRules[SparseArray[...]]
gives the rules specifying elements in a sparse array.
ArrayRules[list]
gives rules for SparseArray[list].
  • The last element of ArrayRules[s] is always , where def is the default value for unspecified elements in the sparse array. »
Get the explicit elements in a SparseArray:
These rules are sufficient to efficiently construct an identical SparseArray:
Get the explicit elements in a SparseArray:
In[1]:=
Click for copyable input
Out[1]=
In[2]:=
Click for copyable input
Out[2]=
These rules are sufficient to efficiently construct an identical SparseArray:
In[3]:=
Click for copyable input
Out[3]=
In[4]:=
Click for copyable input
Out[4]=
The last element of ArrayRules[s] is always :
A SparseArray with a default value of 2:
You can override this by explicitly specifying what default you would like:
These will construct a SparseArray identical to SparseArray[m, Automatic, 1]:
Positions of 1 in an explicit array with the default taken to be 0:
These will construct a SparseArray identical to SparseArray[a]:
Positions of 0 with 1 taken as default:
These will construct a SparseArray identical to SparseArray[a, Automatic, 1]:
Get the number of explicit elements in a SparseArray:
Get the explicit elements of a sparse array satisfying a condition:
Note the more complicated pattern is needed since Cases has special behavior for Rule:
SparseArray objects with positive and negative values:
Get the upper and lower triangular parts of a sparse matrix:
Lower triangular part with 1s on the diagonal:
This just happens to be the LU decomposition of a tridiagonal matrix:
Make a plot showing the positions of the explicit elements of a SparseArray with tooltips:
MatrixPlot generally makes a visually better plot:
For a SparseArray s, SparseArray[ArrayRules[s], Dimensions[s]] is identical to s:
Specifying the dimensions is needed since they would be inferred from explicit elements:
For an explicit array ArrayRules can be written in terms of Position:
This will not work for SparseArray objects because pattern matching works on the FullForm:
New in 5