Index array using boolean result of a comparison

first-steps

#1

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:

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:

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.


#2

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.


#3

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

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

Thanks.


#4

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.


#5

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

rand(n,1)

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

Thanks again,
eric


#6

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.


#7

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