# How to view which of your binary values are 1

**URL:** <https://discourse.julialang.org/t/how-to-view-which-of-your-binary-values-are-1/28446>\
**Category:** Optimization (Mathematical)\
**Created:** [September 5, 2019, 9:30pm UTC](https://discourse.julialang.org/t/how-to-view-which-of-your-binary-values-are-1/28446 "2019-09-05T21:30:48Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![marinarrd](https://avatars.discourse-cdn.com/v4/letter/m/f9ae1b/32.png) [@marinarrd](https://discourse.julialang.org/u/marinarrd)\
**Post date:** [September 5, 2019, 9:30pm UTC](https://discourse.julialang.org/t/how-to-view-which-of-your-binary-values-are-1/28446/1 "2019-09-05T21:30:48Z")

</div>

I am very new to Julia. I wish to obtain the indices of my output that are non-zero

---

<div class="post-metadata">

**Author:** ![longemen3000](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/longemen3000/32/7298_2.png) [@longemen3000](https://discourse.julialang.org/u/longemen3000)\
**Post date:** [September 5, 2019, 10:00pm UTC](https://discourse.julialang.org/t/how-to-view-which-of-your-binary-values-are-1/28446/2 "2019-09-05T22:00:34Z")

</div>

Welcome to julia!, if you need only the indices, the following function can help you.

```julia
indices = findall(!iszero,-2:2) # [-2,-1,1,2]

```

there are some things going on with this

1. `findall` acepts a function and a iterable container
2. `iszero(x)` is a function that gives `true` if x is zero
3. `!` is a negation operator, and it works on functions too, when those functions give a boolean as output
4. `!iszero(x)` then is a function that gives `true` if x is not zero, its not necessary to define a auxiliary function, is enough to write `!iszero`

!

---

<div class="post-metadata">

**Author:** ![marinarrd](https://avatars.discourse-cdn.com/v4/letter/m/f9ae1b/32.png) [@marinarrd](https://discourse.julialang.org/u/marinarrd)\
**Post date:** [September 6, 2019, 1:20am UTC](https://discourse.julialang.org/t/how-to-view-which-of-your-binary-values-are-1/28446/3 "2019-09-06T01:20:28Z")

</div>

Thanks for the response. Sorry I think I meant the key. In this image I have an example. If i input var1 I would like to retain [2.0,5.0,2.0,5.0] but not the others…  
 ![48%20PM%20(2)](https://global.discourse-cdn.com/julialang/original/3X/a/e/aeeef74b8d15ee62fd9055417f7b6ab32e5dfbe6.png)

---

<div class="post-metadata">

**Author:** ![marinarrd](https://avatars.discourse-cdn.com/v4/letter/m/f9ae1b/32.png) [@marinarrd](https://discourse.julialang.org/u/marinarrd)\
**Post date:** [September 6, 2019, 1:21am UTC](https://discourse.julialang.org/t/how-to-view-which-of-your-binary-values-are-1/28446/4 "2019-09-06T01:21:17Z")

</div>

var1 is a DenseAxisArray

---

<div class="post-metadata">

**Author:** ![longemen3000](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/longemen3000/32/7298_2.png) [@longemen3000](https://discourse.julialang.org/u/longemen3000)\
**Post date:** [September 6, 2019, 5:41am UTC](https://discourse.julialang.org/t/how-to-view-which-of-your-binary-values-are-1/28446/5 "2019-09-06T05:41:37Z")

</div>

mmm, i had never worked with DenseAxisArray, can you post the code necessary to generate that array?

> [@Please read: make it easier to help you](https://discourse.julialang.org/t/psa-make-it-easier-to-help-you/14757):
>
> Welcome to the Julia Discourse! We are enthusiastic about helping Julia programmers, both beginner and experienced. This public service announcement (PSA) outlines best practices when asking for help. Following these points makes it easier for us to help you and more likely you’ll get a prompt, useful answer. Keywords are highlighted to make it easier to refer to specific points. Choose a descriptive title that captures the key part of your question, eg “plots with multiple axes” instead of …

---

<div class="post-metadata">

**Author:** ![odow](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/odow/32/28685_2.png) [@odow](https://discourse.julialang.org/u/odow)\
**Post date:** [September 6, 2019, 1:27pm UTC](https://discourse.julialang.org/t/how-to-view-which-of-your-binary-values-are-1/28446/6 "2019-09-06T13:27:42Z")

</div>

You’re probably looking for something like:

```nohighlight
julia> using JuMP

julia> using Gurobi

julia> model = Model(with_optimizer(Gurobi.Optimizer, OutputFlag=0));
Academic license - for non-commercial use only

julia> @variable(model, x[1:2, 1:3], Bin)
2×3 Array{VariableRef,2}:
 x[1,1] x[1,2] x[1,3]
 x[2,1] x[2,2] x[2,3]

julia> @objective(model, Min, sum((-1)^(i + j) * x[i, j] for i in 1:2, j in 1:3))
x[1,1] - x[1,2] + x[1,3] - x[2,1] + x[2,2] - x[2,3]

julia> optimize!(model)
Academic license - for non-commercial use only

julia> x_val = value.(x)
2×3 Array{Float64,2}:
 0.0 1.0 0.0
 1.0 0.0 1.0

julia> x[x_val .> 0.5]
3-element Array{VariableRef,1}:
 x[2,1]
 x[1,2]
 x[2,3]

```

---

<div class="post-metadata">

**Author:** ![marinarrd](https://avatars.discourse-cdn.com/v4/letter/m/f9ae1b/32.png) [@marinarrd](https://discourse.julialang.org/u/marinarrd)\
**Post date:** [September 6, 2019, 5:08pm UTC](https://discourse.julialang.org/t/how-to-view-which-of-your-binary-values-are-1/28446/7 "2019-09-06T17:08:32Z")

</div>

```julia
using JuMP
using Gurobi
m=Model(with_optimizer(Gurobi.Optimizer,OutputFlag=0))
@variable(m,z[i_1=2:m1,i_2=i_1:m1,j_1=2:m2,j_2=j_1:m2],Bin)

```

Thank you! I tried this but I get an error because z is a DenseAxisArray and this is not supported.

---

<div class="post-metadata">

**Author:** ![odow](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/odow/32/28685_2.png) [@odow](https://discourse.julialang.org/u/odow)\
**Post date:** [September 6, 2019, 9:14pm UTC](https://discourse.julialang.org/t/how-to-view-which-of-your-binary-values-are-1/28446/8 "2019-09-06T21:14:07Z")

</div>

So what about something like

```nohighlight
using JuMP, Gurobi
m1 = 4
m2 = 4
m=Model(with_optimizer(Gurobi.Optimizer,OutputFlag=0))
@variable(m,z[i_1=2:m1,i_2=i_1:m1,j_1=2:m2,j_2=j_1:m2],Bin)
optimize!(model)
z_val = value.(z)

on_variables = [key for key in eachindex(z) if z_val[key] > 0.5]

```
