Questions tagged [sparse-array]

In computer science, a sparse array is an array in which most of the elements have the same value (known as the default value—usually 0 or null).

In computer science, a sparse array is an array in which most of the elements have the same value (known as the default value—usually 0 or null).

The occurrence of zero elements in a large array is inefficient for both computation and storage. An array in which there is a large number of zero elements is referred to as being sparse.

In the case of sparse arrays, one can ask for a value from an "empty" array position. If one does this, then for an array of numbers, a value of zero should be returned, and for an array of objects, a value of null should be returned.

A naive implementation of an array may allocate space for the entire array, but in the case where there are few non-default values, this implementation is inefficient. Typically the algorithm used instead of an ordinary array is determined by other known features (or statistical features) of the array. For instance, if the sparsity is known in advance or if the elements are arranged according to some function (e.g., the elements occur in blocks). A heap memory allocator in a program might choose to store regions of blank space in a linked list rather than storing all of the allocated regions in, say a bit array.

Wikipedia: http://en.wikipedia.org/wiki/Sparse_array

94 questions
8
votes
3 answers

Efficient alternative to Outer on sparse arrays in Mathematica?

Suppose I have two very large lists {a1, a2, …} and {b1, b2, …} where all ai and bj are large sparse arrays. For the sake of memory efficiency I store each list as one comprehensive sparse array. Now I would like to compute some function f on all…
groovybaby
  • 181
  • 2
8
votes
3 answers

Matlab: First Non-zero element of each row or column

For example, A = [ -1 0 -2 0 0 2 8 0 1 0 0 0 3 0 -2 0 -3 2 0 0 1 2 0 0 -4]; how can I get a vector of the first nonzero elements of each row?
user2913990
  • 107
  • 1
  • 1
  • 2
8
votes
3 answers

How to initialize a static SparseArray

How can I initialize a static, unmodifiable instance of android.util.SparseArray?
firyice
  • 497
  • 6
  • 23
7
votes
1 answer

SparseArray remove() and delete() what's the difference?

What is the main difference between calling remove() or delete() on a SparseArray, because they both accept key's as arguments. Thanks.
Moshe Rabaev
  • 1,446
  • 10
  • 27
7
votes
3 answers

Sparse array compression using SIMD (AVX2)

I have a sparse array a (mostly zeroes): unsigned char a[1000000]; and I would like to create an array b of indexes to non-zero elements of a using SIMD instructions on Intel x64 architecture with AVX2. I'm looking for tips how to do it…
Paul Jurczak
  • 5,766
  • 36
  • 58
7
votes
6 answers

Condensing a sparse array in Javascript?

I have an array of elements where the entries are sparse. How can I easily condense the sparse array into a dense array so that I don't have to keep checking for null and undefined values every time I loop through the data? Here is some example…
Ivan
  • 8,938
  • 11
  • 45
  • 77
6
votes
2 answers

Efficient storage for a sparse octree?

Can anyone suggest a fast, efficient method for storing and accessing a sparse octree? Preferably something that can be easily implemented in HLSL. (I'm working a raycasting/voxel app) In this instance, the tree can be precalculated, so I'm mostly…
3Dave
  • 26,903
  • 17
  • 82
  • 145
6
votes
7 answers

Get first element of a sparse JavaScript array

I have an array of objects in javascript. I use jquery. How do i get the first element in the array? I cant use the array index - as I assign each elements index when I am adding the objects to the array. So the indexes arent 0, 1, 2 etc. Just need…
amateur
  • 40,217
  • 59
  • 181
  • 303
6
votes
2 answers

Memory consumption of sparse arrays in Node.js

I have written a small program that produces arrays, which runs quite long (almost forever ;-)): var results = []; var i = 1; while (true) { console.log(i++); results.push([]); } When, instead of an empty array, I create a sparse array of…
Golo Roden
  • 112,924
  • 78
  • 260
  • 376
6
votes
2 answers

einsum on a sparse matrix

It seems numpy's einsum function does not work with scipy.sparse matrices. Are there alternatives to do the sorts of things einsum can do with sparse matrices? In response to @eickenberg's answer: The particular einsum I'm wanting to is…
drevicko
  • 12,832
  • 12
  • 63
  • 87
6
votes
2 answers

How to store sparsearray in bundle

I have a SparseArray and want to store it in bundle in onSaveInstanceState method in my activity and restore it in oncreate. I found putSparseParcelableArray method for put SparseArray in bundle and did this in onSaveInstanceState…
Ehsan
  • 380
  • 4
  • 15
5
votes
3 answers

Loading Matlab sparse matrix saved with -v7.3 (HDF5) into Python and operating on it

I'm new to python, coming from matlab. I have a large sparse matrix saved in matlab v7.3 (HDF5) format. I've so far found two ways of loading in the file, using h5py and tables. However operating on the matrix seems to be extremely slow after…
tdc
  • 7,149
  • 11
  • 38
  • 61
5
votes
1 answer

HashMap, SparseArray in Android: multithread a concern?

I know neither HashMap nor SparseArray are threadsafe. Do I have to worry about that if I have a central data repository as HashMap which can be accessed by the activity and potentially by an AsyncTask? Would it be advisable to use a HashTable or…
5
votes
1 answer

How to use SparseArray as a source for Adapter?

I have a sparse array of values which I want to populate in a Spinner, and when the item is selected, I want to get the id (which is the key from the sparse array). What is the preferred way of creating an adapter from a SparseArray? Is it…
5
votes
1 answer

Pandas sparse dataframe larger on disk than dense version

I find that the sparse versions of a dataframe are actually much larger when saved to disk than dense versions. What am I doing wrong? test = pd.DataFrame(ones((4,4000))) test.ix[:,:] = nan test.ix[0,0] = 47 test.to_hdf('test3',…
jeffalstott
  • 2,483
  • 3
  • 22
  • 31