Skip to content

Commit

Permalink
Added logical indexing to subarrays. Fix JuliaLang#4763
Browse files Browse the repository at this point in the history
  • Loading branch information
ivarne committed Mar 4, 2014
1 parent 86b91f0 commit ca4a9cc
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 0 deletions.
10 changes: 10 additions & 0 deletions base/subarray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,16 @@ function getindex{T,S<:Integer}(s::SubArray{T,1}, I::AbstractVector{S})
getindex(s.parent, t)
end

function getindex{T}(s::SubArray{T,1}, I::AbstractVector{Bool})
t = Array(T, 0)
for i = 1:length(I)
if I[i]
push!(t, s.parent[s.first_index + (i-1)*s.strides[1]])
end
end
t
end

function translate_indexes(s::SubArray, I::Union(Real,AbstractArray)...)
n = length(I)
newindexes = Any[s.indexes...]
Expand Down
4 changes: 4 additions & 0 deletions test/arrayops.jl
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,10 @@ p = permutedims(s, [2,1])
@test p[1,1]==a[2,2] && p[1,2]==a[3,2]
@test p[2,1]==a[2,3] && p[2,2]==a[3,3]

a = [1:10]
b = sub(a, 5:8)
@test filter(x -> x < 7, b) == [5, 6]

## ipermutedims ##

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

0 comments on commit ca4a9cc

Please sign in to comment.