## \$. y (Sparse)

The Sparse verb changes how nouns are stored. "Sparse" provides a more efficient method for storing sparse arrays, but doesn't affect how the noun operates in your programs.

a =.  ?  100000 \$ 1000  NB. Create a vector "a" of 10,000 random numbers between zero and 999

ba =. a = 999           NB. This places a "1" in array "ba" wherever the integer 999 appeared in array "a"
NB. "ba" is a binary vector of 10,000 zeros and ones

+/ba                    NB. How many ones are there in ba?
95                         NB. There are only 95 ones out of the 10,000 binary digits in ba.
NB. The rest are zeros. So ba is a sparse array.

sba=. \$. ba             NB. Apply the Sparse verb to ba
NB. This creates a noun "sba" that J will treat just the same as "ba",
NB. However,"sba" will bestored more efficiently.

\$ba
100000
\$sba
100000                     NB. Both ba and sba are the same size

ba -: sba               NB. As far as the J interpreter is concerned, ba and sba are identical.
1

7!:5 <'ba'              NB. Let's look at the storage space taken up by both nouns
131072                     NB. "ba" takes up 131,072 bytes
7!:5 <'sba'
1664                       NB. "sba" takes up 1,1664 bytes

NB. If an array is sparse, the Sparse verb can reduce the storage space it requires without affecting its operation in your program.

NB. What happens if you try to Sparse an array that isn't sparse?  Let's see:

20 {. a                 NB. "a" consists of random numbers between 0 and 999.
406 471 620 537 308 289 692 964 458 301 929 778 316 611 841 741 346 385 849 570
NB. So our original array "a" isn't sparse.

7!:5 <'a'               NB. How much space does "a" take up?
1.04858e6

sa=. \$. a               NB. Let's apply "Sparse" to "a" and see what happens.

a -: sa                 NB. The interpreter says they are the same
1

7!:5 <'sa'              NB. However, the sparse version of "a" takes up twice as much space as the normal version.
2.09754e6

NB. If an array isn't sparse, the Sparse verb shouldn't be used to try to reduce storage, because it will increase storage instead.

### Common uses

You can use Sparse when you have large arrays containing only a few values, to save storage.

sample code

WRITEHERE

sample code

WRITEHERE

sample code