# SparseMatrixCSC in Julia?

**URL:** https://discourse.julialang.org/t/sparsematrixcsc-in-julia/26731
**Category:** General Usage
**Created:** [July 24, 2019, 11:47am UTC](https://discourse.julialang.org/t/sparsematrixcsc-in-julia/26731 "2019-07-24T11:47:10Z")
**Posts on this page:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![bsnyh](https://avatars.discourse-cdn.com/v4/letter/b/ce7236/32.png) [@bsnyh](https://discourse.julialang.org/u/bsnyh)
#### Post date: [July 24, 2019, 11:47am UTC](https://discourse.julialang.org/t/sparsematrixcsc-in-julia/26731/1 "2019-07-24T11:47:10Z")

</div>

Hi, I’m curious about this SparseMatrixCSC object in Julia. The following is the constructor. I was wondering, how to understand that “The row indices in every column need to be sorted. If your `SparseMatrixCSC` object contains unsorted row indices, one quick way to sort them is by doing a double transpose.” ?

```julia
struct SparseMatrixCSC{Tv,Ti<:Integer} <: AbstractSparseMatrix{Tv,Ti}
    m::Int # Number of rows
    n::Int # Number of columns
    colptr::Vector{Ti} # Column i is in colptr[i]:(colptr[i+1]-1)
    rowval::Vector{Ti} # Row indices of stored values
    nzval::Vector{Tv} # Stored values, typically nonzeros
end

```

Thank you a lot for your time and attention !

---

<div class="post-metadata">

### Author: ![foobar\_lv2](https://avatars.discourse-cdn.com/v4/letter/f/ee59a6/32.png) [@foobar\_lv2](https://discourse.julialang.org/u/foobar_lv2)
#### Post date: [July 24, 2019, 12:10pm UTC](https://discourse.julialang.org/t/sparsematrixcsc-in-julia/26731/2 "2019-07-24T12:10:07Z")

</div>

When trying to access `A[j,i]`, then `colptr[i]:(colptr[i+1]-1)` is the range of indices where the values (`nzval`) and row indices (`rowval`) are stored. Next, we need to find `j` in `rowval[lo:hi]`.

`rowval[lo:hi]` is kept sorted, in order to permit binary search (log time). If you somehow end up with a SparseMatrixCSC that violates this invariant, then `A[j,i]` can give wrong answers (return zero, even if a nonzero entry exists). The comment about double transpose gives a quick-and-dirty trick to recover from such an invalid situation.
