# How to remove irrelevant array entries?

**URL:** <https://discourse.julialang.org/t/how-to-remove-irrelevant-array-entries/38245>\
**Category:** New to Julia\
**Created:** [April 26, 2020, 4:00pm UTC](https://discourse.julialang.org/t/how-to-remove-irrelevant-array-entries/38245 "2020-04-26T16:00:26Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ahmed\_Salih](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ahmed_salih/32/206579_2.png) [@Ahmed\_Salih](https://discourse.julialang.org/u/Ahmed_Salih)\
**Post date:** [April 26, 2020, 4:00pm UTC](https://discourse.julialang.org/t/how-to-remove-irrelevant-array-entries/38245/1 "2020-04-26T16:00:26Z")

</div>

Hello!

In this case I have:

```julia
idc
2×9 Array{Int64,2}:
 1 2 0 1 1 2 0 0 2
 1 1 1 2 0 2 2 0 0

```

I only care about columns, in which 0 does not appear. Therefore, coming from Matlab, I would like to do something which gives me:

```julia
idc
2×4 Array{Int64,2}:
 1 2 1 2
 1 1 2 2 

```

But I cannot figure out a simple way to remove the irrelevant columns. Maybe I shouldn’t alter the Array, but do something else?

Kind regards

---

<div class="post-metadata">

**Author:** ![dmolina](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dmolina/32/5246_2.png) [@dmolina](https://discourse.julialang.org/u/dmolina)\
**Post date:** [April 26, 2020, 5:05pm UTC](https://discourse.julialang.org/t/how-to-remove-irrelevant-array-entries/38245/2 "2020-04-26T17:05:19Z")

</div>

I am sure that there should be a better way, but you can do:

```julia
# Get the column indexes for which all values are not 0
ids=[all(!=(0), r) for r in eachcol(idc)]
# Filter only these columns
idc = idc[:,ids]

```

---

<div class="post-metadata">

**Author:** ![yha](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yha/32/3502_2.png) [@yha](https://discourse.julialang.org/u/yha)\
**Post date:** [April 26, 2020, 5:28pm UTC](https://discourse.julialang.org/t/how-to-remove-irrelevant-array-entries/38245/3 "2020-04-26T17:28:31Z")

</div>

A more compact, but probably much less efficient, solution:

```julia
reduce(hcat, c for c in eachcol(idc) if all(!=(0),c))

```

---

<div class="post-metadata">

**Author:** ![Ahmed\_Salih](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ahmed_salih/32/206579_2.png) [@Ahmed\_Salih](https://discourse.julialang.org/u/Ahmed_Salih)\
**Post date:** [April 27, 2020, 8:43am UTC](https://discourse.julialang.org/t/how-to-remove-irrelevant-array-entries/38245/4 "2020-04-27T08:43:25Z")

</div>

Thanks to both of you. Was not aware of “eachcol” functionality.

Kind regards

---

<div class="post-metadata">

**Author:** ![Ahmed\_Salih](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ahmed_salih/32/206579_2.png) [@Ahmed\_Salih](https://discourse.julialang.org/u/Ahmed_Salih)\
**Post date:** [April 27, 2020, 11:07am UTC](https://discourse.julialang.org/t/how-to-remove-irrelevant-array-entries/38245/5 "2020-04-27T11:07:31Z")

</div>

For anyone interested in doing this kind of thing, I would advise trying to circumvent it - both options dmolina and yha show do the job, but they use a very large amount of allocations around 250k to 500k respectively to achieve it. I instead wrote out a bunch of if statements and got a lot lower allocation count.

Kind regards

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [April 27, 2020, 12:05pm UTC](https://discourse.julialang.org/t/how-to-remove-irrelevant-array-entries/38245/6 "2020-04-27T12:05:43Z")

</div>

> [@Ahmed\_Salih](#):
>
> I instead wrote out a bunch of if statements and got a lot lower allocation count.

Just treating your data as a `Vector` of, say, `SVector{2}`s or tuples may be very efficient and idiomatic.

---

<div class="post-metadata">

**Author:** ![Ahmed\_Salih](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ahmed_salih/32/206579_2.png) [@Ahmed\_Salih](https://discourse.julialang.org/u/Ahmed_Salih)\
**Post date:** [April 27, 2020, 1:53pm UTC](https://discourse.julialang.org/t/how-to-remove-irrelevant-array-entries/38245/7 "2020-04-27T13:53:44Z")

</div>

Could you explain a bit more, why this would be of benefit?

You are right in making it tuple’s would make it more efficient, since the sizes are fixed then

Kind regards

---

<div class="post-metadata">

**Author:** ![Skoffer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/skoffer/32/378_2.png) [@Skoffer](https://discourse.julialang.org/u/Skoffer)\
**Post date:** [April 27, 2020, 8:29pm UTC](https://discourse.julialang.org/t/how-to-remove-irrelevant-array-entries/38245/8 "2020-04-27T20:29:03Z")

</div>

Well, we can try to allocate less.

```julia
function filter_out_all_zero_columns!(X)
    cnt = 1
    for i in axes(X, 2)
        zc = false
        for j in axes(X, 1)
            if X[j, i] == 0
                zc = true
                break
            end
        end
        if !zc & (cnt != i)
            for j in axes(X, 1)
                X[j, cnt] = X[j, i]
            end
            cnt += 1
        end
    end
    X[:, 1:cnt - 1]
end

```

And benchmarks

```julia
using Random
Random.seed!(2020)
idc = rand(0:2, 2, 9)

@btime filter_out_all_zero_columns!(Y) setup = (Y = copy($idc))
# 58.250 ns (1 allocation: 128 bytes)

```

It can be made faster if change `X[:, 1:cnt-1]` to `@view X[:, 1:cnt-1]`, but it depends on a problem.

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [April 28, 2020, 5:49am UTC](https://discourse.julialang.org/t/how-to-remove-irrelevant-array-entries/38245/9 "2020-04-28T05:49:21Z")

</div>

> [@Ahmed\_Salih](#):
>
> Could you explain a bit more, why this would be of benefit?

Because what you are doing is manipulating columns as units of data, so it is better to store them that way. Using `SVector`s or tuples, the compiler will know the number of elements in each, and generate very efficient code.

As a bonus, you can `filter` it like any other vector.

---

<div class="post-metadata">

**Author:** ![Ahmed\_Salih](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ahmed_salih/32/206579_2.png) [@Ahmed\_Salih](https://discourse.julialang.org/u/Ahmed_Salih)\
**Post date:** [April 28, 2020, 9:13pm UTC](https://discourse.julialang.org/t/how-to-remove-irrelevant-array-entries/38245/10 "2020-04-28T21:13:23Z")

</div>

Thanks for the explanation.

My experience has always been that using tuple’s is slightly more efficient than SVectors, so will try to stick wih them in the future, whenever I can.

Sometimes I just forget to think about how I am storing data etc.

Kind regards

---

<div class="post-metadata">

**Author:** ![Ahmed\_Salih](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ahmed_salih/32/206579_2.png) [@Ahmed\_Salih](https://discourse.julialang.org/u/Ahmed_Salih)\
**Post date:** [April 28, 2020, 9:14pm UTC](https://discourse.julialang.org/t/how-to-remove-irrelevant-array-entries/38245/11 "2020-04-28T21:14:48Z")

</div>

Thank you for showcasing that!

This is one of the things I like about Julia is that one can write code at very many different levels of complexity - I had never seen the `axes` function before now.

Kind regards

---

<div class="post-metadata">

**Author:** ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)\
**Post date:** [April 28, 2020, 9:42pm UTC](https://discourse.julialang.org/t/how-to-remove-irrelevant-array-entries/38245/12 "2020-04-28T21:42:39Z")

</div>

> [@Ahmed\_Salih](#):
>
> My experience has always been that using tuple’s is slightly more efficient than SVectors, so will try to stick wih them in the future, whenever I can.

That’s not my experience. Are you sure it’s not a benchmarking issue?

If SVectors offer more convenience, I would use them. In many cases you just need a tuple, of course.

---

<div class="post-metadata">

**Author:** ![dpsanders](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dpsanders/32/3573_2.png) [@dpsanders](https://discourse.julialang.org/u/dpsanders)\
**Post date:** [April 29, 2020, 3:17am UTC](https://discourse.julialang.org/t/how-to-remove-irrelevant-array-entries/38245/13 "2020-04-29T03:17:15Z")

</div>

An `SVector` is just a wrapper around a tuple so they should be exactly equivalent in most circumstances (I believe).
