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
2
votes
3 answers

What does "SparseArray - there can be gaps in the indices" mean?

Developing a program which uses hash map with integer as keys and objects as values. I keep on getting Lint warning informing SparseArray is more efficient and when I read about the same it was given in this Link that, there can be gaps in the…
Suman
  • 4,021
  • 7
  • 41
  • 63
2
votes
5 answers

SparseArray clone in Android

I am trying to iterate through a SparseArray and delete a few items. private SparseArray record; int size = record.size(); for (int index = 0; index < size; index++) { if (record.valueAt(index) < threshold){ …
nandeesh
  • 24,272
  • 6
  • 65
  • 74
1
vote
2 answers

Assignments in Wolfram Mathematica

Question about assignments and variables (* For example *) SP = SparseArray[{},5] or SP = Range[5] now we want to work with this array in some another function : (* example *) Fun[array_]:= array[[3]] = 100 ; (* set cell №3 equal to 100*) then we…
1
vote
3 answers

Is Cocoa's NSMutableArray sparse?

If I create an NSMutableArray that might have up to 2^16 elements, but will mostly be empty, will I be wasting space or is NSMutableArray implemented as a sparse array?
cfischer
  • 23,024
  • 36
  • 125
  • 209
1
vote
1 answer

Store and update huge (and sparse?) multi-dimensional array efficiently to count conditional probabilities

Just for fun I would like to count the conditional probabilities that a word (from a natural language) appears in a text, depending on the last and next to last word. I.e. I would take a huge bunch of e.g. English texts and count how often each…
fuenfundachtzig
  • 6,344
  • 11
  • 54
  • 77
1
vote
2 answers

Convert Sparsely-Populated XML List to HTML Table Using XSL

I'm trying to convert an XML file into a nice-looking HTML table using XSL, and I can't for the life of me figure out how to get the columns all lined up properly. I found a similar question (Convert XML List to HTML Table Using XSL), which was…
Nico
  • 13
  • 5
1
vote
3 answers

check if there is at least a single true value in SparseBooleanArray

I'm creating an Android app with a ListView and I'm using this line: SparseBooleanArray checkedPositions = list.getCheckedItemPositions(); Then I want to iterate over the array but only if there is at least a single value which is true in the…
Yonatan Nir
  • 8,303
  • 23
  • 80
  • 156
1
vote
1 answer

is there something like coo_matrix but for sparse vectors?

I'm trying to create a sparse vector from a series of arrays where there are some overlapping indexes. For a matrix there's a very convenient object in scipy that does exactly this: coo_matrix((data, (i, j)), [shape=(M, N)]) So if data happens to…
aaragon
  • 2,025
  • 3
  • 21
  • 50
1
vote
1 answer

Optimising Sparse Array Math

I have a sparse array: term_doc its size is 622256x715 of Float64. It is very sparse: Of its ~444,913,040 cells, only about 22,215 of them normally are nonempty. Of the 622256 rows only 4,699 are occupied though of the 715 columns all are…
Lyndon White
  • 24,284
  • 15
  • 77
  • 126
1
vote
1 answer

extract sparse rows from sparse matrix in r

I have a large sparse matrix to analyze in R. For instance: i <- c(1,3:8); j <- c(2,9,6:10); x <- 7 * (1:7) (A <- sparseMatrix(i, j, x = x)) [1,] . 7 . . . . . . . . [2,] . . . . . . . . . . [3,] . . . . . . . . 14 . [4,] . . . . . 21 …
1
vote
1 answer

indexOfValue(E) of SparseArrayCompat always return -1?

indexOfValue(E) method always return -1 while I am sure my E object exists in my SparseArray object, why? I have : static final SparseArrayCompat LOCATION_SHARING_TIME = new SparseArrayCompat(); static { LOCATION_SHARING_TIME.put(0,…
maohieng
  • 1,336
  • 16
  • 25
1
vote
1 answer

Finding values that repeat more than twice in a table

I am trying do something that in SQL might be done with "...having count(ID) > 2 ..." I want to find those values of a col in a two column subset of a dataframe that repeat more than twice. The function table gives me sparse matrix and I am not sure…
cumin
  • 411
  • 4
  • 15
1
vote
3 answers

Which is the best way to implement a sparse vector in Java?

Which is the best way to implement a sparse vector in Java? Of course the good thing would be to have something that can be manipulated quite easily (normalization, scalar product and so on) Thanks in advance
Jack
  • 125,196
  • 27
  • 216
  • 324
1
vote
2 answers

Recommended way to map between JavaScript sparse array and C# (sparse)array?

I am trying to map a JavaScript sparse array to a C# representation. What is the recommended way of doing this? It am considering using a dictionary containing the list of oridinals that contained a value in the original array. Any other…
Andrew
  • 2,305
  • 2
  • 20
  • 34
1
vote
2 answers

Creating each method for custom two dimensional array

I followed these instructions to create a custom class for two dimensional arrays. class SparseArray attr_reader :hash def initialize @hash = {} end def [](key) hash[key] ||= {} end def rows hash.length end …
migu
  • 3,990
  • 5
  • 33
  • 50