Search code examples
matlabmatrixoctaveparticles

how to create a 3d sparse like matrix in octave


my Problem is difficult or too simple. So I've been using sparse to creat a matrix from different vectors (example here)

I use sparse(i,j,s,m,n) where i and j are allocations for particles i derived with floor etc. (it is particle in cell simulation).

this is for 1D. I want to vectorize this problem for 2D and got the problem that i can't creat 3D sparse matrices in octave.

I'm really no pro with octave and i'm searching for a solution that i can put the values from s nicely into a matrix according to the i and j values.

for example: i got particle one in cell x=2 and y=2 than i want to have a matrix with value from particle one in (1,2,2,value). is there a way without much if's and for's?


Solution

  • At the moment is not possible to have N-dimensional sparse matrices in Octave. You can see this with the following example which tries to reshape a 3x9 sparse matrix into 3x3x3:

    octave> sp = sparse (2, 6, 1, 3, 9)
    sp =
    
    Compressed Column Sparse (rows = 3, cols = 9, nnz = 1 [3.7%])
    
      (2, 6) ->  1
    
    octave> reshape (sp, [3 3 3])
    warning: reshape: sparse reshape to N-d array smashes dims
    ans =
    
    Compressed Column Sparse (rows = 3, cols = 9, nnz = 1 [3.7%])
    
      (2, 6) ->  1
    

    What you can do is to have a cell array of sparse matrices. So for a particle at KxMxN you could have data{k} = sparse (M, N, value) and access it with data{k}(m,n). It's not ideal but depending on how you get your data organized you may make things more or less readable.