Skip to content

Commit ca4a9cc

Browse files
committed
Added logical indexing to subarrays. Fix JuliaLang#4763
1 parent 86b91f0 commit ca4a9cc

File tree

2 files changed

+14
-0
lines changed

2 files changed

+14
-0
lines changed

base/subarray.jl

+10
Original file line numberDiff line numberDiff line change
@@ -277,6 +277,16 @@ function getindex{T,S<:Integer}(s::SubArray{T,1}, I::AbstractVector{S})
277277
getindex(s.parent, t)
278278
end
279279

280+
function getindex{T}(s::SubArray{T,1}, I::AbstractVector{Bool})
281+
t = Array(T, 0)
282+
for i = 1:length(I)
283+
if I[i]
284+
push!(t, s.parent[s.first_index + (i-1)*s.strides[1]])
285+
end
286+
end
287+
t
288+
end
289+
280290
function translate_indexes(s::SubArray, I::Union(Real,AbstractArray)...)
281291
n = length(I)
282292
newindexes = Any[s.indexes...]

test/arrayops.jl

+4
Original file line numberDiff line numberDiff line change
@@ -305,6 +305,10 @@ p = permutedims(s, [2,1])
305305
@test p[1,1]==a[2,2] && p[1,2]==a[3,2]
306306
@test p[2,1]==a[2,3] && p[2,2]==a[3,3]
307307

308+
a = [1:10]
309+
b = sub(a, 5:8)
310+
@test filter(x -> x < 7, b) == [5, 6]
311+
308312
## ipermutedims ##
309313

310314
tensors = {rand(1,2,3,4),rand(2,2,2,2),rand(5,6,5,6),rand(1,1,1,1)}

0 commit comments

Comments
 (0)