# Remove zero from Matrix

**URL:** <https://discourse.julialang.org/t/remove-zero-from-matrix/102322>\
**Category:** General Usage\
**Created:** [July 31, 2023, 5:04pm UTC](https://discourse.julialang.org/t/remove-zero-from-matrix/102322 "2023-07-31T17:04:00Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Sergey\_Novak](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sergey_novak/32/37716_2.png) [@Sergey\_Novak](https://discourse.julialang.org/u/Sergey_Novak)\
**Post date:** [July 31, 2023, 5:04pm UTC](https://discourse.julialang.org/t/remove-zero-from-matrix/102322/1 "2023-07-31T17:04:01Z")

</div>

Hello, I have a matrix of the following form

```julia
20×3 Matrix{Float64}:
 0.0 0.0 0.0
 0.0 0.0 0.0
 9.94549e-6 1.0e-5 -6.21704e-6
 0.0 0.0 0.0
 0.0 0.0 0.0
 1.01235e-5 2.78136e-5 -6.45014e-6
 0.0 0.0 0.0
 0.0 0.0 0.0
 0.0 0.0 0.0
 0.0 0.0 0.0
 0.0 0.0 0.0
 0.0 0.0 0.0
 0.0 0.0 0.0
 9.97805e-6 1.0e-5 -7.118e-6
 0.0 0.0 0.0
 0.0 0.0 0.0
 0.0 0.0 0.0
 0.0 0.0 0.0
 9.61955e-6 1.0e-5 -5.62205e-6
 0.0 0.0 0.0

```

How can i remove all zeros type of `0.0` from matrix?  
I need to build the second and third columns without zeros 0.0, and all I could come up with was to remove the zeros. I using CairoMakie for plot  
Thank your for help

---

<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:** [July 31, 2023, 5:22pm UTC](https://discourse.julialang.org/t/remove-zero-from-matrix/102322/2 "2023-07-31T17:22:17Z")

</div>

I’m not sure I understand what you’re after. You might need to provide an example of your desired output. But here are a couple of related options assuming your matrix is `z`

```julia
# for each column, return the nonzero values
julia> [filter(!iszero,c) for c in eachcol(z)]
3-element Vector{Vector{Float64}}:
 [9.94549e-6, 1.01235e-5, 9.97805e-6, 9.61955e-6]
 [1.0e-5, 2.78136e-5, 1.0e-5, 1.0e-5]
 [-6.21704e-6, -6.45014e-6, -7.118e-6, -5.62205e-6]

# remove rows that are entirely zero
julia> permutedims(stack(filter(!iszero,eachrow(z))))
4×3 Matrix{Float64}:
 9.94549e-6 1.0e-5 -6.21704e-6
 1.01235e-5 2.78136e-5 -6.45014e-6
 9.97805e-6 1.0e-5 -7.118e-6
 9.61955e-6 1.0e-5 -5.62205e-6

# remove zeros from each column, then reassemble (assuming the shortened columns have the same lengths)
julia> reduce(hcat, [filter(!iszero, c) for c in eachcol(z)])
4×3 Matrix{Float64}:
 9.94549e-6 1.0e-5 -6.21704e-6
 1.01235e-5 2.78136e-5 -6.45014e-6
 9.97805e-6 1.0e-5 -7.118e-6
 9.61955e-6 1.0e-5 -5.62205e-6

```

---

<div class="post-metadata">

**Author:** ![Iulian.Cioarca](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/iulian.cioarca/32/30166_2.png) [@Iulian.Cioarca](https://discourse.julialang.org/u/Iulian.Cioarca)\
**Post date:** [July 31, 2023, 5:26pm UTC](https://discourse.julialang.org/t/remove-zero-from-matrix/102322/3 "2023-07-31T17:26:25Z")

</div>

`x[x[:,1].!=0, :]`, asssuming you want to remove all zeros and each column has zeros on the same position.

---

<div class="post-metadata">

**Author:** ![Sergey\_Novak](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sergey_novak/32/37716_2.png) [@Sergey\_Novak](https://discourse.julialang.org/u/Sergey_Novak)\
**Post date:** [July 31, 2023, 5:28pm UTC](https://discourse.julialang.org/t/remove-zero-from-matrix/102322/4 "2023-07-31T17:28:51Z")

</div>

Thank you, this is exactly what I needed

---

<div class="post-metadata">

**Author:** ![nsajko](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nsajko/32/221187_2.png) [@nsajko](https://discourse.julialang.org/u/nsajko)\
**Post date:** [July 31, 2023, 5:55pm UTC](https://discourse.julialang.org/t/remove-zero-from-matrix/102322/5 "2023-07-31T17:55:13Z")

</div>

> [@Iulian.Cioarca](#):
>
> `x[x[:,1].!=0, :]`

This is nice, however I’ll note that some improvements are possible:

1. avoid the unnecessary copy: `x[(@view x[:,1]) .!= 0, :]`
2. correct for indexing bases other than one: `x[(@view x[:,begin]) .!= 0, :]`
3. using `iszero` is arguably prettier than comparing against a literal: `x[(!iszero).(@view x[:,begin]), :]`

---

<div class="post-metadata">

**Author:** ![rocco\_sprmnt21](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rocco_sprmnt21/32/20127_2.png) [@rocco\_sprmnt21](https://discourse.julialang.org/u/rocco_sprmnt21)\
**Post date:** [July 31, 2023, 6:34pm UTC](https://discourse.julialang.org/t/remove-zero-from-matrix/102322/6 "2023-07-31T18:34:23Z")

</div>

a little pulled by the hair, but there would also be this

```julia
using SparseArrays
reshape(SparseArrays.nzvalview(sparse($m)),:,3)

```

```julia
reshape(nonzeros(sparse($m)),:,3)

```
