0
votes

I try to index a String31 array with a boolean array:

y = Bool[1 1 0 1 1 1 0 0 0 1 0 1 ...]
size = (1, 1024)    

and

labels = String31["ZINC000000000007", "ZINC000000000010", "ZINC000000000011", "ZINC000000000012", "ZINC000000000014", "ZINC000000000015", "ZINC000000000017", "ZINC000000000018", ... ]
size = (1024,)    

when I try to access the positive ones with:

labels[ y]

I get this error:

BoundsError: attempt to access 1024-element Vector{String31} at index [1×1024 Matrix{Bool}]

Stacktrace:
 [1] throw_boundserror(A::Vector{String31}, I::Tuple{Base.LogicalIndex{Int64, Matrix{Bool}}})
   @ Base ./abstractarray.jl:691
 [2] checkbounds
   @ ./abstractarray.jl:656 [inlined]
 [3] _getindex
   @ ./multidimensional.jl:838 [inlined]
 [4] getindex(A::Vector{String31}, I::Matrix{Bool})
   @ Base ./abstractarray.jl:1218
 [5] top-level scope
   @ ./In[106]:54
 [6] eval
   @ ./boot.jl:373 [inlined]
 [7] include_string(mapexpr::typeof(REPL.softscope), mod::Module, code::String, filename::String)
   @ Base ./loading.jl:1196
1

1 Answers

1
votes

As the error message tells you, y is a Matrix, not a Vector, but labels is a Vector. Boolean indexing only works when the index array has the same rank as the array indexed into.

So either create an appropriate literal,

y = Bool[1, 1, 0, 1, 0, ...]

(note the commas!), or use another construction method giving you a Vector.