使用布尔数组访问 String31 数组
我尝试使用布尔数组索引 String31 数组:
y = Bool[1 1 0 1 1 1 0 0 0 1 0 1 ...]
size = (1, 1024)
当
labels = String31["ZINC000000000007", "ZINC000000000010", "ZINC000000000011", "ZINC000000000012", "ZINC000000000014", "ZINC000000000015", "ZINC000000000017", "ZINC000000000018", ... ]
size = (1024,)
我尝试使用以下方式访问正数组时:
labels[ y]
我收到此错误:
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
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
正如错误消息告诉您的那样,
y
是matrix
,而不是vector
,但是标签
是向量
。布尔索引仅在索引阵列的等级与索引中的数组相同时才能起作用。因此,要么创建适当的文字
(请注意逗号!),要么使用另一种构造方法为您提供
vector
。As the error message tells you,
y
is aMatrix
, not aVector
, butlabels
is aVector
. Boolean indexing only works when the index array has the same rank as the array indexed into.So either create an appropriate literal,
(note the commas!), or use another construction method giving you a
Vector
.