# Finding column indices of nonzero elements in a sparse matrix

**URL:** <https://discourse.julialang.org/t/finding-column-indices-of-nonzero-elements-in-a-sparse-matrix/67063>\
**Category:** General Usage\
**Tags:** sparse\
**Created:** [August 26, 2021, 9:03pm UTC](https://discourse.julialang.org/t/finding-column-indices-of-nonzero-elements-in-a-sparse-matrix/67063 "2021-08-26T21:03:36Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![yewalenikhil65](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yewalenikhil65/32/26873_2.png) [@yewalenikhil65](https://discourse.julialang.org/u/yewalenikhil65)\
**Post date:** [August 26, 2021, 9:03pm UTC](https://discourse.julialang.org/t/finding-column-indices-of-nonzero-elements-in-a-sparse-matrix/67063/1 "2021-08-26T21:03:36Z")

</div>

Is there any julia way to find column indices of nonzero elements in a sparse matrix ?  
I mean something that `nzrange(A,j)` does, but here `j` is the row index

---

<div class="post-metadata">

**Author:** ![zdenek\_hurak](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/zdenek_hurak/32/53118_2.png) [@zdenek\_hurak](https://discourse.julialang.org/u/zdenek_hurak)\
**Post date:** [August 26, 2021, 9:30pm UTC](https://discourse.julialang.org/t/finding-column-indices-of-nonzero-elements-in-a-sparse-matrix/67063/2 "2021-08-26T21:30:15Z")

</div>

Based on your description, I am not quite sure I understand what you want. Consider the following sparse matrix

```julia
julia> A = sparse([1 2 0; 0 0 3; 0 4 0])
3×3 SparseMatrixCSC{Int64, Int64} with 4 stored entries:
 1 2 ⋅
 ⋅ ⋅ 3
 ⋅ 4 ⋅

```

What should the desired outcome be in this example?

---

<div class="post-metadata">

**Author:** ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)\
**Post date:** [August 26, 2021, 9:50pm UTC](https://discourse.julialang.org/t/finding-column-indices-of-nonzero-elements-in-a-sparse-matrix/67063/3 "2021-08-26T21:50:01Z")

</div>

Does this produce the required result:  
`unique(rowvals(permutedims(A)))`

---

<div class="post-metadata">

**Author:** ![yewalenikhil65](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yewalenikhil65/32/26873_2.png) [@yewalenikhil65](https://discourse.julialang.org/u/yewalenikhil65)\
**Post date:** [August 27, 2021, 5:22am UTC](https://discourse.julialang.org/t/finding-column-indices-of-nonzero-elements-in-a-sparse-matrix/67063/4 "2021-08-27T05:22:20Z")

</div>

@rafael.guerra No this doesnt produce the required result.

@zdenek_hurak  
what i wanted was something like this,

```julia
A = sparse([1 2 0 ;0 3 0 ;0 0 4])
julia> nzrange(sparse(A'),1)
1:2
julia> nzrange(sparse(A'),2)
3:3

```

I wished to know if there is way to avoid transposing and get resuts as `nzrange` applied on the rows of sparse matrix

---

<div class="post-metadata">

**Author:** ![yewalenikhil65](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yewalenikhil65/32/26873_2.png) [@yewalenikhil65](https://discourse.julialang.org/u/yewalenikhil65)\
**Post date:** [August 27, 2021, 5:34am UTC](https://discourse.julialang.org/t/finding-column-indices-of-nonzero-elements-in-a-sparse-matrix/67063/5 "2021-08-27T05:34:23Z")

</div>

I have a rough way of doing this, Just wish to know if there is more efficient way.

```julia
julia> A = sparse([1 2 0 ;0 3 0 ;0 0 4])
3×3 SparseMatrixCSC{Int64, Int64} with 4 stored entries:
 1 2 ⋅
 ⋅ 3 ⋅
 ⋅ ⋅ 4

(Is,Js,Vs)=findnz(A)
julia> a = sparse(Js,Is,Vs,size(A,2),size(A,1))
3×3 SparseMatrixCSC{Int64, Int64} with 4 stored entries:
 1 ⋅ ⋅
 2 3 ⋅
 ⋅ ⋅ 4

```

This gives

```julia
julia> nzrange(a,1)
1:2

julia> nzrange(a,2)
3:3

```

---

<div class="post-metadata">

**Author:** ![artkuo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/artkuo/32/28577_2.png) [@artkuo](https://discourse.julialang.org/u/artkuo)\
**Post date:** [August 27, 2021, 9:00pm UTC](https://discourse.julialang.org/t/finding-column-indices-of-nonzero-elements-in-a-sparse-matrix/67063/6 "2021-08-27T21:00:10Z")

</div>

This may do what you want, assuming it is correct to get contiguous ranges:

```julia
julia> using SparseArrays

julia> A = sparse([1 2 0 ;0 3 0 ;0 0 4]);

julia> findnzbyrow(A,row) = (x->findfirst(x):findlast(x))(findnz(A)[1] .== row)
findnzbyrow (generic function with 1 method)

julia> findnzbyrow(A,1)
1:2

julia> findnzbyrow(A,2)
3:3

```

This is better than transposing by hand, which probably makes copies. However, it’s slightly clunky because the `findlast` repeats some of the effort of `findfirst`. There may be cool ways to avoid this, but you could always write your own `findfirstandlast`. Also depends on your use case, because it’s possible you don’t need ranges at all, and can use the bitvector produced by `findnz(A)[1] .== row`.

EDIT: In general I don’t think you can expect to get contiguous ranges for the original `A`, unless you know that your data will be arranged properly. So it’s just plain better to use bitvectors if you can. Your hand transpose will give contiguous ranges appropriate for that matrix.
