# A good way to filter an array with keeping its dimension

**URL:** <https://discourse.julialang.org/t/a-good-way-to-filter-an-array-with-keeping-its-dimension/42603>\
**Category:** General Usage\
**Tags:** question\
**Created:** [July 6, 2020, 11:44am UTC](https://discourse.julialang.org/t/a-good-way-to-filter-an-array-with-keeping-its-dimension/42603 "2020-07-06T11:44:46Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![SSU](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ssu/32/11306_2.png) [@SSU](https://discourse.julialang.org/u/SSU)\
**Post date:** [July 6, 2020, 11:44am UTC](https://discourse.julialang.org/t/a-good-way-to-filter-an-array-with-keeping-its-dimension/42603/1 "2020-07-06T11:44:46Z")

</div>

I want to filter `nothing` in an array with keeping its dimension.  
Is there any better way to do this other than using `reshape()`?

```julia
x = [
    -0.240487 1.72634
    -0.946312 0.915657
    0.497511 3.77469
    -1.63141 1.31983
   nothing nothing
   nothing nothing
   nothing nothing
   nothing nothing
]
size(filter(!isnothing, x)) # (8,)

```

I want to get the following object as an output.

```julia
# Array{Float64,2}
 [
    -0.240487 1.72634
    -0.946312 0.915657
    0.497511 3.77469
    -1.63141 1.31983
]

```

---

<div class="post-metadata">

**Author:** ![oheil](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oheil/32/220745_2.png) [@oheil](https://discourse.julialang.org/u/oheil)\
**Post date:** [July 6, 2020, 12:21pm UTC](https://discourse.julialang.org/t/a-good-way-to-filter-an-array-with-keeping-its-dimension/42603/2 "2020-07-06T12:21:25Z")

</div>

First you have to define which rows you want to filter, e.g. what about rows 6 and 8 in

```julia
x = [
    -0.240487 1.72634
    -0.946312 0.915657
    0.497511 3.77469
    -1.63141 1.31983
   nothing nothing
   nothing 0.76453
   nothing nothing
   -0.47456 nothing
]

```

---

<div class="post-metadata">

**Author:** ![SSU](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ssu/32/11306_2.png) [@SSU](https://discourse.julialang.org/u/SSU)\
**Post date:** [July 6, 2020, 2:53pm UTC](https://discourse.julialang.org/t/a-good-way-to-filter-an-array-with-keeping-its-dimension/42603/3 "2020-07-06T14:53:22Z")

</div>

True…  
I want to filter all the rows that contains nothing.  
Row 6 and 8 should be filtered in my case.

---

<div class="post-metadata">

**Author:** ![nilshg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nilshg/32/2283_2.png) [@nilshg](https://discourse.julialang.org/u/nilshg)\
**Post date:** [July 6, 2020, 2:59pm UTC](https://discourse.julialang.org/t/a-good-way-to-filter-an-array-with-keeping-its-dimension/42603/4 "2020-07-06T14:59:24Z")

</div>

```julia
julia> x[.!any.(isnothing, eachrow(x)), :]
4×2 Array{Union{Nothing, Float64},2}:
 -0.240487 1.72634
 -0.946312 0.915657
  0.497511 3.77469
 -1.63141 1.31983

```

---

<div class="post-metadata">

**Author:** ![SSU](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ssu/32/11306_2.png) [@SSU](https://discourse.julialang.org/u/SSU)\
**Post date:** [July 7, 2020, 5:58am UTC](https://discourse.julialang.org/t/a-good-way-to-filter-an-array-with-keeping-its-dimension/42603/5 "2020-07-07T05:58:01Z")

</div>

Thank you!  
I’ve never used `eachrow()` before.
