# Efficiently extracting a column of a sparse matrix

**URL:** https://discourse.julialang.org/t/efficiently-extracting-a-column-of-a-sparse-matrix/114904
**Category:** New to Julia
**Tags:** sparsearrays
**Created:** [May 29, 2024, 7:56am UTC](https://discourse.julialang.org/t/efficiently-extracting-a-column-of-a-sparse-matrix/114904 "2024-05-29T07:56:53Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![gtgt](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gtgt/32/46932_2.png) [@gtgt](https://discourse.julialang.org/u/gtgt)
#### Post date: [May 29, 2024, 7:56am UTC](https://discourse.julialang.org/t/efficiently-extracting-a-column-of-a-sparse-matrix/114904/1 "2024-05-29T07:56:53Z")

</div>

Given a sparse matrix `S`, how can I efficiently extract the `i`th column of `S`? I know I can use `@view S.nzval[S.colptr[i] : S.colptr[i + 1] - 1]` but it’s a bit clunky.

---

<div class="post-metadata">

### Author: ![mikmoore](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mikmoore/32/31109_2.png) [@mikmoore](https://discourse.julialang.org/u/mikmoore)
#### Post date: [May 29, 2024, 2:47pm UTC](https://discourse.julialang.org/t/efficiently-extracting-a-column-of-a-sparse-matrix/114904/2 "2024-05-29T14:47:18Z")

</div>

Note that what you wrote only extracts the structural nonzeros of the column, not the full column. You also need to attach the row indices and rewrap in a sparse vector. But I’ll assume you knew that and simply misspoke.

There is an efficient method for getting a _copy_ (not view) of a column simply via `S[:,col]` (see `@less S[:,1]`). It does contain the code you wrote as part of it. You could create a version of the builtin that uses `@view`. Be warned that any mutation of the nonzero structure may cause it to diverge from or corrupt the original.

It may be that a specialized `view` type is deliberately unsupported due to this fragility. But I’ll remark that `SparseMatrixCSC` (the standard sparse array type) supports O(1) random access to columns, so the generic `@view` is probably mostly okay. They even specialize a couple of methods (`*`, `nnz`, and `nonzeros`) on column views of `AbstractSparseMatrixCSC` (via the `SparseArrays.SparseColumnView` alias), but maybe not all the methods you would hope for.

So the answer to your question is “the most efficient way is what you’re proposing (with the bug fixed, like `S[:,1]`)”. But I would see whether a standard column view (or non-view) comes close in performance and prefer that if it’s close, to outsource that headache.

---

<div class="post-metadata">

### Author: ![gtgt](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gtgt/32/46932_2.png) [@gtgt](https://discourse.julialang.org/u/gtgt)
#### Post date: [May 29, 2024, 2:52pm UTC](https://discourse.julialang.org/t/efficiently-extracting-a-column-of-a-sparse-matrix/114904/3 "2024-05-29T14:52:52Z")

</div>

Sorry, I meant to write “compressed column” instead of column.

---

<div class="post-metadata">

### Author: ![Dan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dan/32/42581_2.png) [@Dan](https://discourse.julialang.org/u/Dan)
#### Post date: [May 29, 2024, 3:21pm UTC](https://discourse.julialang.org/t/efficiently-extracting-a-column-of-a-sparse-matrix/114904/4 "2024-05-29T15:21:04Z")

</div>

The following should be efficient:

```julia
julia> using SparseArrays

julia> M = sprand(5,5,0.3)
5×5 SparseMatrixCSC{Float64, Int64} with 5 stored entries:
  ⋅ 0.701338 ⋅ ⋅ ⋅ 
  ⋅ ⋅ ⋅ ⋅ 0.804534
  ⋅ 0.700287 ⋅ ⋅ 0.131449
  ⋅ ⋅ 0.15562 ⋅ ⋅ 
  ⋅ ⋅ ⋅ ⋅ ⋅ 

julia> v = @view M[:,2]
5-element view(::SparseMatrixCSC{Float64, Int64}, :, 2) with eltype Float64:
 0.7013376258268622
 0.0
 0.7002872223003926
 0.0
 0.0

julia> nonzeros(v)
2-element view(::Vector{Float64}, 1:2) with eltype Float64:
 0.7013376258268622
 0.7002872223003926

```
