# Sparse arrays of higher order

**URL:** <https://discourse.julialang.org/t/sparse-arrays-of-higher-order/42875>\
**Category:** General Usage\
**Tags:** array, sparsearrays\
**Created:** [July 10, 2020, 9:42pm UTC](https://discourse.julialang.org/t/sparse-arrays-of-higher-order/42875 "2020-07-10T21:42:19Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![chakravala](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chakravala/32/6832_2.png) [@chakravala](https://discourse.julialang.org/u/chakravala)\
**Post date:** [July 10, 2020, 9:42pm UTC](https://discourse.julialang.org/t/sparse-arrays-of-higher-order/42875/1 "2020-07-10T21:42:19Z")

</div>

Are there any good packages which have a similar feature as `SparseArrays.SparseMatrixCSC` but with higher order arrays? For example, a sparse array with 3 indices or 4 indices, stored in a similar way to a sparse matrix, except for having more indices.

---

<div class="post-metadata">

**Author:** ![Oscar\_Smith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oscar_smith/32/25343_2.png) [@Oscar\_Smith](https://discourse.julialang.org/u/Oscar_Smith)\
**Post date:** [July 10, 2020, 11:50pm UTC](https://discourse.julialang.org/t/sparse-arrays-of-higher-order/42875/2 "2020-07-10T23:50:37Z")

</div>

There isn’t anything in stdlib I know of that does this. Part of the reason is that in higher dimensions indices take more room to store so arrays need to be sparser for it to be worth it. Also higher dimensional sparse data often has specific structure worth exploiting. That said, you can make a Dictionary of Keys or array of sparse matrices pretty easily though.

---

<div class="post-metadata">

**Author:** ![dpsanders](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dpsanders/32/3573_2.png) [@dpsanders](https://discourse.julialang.org/u/dpsanders)\
**Post date:** [July 11, 2020, 3:43am UTC](https://discourse.julialang.org/t/sparse-arrays-of-higher-order/42875/3 "2020-07-11T03:43:31Z")

</div>

What’s a DOK?

Is a dictionary mapping tuples (or SVectors) to keys too slow for this? (Or maybe DOK is Dictionary of Keys?)

---

<div class="post-metadata">

**Author:** ![piever](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/piever/32/1815_2.png) [@piever](https://discourse.julialang.org/u/piever)\
**Post date:** [July 11, 2020, 10:24am UTC](https://discourse.julialang.org/t/sparse-arrays-of-higher-order/42875/4 "2020-07-11T10:24:08Z")

</div>

You could do a `StructArray` of tuples and keep it sorted (`sortperm` on `StructArray{NTuple{N, Int}}` is much faster than on `Vector{NTuple{N, Int}}`), and use a regular vector for the values. Then, you’d use `searchsortedfirst` to find the keys. That’s pretty much what `IndexedTables.NDTable` does, so you could also use that directly.

---

<div class="post-metadata">

**Author:** ![raphasampaio](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/raphasampaio/32/218602_2.png) [@raphasampaio](https://discourse.julialang.org/u/raphasampaio)\
**Post date:** [September 17, 2025, 2:12am UTC](https://discourse.julialang.org/t/sparse-arrays-of-higher-order/42875/5 "2025-09-17T02:12:46Z")

</div>

There’s an n-dimensional sparse array package that might be useful: [GitHub - JuliaSparse/NDimensionalSparseArrays.jl: A Julia package for n-dimensional sparse arrays](https://github.com/JuliaSparse/NDimensionalSparseArrays.jl)
