# Missing method to slice sparse matrices?

**URL:** https://discourse.julialang.org/t/missing-method-to-slice-sparse-matrices/18430
**Category:** General Usage
**Tags:** linearalgebra
**Created:** [December 7, 2018, 6:59pm UTC](https://discourse.julialang.org/t/missing-method-to-slice-sparse-matrices/18430 "2018-12-07T18:59:23Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![dpo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dpo/32/3335_2.png) [@dpo](https://discourse.julialang.org/u/dpo)
#### Post date: [December 7, 2018, 6:59pm UTC](https://discourse.julialang.org/t/missing-method-to-slice-sparse-matrices/18430/1 "2018-12-07T18:59:23Z")

</div>

In Julia 1.0.2,

```julia
julia> D = convert(SparseMatrixCSC{Float32,Int32}, spdiagm(0 => rand(5)))

julia> D[2:3,:]
ERROR: MethodError: no method matching getindex_traverse_col(::UnitRange{Int64}, ::Int32, ::Int64)
Closest candidates are:
  getindex_traverse_col(::AbstractUnitRange, ::Int64, ::Int64) at /Users/osx/buildbot/slave/package_osx64/build/usr/share/julia/stdlib/v1.0/SparseArrays/src/sparsematrix.jl:1971
  getindex_traverse_col(::StepRange, ::Int64, ::Int64) at /Users/osx/buildbot/slave/package_osx64/build/usr/share/julia/stdlib/v1.0/SparseArrays/src/sparsematrix.jl:1972
Stacktrace:
 [1] macro expansion at ./simdloop.jl:72 [inlined]
 [2] getindex(::SparseMatrixCSC{Float32,Int32}, ::UnitRange{Int64}, ::UnitRange{Int64}) at /Users/osx/buildbot/slave/package_osx64/build/usr/share/julia/stdlib/v1.0/SparseArrays/src/sparsematrix.jl:1995
 [3] getindex(::SparseMatrixCSC{Float32,Int32}, ::UnitRange{Int64}, ::Colon) at /Users/osx/buildbot/slave/package_osx64/build/usr/share/julia/stdlib/v1.0/SparseArrays/src/sparsematrix.jl:1934
 [4] top-level scope at none:0

```

This seems to fix it:

```julia
julia> import SparseArrays.getindex_traverse_col

julia> getindex_traverse_col(::AbstractUnitRange, lo::Ti, hi::Tj) where {Ti<:Integer,Tj<:Integer} = lo:hi

```

Is that the way to go?

---

<div class="post-metadata">

### Author: ![Pbellive](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pbellive/32/3604_2.png) [@Pbellive](https://discourse.julialang.org/u/Pbellive)
#### Post date: [December 7, 2018, 7:09pm UTC](https://discourse.julialang.org/t/missing-method-to-slice-sparse-matrices/18430/2 "2018-12-07T19:09:46Z")

</div>

This seems like a bug to me. I would suggest making the change you suggested to getindex\_traverse\_col and making a PR. Or filing an issue.

---

<div class="post-metadata">

### Author: ![mbauman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mbauman/32/31082_2.png) [@mbauman](https://discourse.julialang.org/u/mbauman)
#### Post date: [December 7, 2018, 7:09pm UTC](https://discourse.julialang.org/t/missing-method-to-slice-sparse-matrices/18430/3 "2018-12-07T19:09:51Z")

</div>

Yup, that looks like the right fix. Want to submit a pull request with this change? note that you don’t need the type parameters — you can just do `::Integer` here:

```
getindex_traverse_col(::AbstractUnitRange, lo::Integer, hi::Integer) = ...

```

It’d also be good to fix the `::StepRange` method similarly.

---

<div class="post-metadata">

### Author: ![dpo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dpo/32/3335_2.png) [@dpo](https://discourse.julialang.org/u/dpo)
#### Post date: [December 8, 2018, 7:29pm UTC](https://discourse.julialang.org/t/missing-method-to-slice-sparse-matrices/18430/4 "2018-12-08T19:29:57Z")

</div>

Thanks. Here we go: [https://github.com/JuliaLang/julia/pull/30319](https://github.com/JuliaLang/julia/pull/30319)

---

<div class="post-metadata">

### Author: ![viralbshah](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/viralbshah/32/54_2.png) [@viralbshah](https://discourse.julialang.org/u/viralbshah)
#### Post date: [December 20, 2018, 1:58am UTC](https://discourse.julialang.org/t/missing-method-to-slice-sparse-matrices/18430/5 "2018-12-20T01:58:08Z")

</div>

Thanks for reporting the issue and the PR. It is merged now.

---

<div class="post-metadata">

### Author: ![dpo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dpo/32/3335_2.png) [@dpo](https://discourse.julialang.org/u/dpo)
#### Post date: [December 20, 2018, 7:52am UTC](https://discourse.julialang.org/t/missing-method-to-slice-sparse-matrices/18430/6 "2018-12-20T07:52:15Z")

</div>

Thank you!
