0
votes

I have a 1000-element Array{SparseMatrixCSC{Float64,Int64},1} array of sparse matrices in Julia, named A, which contains 1000 sparse matrices. I used the command sparse of the SparseArrays package of Julia, to store each sparse matrix of the Array. I present an extract below:

julia> A
1000-element Array{SparseMatrixCSC{Float64,Int64},1}:

  [1   ,    1]  =  0.994372
  [2   ,    1]  =  0.991773
  [3   ,    1]  =  0.992271
  [4   ,    1]  =  0.998889
  [5   ,    1]  =  0.992853
  [6   ,    1]  =  0.998921
  [7   ,    1]  =  0.98486
  [8   ,    1]  =  0.988783
  [9   ,    1]  =  0.995152
  ⋮
  [1492,   42]  =  0.955595
  [1493,   42]  =  0.982923
  [1494,   42]  =  0.951944
  [1495,   42]  =  1.0
  [1496,   42]  =  0.975999
  [1497,   42]  =  0.954872
  [1498,   42]  =  0.963355
  [1499,   42]  =  0.925815
  [1500,   42]  =  0.93627

  [1   ,    1]  =  0.975476
  [2   ,    1]  =  0.977395
  [3   ,    1]  =  0.996842
  [4   ,    1]  =  0.996767
  [5   ,    1]  =  0.998007
  [6   ,    1]  =  0.996788
  [7   ,    1]  =  0.959937
  [8   ,    1]  =  0.996806
  [9   ,    1]  =  0.97679
  ⋮
  [1492,   42]  =  0.991332
  [1493,   42]  =  0.999623
  [1494,   42]  =  0.982065
  [1495,   42]  =  0.984356
  [1496,   42]  =  0.998067
  [1497,   42]  =  0.987055
  [1498,   42]  =  0.995269
  [1499,   42]  =  0.977139
  [1500,   42]  =  0.98173
  ....

I want to apply the following function to A:

map(function maxkernLY(x) map(y->y[2],mapslices(findmax, x, dims=2)) end,A)

The function takes each matrix of the Array, and for each row of the selected matrix, it looks for the maximum value. When A is composed by dense matrices, the function works perfectly, but when A is composed by sparse matrices like above, I get the following error:

MethodError: no method matching zero(::Type{Tuple{Float64,Int64}})

Any hint? Could be any Array of sparse matrices, even an array of 2 small sparse matrices, not necessarily the example above.

1

1 Answers

1
votes

Does this:

getindex.(findmax.(A, dims=2), 2)

give you what you want? (a slight difference from your code is that it returns indices within the whole arrays not within rows, but this can be simply fixed if you do not like it; in practice these double indices might be even easier to work with later).

Regarding your original code - this seems to be a bug in Julia. Which is confirmed when you read the definition of setindex! around line 2677 in \SparseArrays\src\sparsematrix.jl.

EDIT

If you want to use mapslices you can use something like this:

map(x -> mapslices(t -> collect(findmax(t)), x, dims=2)[:, 2], A)

or

getindex.(mapslices.(t -> collect(findmax(t)), A, dims=2), :, 2)

this will give you an equivalent result to your original code.