0

I want to create a sparse cell that can be used as a 2D accumulate structure.

      1        2        3        4
1   [1]    [3,2]  [2,3,2]    [1,2]
2 [1,3]    [1,2]      [2]    [1,4]
3   [2]    [1,2]    [2,3]      [1]

The dimension of the matrix is around 10e6 Here are the few things I though of:

Sparse Matrix

Of course this won't work since the element in the matrix should be array.

Cell

This works somehow. But the memory is extremely heavy. It takes tons of memory at the very first assignment! Though it is said that cell structure is sparse itself, it doesn't appear practical in my case.

>> whos
  Name         Size                   Bytes  Class     Attributes

  C         9357x12363            925445184  cell                

Map

I though of using map to store the set where the coordinate being the key. However I can only find ways to assign 'char' as keytype, rather than two-dimensional coordinates.

SolessChong
  • 3,370
  • 8
  • 40
  • 67
  • You can use the third option `Map` with strings(char type) with `0` as the delimiter if you know that `0` won't be a coordinate? In that case, row-1, col-3 would be `'20302'`. – Divakar Apr 20 '14 at 12:58
  • That's un-elegant man – SolessChong Apr 20 '14 at 13:00
  • 1
    I meant a cell array of size `9357x12363`, with each cell a character like `'20302'` to denote `[2,3,2]`, etc.. A cell array of strings doesn't sound too un-elegant I guess. On the memory side, it could be more efficient than the cell array format you have. – Divakar Apr 20 '14 at 13:26
  • 1
    You could also use `'2,3,2'` to denote `[2,3,2]`, then you could use `0`. – Rafael Monteiro Apr 20 '14 at 14:26
  • What about using an array of sparse matrices? Then the first matrix would contain the first element in each array, the second matrix the second element (if it exists) and so on... – riklund Apr 20 '14 at 15:00
  • You could always use a sparse matrix and map your original `m,n` to a single `uint32`. In your example, the value of `C(1,3,:)` would be stored in `M(bitshift(1,16)+3,:)`. – beaker Apr 20 '14 at 15:26
  • What is the maximum value you want to store in each cell? What is the maximum number of values in each cell? Are they all integer? – Luis Mendo Apr 20 '14 at 16:45
  • @Divakar Agree on the latter part of your comment. Plain cell structure is really memory-consuming. – SolessChong Apr 21 '14 at 01:23
  • @beaker It should work I guess. However it's too much c++ style. I want some MATLAB-ish code. :) – SolessChong Apr 21 '14 at 01:24
  • @riklund The number of elements in the array is undetermined, which can be really large in some situations. – SolessChong Apr 21 '14 at 01:26
  • @SolessChong In that case, use `sub2ind`. It does the same thing but demands that you declare the matrix size up front. – beaker Apr 21 '14 at 02:28

0 Answers0