# Printing zero elements in lines of a sparse matrix

**URL:** <https://discourse.julialang.org/t/printing-zero-elements-in-lines-of-a-sparse-matrix/94530>\
**Category:** General Usage\
**Created:** [February 12, 2023, 8:13pm UTC](https://discourse.julialang.org/t/printing-zero-elements-in-lines-of-a-sparse-matrix/94530 "2023-02-12T20:13:20Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Gravlax](https://avatars.discourse-cdn.com/v4/letter/g/dbc845/32.png) [@Gravlax](https://discourse.julialang.org/u/Gravlax)\
**Post date:** [February 12, 2023, 8:13pm UTC](https://discourse.julialang.org/t/printing-zero-elements-in-lines-of-a-sparse-matrix/94530/1 "2023-02-12T20:13:20Z")

</div>

Dear all,  
I would like to print the non-zero elements along a give line of a sparse matrix.  
In fact I’m trying to find a way to reproduce the behaviour of MATLAB. For example, here’s how a line is printed in MATLAB, it’s very useful:  
 ![image](https://global.discourse-cdn.com/julialang/original/3X/a/7/a7cede1b9109ce74d1ee84be707e0e99296f2b09.png)  
So far, I’ve tried a similar approach with Julia but it seems that it converts the row to a dense row (hence full of zeros) before printing it. Any hint regarding printing sparse matrix elements?  
Cheers!

---

<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:** [February 12, 2023, 8:49pm UTC](https://discourse.julialang.org/t/printing-zero-elements-in-lines-of-a-sparse-matrix/94530/2 "2023-02-12T20:49:22Z")

</div>

IIUC this is pretty much the normal behaviour in Julia:

```julia
julia> using SparseArrays

julia> sp = sprand(10,10,0.1)
10×10 SparseMatrixCSC{Float64, Int64} with 11 stored entries:
  ⋅ ⋅ ⋅ 0.27207 ⋅ ⋅ ⋅ ⋅ ⋅ ⋅ 
  ⋅ ⋅ ⋅ ⋅ ⋅ ⋅ ⋅ ⋅ ⋅ ⋅ 
  ⋅ ⋅ ⋅ ⋅ ⋅ ⋅ ⋅ ⋅ ⋅ ⋅ 
  ⋅ ⋅ ⋅ ⋅ ⋅ ⋅ ⋅ ⋅ ⋅ ⋅ 
  ⋅ ⋅ ⋅ ⋅ ⋅ ⋅ ⋅ ⋅ 0.0865405 0.9079
  ⋅ ⋅ ⋅ 0.246534 ⋅ ⋅ 0.743724 0.611045 ⋅ ⋅ 
  ⋅ ⋅ ⋅ ⋅ ⋅ ⋅ ⋅ ⋅ 0.684832 ⋅ 
  ⋅ ⋅ ⋅ 0.626594 ⋅ ⋅ ⋅ ⋅ ⋅ ⋅ 
  ⋅ ⋅ ⋅ 0.541967 ⋅ ⋅ ⋅ ⋅ ⋅ ⋅ 
  ⋅ ⋅ ⋅ ⋅ ⋅ ⋅ ⋅ ⋅ 0.330361 0.940725

julia> sp[:,4]
10-element SparseVector{Float64, Int64} with 4 stored entries:
  [1] = 0.27207
  [6] = 0.246534
  [8] = 0.626594
  [9] = 0.541967

```

---

<div class="post-metadata">

**Author:** ![Gravlax](https://avatars.discourse-cdn.com/v4/letter/g/dbc845/32.png) [@Gravlax](https://discourse.julialang.org/u/Gravlax)\
**Post date:** [February 12, 2023, 9:07pm UTC](https://discourse.julialang.org/t/printing-zero-elements-in-lines-of-a-sparse-matrix/94530/3 "2023-02-12T21:07:36Z")

</div>

Hmmm OK, thanks!  
I confirm I get the same result than you from execution within the REPL.  
In my case I was running within a script:

```julia
 println(typeof(Kuusc))
 println(Kuusc[45,:])

```

and what comes out is:

 ![image](https://global.discourse-cdn.com/julialang/original/3X/0/b/0b4e481e1615e8fb7980515844eaed6a214b42d6.png) and so on  
Same with `@show`.  
Maybe, this is because I’m doing this in the middle of a script and not from the REPL directly (and in MATLAB I’m running in the debugger…).

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [February 12, 2023, 9:15pm UTC](https://discourse.julialang.org/t/printing-zero-elements-in-lines-of-a-sparse-matrix/94530/4 "2023-02-12T21:15:46Z")

</div>

See [pretty-printing in Julia](https://docs.julialang.org/en/v1/manual/types/#man-custom-pretty-printing) — `println` uses `show(io, x)`, but the REPL display uses `show(io, "text/plain", x)`. So do e.g.

```julia
show(stdout, "text/plain", Kuusc[45,:])

```

PS. Please [use quoted text](https://discourse.julialang.org/t/please-read-make-it-easier-to-help-you/14757), _not_ screenshots.

---

<div class="post-metadata">

**Author:** ![Gravlax](https://avatars.discourse-cdn.com/v4/letter/g/dbc845/32.png) [@Gravlax](https://discourse.julialang.org/u/Gravlax)\
**Post date:** [February 13, 2023, 6:56am UTC](https://discourse.julialang.org/t/printing-zero-elements-in-lines-of-a-sparse-matrix/94530/5 "2023-02-13T06:56:38Z")

</div>

> [@stevengj](#):
>
> `show(stdout, "text/plain", Kuusc[45,:])`

Thanks a lot, I had no clue about this difference in printing style.  
I have tried your suggestion: the printing is indeed different but still not similar to REPL. Here’s what comes out:

```julia
840-element view(::SparseMatrixCSC{Float64, Int64}, 43, :) with eltype Float64:
 0.0
 0.0
 0.0
 0.0
 0.0
 0.0
 0.0

```

I’ve truncated it because it does print all elements of the sparse matrix. Sorry for the screenshots, I’ve partly edited my post above.

---

<div class="post-metadata">

**Author:** ![Wemko](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/wemko/32/33624_2.png) [@Wemko](https://discourse.julialang.org/u/Wemko)\
**Post date:** [February 13, 2023, 7:50am UTC](https://discourse.julialang.org/t/printing-zero-elements-in-lines-of-a-sparse-matrix/94530/6 "2023-02-13T07:50:04Z")

</div>

Did you use views function in the print or show function? If not, why the type of output is a view?

---

<div class="post-metadata">

**Author:** ![Gravlax](https://avatars.discourse-cdn.com/v4/letter/g/dbc845/32.png) [@Gravlax](https://discourse.julialang.org/u/Gravlax)\
**Post date:** [February 13, 2023, 7:55am UTC](https://discourse.julialang.org/t/printing-zero-elements-in-lines-of-a-sparse-matrix/94530/7 "2023-02-13T07:55:43Z")

</div>

Thanks for point ting this out. I understood why this was a view. My main function was decorated with the `@views` macro.  
Now, I’ve removed it and the printing suggested by @stevengj works perfectly!  
Thanks a lot to all of you!
