# Index array using boolean result of a comparison

**URL:** <https://discourse.julialang.org/t/index-array-using-boolean-result-of-a-comparison/5712>\
**Category:** General Usage\
**Tags:** first-steps\
**Created:** [September 4, 2017, 1:31pm UTC](https://discourse.julialang.org/t/index-array-using-boolean-result-of-a-comparison/5712 "2017-09-04T13:31:55Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![ericsfraga](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ericsfraga/32/192_2.png) [@ericsfraga](https://discourse.julialang.org/u/ericsfraga)\
**Post date:** [September 4, 2017, 1:31pm UTC](https://discourse.julialang.org/t/index-array-using-boolean-result-of-a-comparison/5712/1 "2017-09-04T13:31:55Z")

</div>

Hello,  
I am trying to reset the values of a vector (1d array) for any values that are outside a bound. In 0.5, I did the following, for instance:

```julia
x[x.<a] = a[x.<a]

```

This works if I type it in on the console but fails when executed as part of a function with an error of this form:

```julia
ERROR: LoadError: BoundsError: attempt to access 2-element Array{Float64,1} at index [Base.LogicalIndex(Bool[true; false])]
Stacktrace:
 [1] throw_boundserror(::Array{Float64,1}, ::Tuple{Base.LogicalIndex{Int64,BitArray{2}}}) at ./abstractarray.jl:433
 [2] checkbounds at ./abstractarray.jl:362 [inlined]
 [3] macro expansion at ./multidimensional.jl:441 [inlined]
 [4] _getindex at ./multidimensional.jl:438 [inlined]
 [5] getindex at ./abstractarray.jl:882 [inlined]

```

x and a are both 1d arrays of Float64.  
Any suggestions appreciated.  
Thank you.

---

<div class="post-metadata">

**Author:** ![mbauman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mbauman/32/31082_2.png) [@mbauman](https://discourse.julialang.org/u/mbauman)\
**Post date:** [September 4, 2017, 1:37pm UTC](https://discourse.julialang.org/t/index-array-using-boolean-result-of-a-comparison/5712/2 "2017-09-04T13:37:59Z")

</div>

> [@ericsfraga](#):
>
> x and a are both 1d arrays

Are you sure about that? It looks like `x` is a 2-dimensional column matrix. We tightened bounds checking for logical arrays in 0.6, but I think we went a little too far. This does seem like it should work.

---

<div class="post-metadata">

**Author:** ![ericsfraga](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ericsfraga/32/192_2.png) [@ericsfraga](https://discourse.julialang.org/u/ericsfraga)\
**Post date:** [September 4, 2017, 1:46pm UTC](https://discourse.julialang.org/t/index-array-using-boolean-result-of-a-comparison/5712/3 "2017-09-04T13:46:24Z")

</div>

Yes, quite sure; outputting summary(x) and summary(a) just before says:

```julia
2-element Array{Float64,1}
2-element Array{Float64,1}

```

Thanks.

---

<div class="post-metadata">

**Author:** ![ericsfraga](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ericsfraga/32/192_2.png) [@ericsfraga](https://discourse.julialang.org/u/ericsfraga)\
**Post date:** [September 4, 2017, 1:49pm UTC](https://discourse.julialang.org/t/index-array-using-boolean-result-of-a-comparison/5712/4 "2017-09-04T13:49:58Z")

</div>

Sorry, I lied!   
The array is actually 2×1 Array{Float64,2} (same size in my mind but obviously different rank).  
I need to figure out why now… this did use to work in 0.5.  
Again, my apologies for the noise.

---

<div class="post-metadata">

**Author:** ![ericsfraga](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ericsfraga/32/192_2.png) [@ericsfraga](https://discourse.julialang.org/u/ericsfraga)\
**Post date:** [September 4, 2017, 1:52pm UTC](https://discourse.julialang.org/t/index-array-using-boolean-result-of-a-comparison/5712/5 "2017-09-04T13:52:28Z")

</div>

Question: did rand change in behaviour since 0.5? I used to do

```julia
rand(n,1)

```

to get vector n long; without the ,1, my code works fine now.

Thanks again,  
eric

---

<div class="post-metadata">

**Author:** ![mbauman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mbauman/32/31082_2.png) [@mbauman](https://discourse.julialang.org/u/mbauman)\
**Post date:** [September 4, 2017, 2:08pm UTC](https://discourse.julialang.org/t/index-array-using-boolean-result-of-a-comparison/5712/6 "2017-09-04T14:08:28Z")

</div>

No; the change was in how we check for consistent shapes of logical indexing. We used to be much more permissive here but now on 0.6 the shapes must match exactly. Just reshape your logical mask to a 1-d vector with `vec` and it should work on both versions.

---

<div class="post-metadata">

**Author:** ![ericsfraga](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ericsfraga/32/192_2.png) [@ericsfraga](https://discourse.julialang.org/u/ericsfraga)\
**Post date:** [September 4, 2017, 2:11pm UTC](https://discourse.julialang.org/t/index-array-using-boolean-result-of-a-comparison/5712/7 "2017-09-04T14:11:25Z")

</div>

Thank you. This makes sense. Code now sorted and everything working fine.
