# How to count the number of occurances in an array for each column

**URL:** <https://discourse.julialang.org/t/how-to-count-the-number-of-occurances-in-an-array-for-each-column/22763>\
**Category:** New to Julia\
**Created:** [April 4, 2019, 4:38pm UTC](https://discourse.julialang.org/t/how-to-count-the-number-of-occurances-in-an-array-for-each-column/22763 "2019-04-04T16:38:36Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![HelgavonLichtenstein](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/helgavonlichtenstein/32/5005_2.png) [@HelgavonLichtenstein](https://discourse.julialang.org/u/HelgavonLichtenstein)\
**Post date:** [April 4, 2019, 4:38pm UTC](https://discourse.julialang.org/t/how-to-count-the-number-of-occurances-in-an-array-for-each-column/22763/1 "2019-04-04T16:38:36Z")

</div>

Hello,

Suppose I have a 3x3 array of values that include 0. I would like to know the number of occurrences/times per column that contains a value greater than 0 (in other words count the number of times a column does not include a zero).

What I think I need to do is:

```julia
a = [1 2 0; 2 0 0;3 1 1]
count(i->(i>0), a[,:])

```

But this gives me `ERROR: syntax: unexpected ","`

It is important for me to know the number of occurrences per column as this number is then used in a formula further on.

Any advice is appreciated.

---

<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 4, 2019, 4:48pm UTC](https://discourse.julialang.org/t/how-to-count-the-number-of-occurances-in-an-array-for-each-column/22763/2 "2019-04-04T16:48:12Z")

</div>

Do you want

1. The number of columns with no zeros
2. The number of non-zeros per column
3. The number of values greater than zero per column  
?

You seem to ask for all three at various points in your post.

Edit: sorry, you only ask for 1 and 3.

---

<div class="post-metadata">

**Author:** ![aharoun](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/aharoun/32/6887_2.png) [@aharoun](https://discourse.julialang.org/u/aharoun)\
**Post date:** [April 4, 2019, 4:59pm UTC](https://discourse.julialang.org/t/how-to-count-the-number-of-occurances-in-an-array-for-each-column/22763/3 "2019-04-04T16:59:29Z")

</div>

I don’t think `a[,:]` is a valid syntax. May be something like this?

```julia
julia> a = [1 2 0; 2 0 0;3 1 1];

julia> sum(a.>zero(eltype(a)),dims=1)
1×3 Array{Int64,2}:
 3 2 1

```

---

<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 4, 2019, 5:04pm UTC](https://discourse.julialang.org/t/how-to-count-the-number-of-occurances-in-an-array-for-each-column/22763/4 "2019-04-04T17:04:12Z")

</div>

```julia
sum(x->x>0, a, dims=1) 

```

Btw, in comparisons you don’t need all the zero/eltype stuff. Just use `>0`

---

<div class="post-metadata">

**Author:** ![HelgavonLichtenstein](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/helgavonlichtenstein/32/5005_2.png) [@HelgavonLichtenstein](https://discourse.julialang.org/u/HelgavonLichtenstein)\
**Post date:** [April 4, 2019, 5:13pm UTC](https://discourse.julialang.org/t/how-to-count-the-number-of-occurances-in-an-array-for-each-column/22763/5 "2019-04-04T17:13:04Z")

</div>

Sorry, I mean 3, the number of values greater than zero per column.

---

<div class="post-metadata">

**Author:** ![HelgavonLichtenstein](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/helgavonlichtenstein/32/5005_2.png) [@HelgavonLichtenstein](https://discourse.julialang.org/u/HelgavonLichtenstein)\
**Post date:** [April 4, 2019, 5:15pm UTC](https://discourse.julialang.org/t/how-to-count-the-number-of-occurances-in-an-array-for-each-column/22763/6 "2019-04-04T17:15:12Z")

</div>

Thank you. I was trying to tell Julia I was interested only in each column in a.

---

<div class="post-metadata">

**Author:** ![HelgavonLichtenstein](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/helgavonlichtenstein/32/5005_2.png) [@HelgavonLichtenstein](https://discourse.julialang.org/u/HelgavonLichtenstein)\
**Post date:** [April 4, 2019, 5:23pm UTC](https://discourse.julialang.org/t/how-to-count-the-number-of-occurances-in-an-array-for-each-column/22763/7 "2019-04-04T17:23:48Z")

</div>

To build further, I would like to be able to place the number of values greater than zero per column into the following formula:

`b .= 1/(sum(x->x>0, a, dims=1))`

But this generates  
` **syntax: incomplete: premature end of input** `

---

<div class="post-metadata">

**Author:** ![aharoun](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/aharoun/32/6887_2.png) [@aharoun](https://discourse.julialang.org/u/aharoun)\
**Post date:** [April 4, 2019, 5:39pm UTC](https://discourse.julialang.org/t/how-to-count-the-number-of-occurances-in-an-array-for-each-column/22763/8 "2019-04-04T17:39:09Z")

</div>

Your error message seems to be related to something else, maybe due to an incomplete Julia expression.

However your code would still give an error because the division would not work.

```julia
julia> 1/(sum(x->x>0, a, dims=1))
ERROR: MethodError: no method matching /(::Int64, ::Array{Int64,2})

```

I guess you want to do the division element by element:

```julia
b = 1 ./(sum(x->x>0, a, dims=1))

```

---

<div class="post-metadata">

**Author:** ![HelgavonLichtenstein](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/helgavonlichtenstein/32/5005_2.png) [@HelgavonLichtenstein](https://discourse.julialang.org/u/HelgavonLichtenstein)\
**Post date:** [April 4, 2019, 5:42pm UTC](https://discourse.julialang.org/t/how-to-count-the-number-of-occurances-in-an-array-for-each-column/22763/9 "2019-04-04T17:42:53Z")

</div>

Thank you! I was missing a bracket. I am new to Julia (and programing) and get confused about where to place `.` to broadcast. I’ve read the documentation but its just something I will have to practice.
