-
-
Notifications
You must be signed in to change notification settings - Fork 5.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Implement logical indexing for SubArray. Fixes #4763 #4764
Conversation
This does only work for 1d subarrays. I don't know how to generalize it to higher dimensions (yet). |
Generalizing to higher dimensions could be done using the same trick as in
One could (and probably should) hand-write versions for low-dimensional cases, but a general solution would involve Given the impending release I will allow others to decide whether to merge this. If it gets merged, another issue needs to be filed to handle |
I think we should at least merge a |
@timholy yes, I used the |
There is no need to write out cases for dimensions 1-5; a static parameter could be used:
|
@JeffBezanson I thought about this definition at first too, but it does not work on 1D and 2D tests: import Base.getindex
import Base.Test.@test
function getindex_bool_1d(S::SubArray, I::AbstractArray{Bool})
n = sum(I)
out = similar(S, n)
c = 1
for i = 1:length(I)
if I[i]
out[c] = S[i]
c += 1
end
end
out
end
#getindex{T}(S::SubArray{T,1}, I::AbstractArray{Bool,1}) = getindex_bool_1d(S, I)
#getindex{T}(S::SubArray{T,2}, I::AbstractArray{Bool,2}) = getindex_bool_1d(S, I)
getindex{T,N}(S::SubArray{T,N}, I::AbstractArray{Bool,N}) = getindex_bool_1d(S, I)
# 1D case
A = sub([1:10], 5:8)
@test A[A.<7] == [5, 6]
# 2D case
B = reshape(1:16, 4, 4)
Bs = sub(B, 2:3, 2:3)
@test Bs[Bs.>8] == [10, 11]
Output:
|
@JeffBezanson, if I understand your suggestion correctly (and I may not be), the main problem is that it will stink from a performance perspective. Currently it's pretty important to use cartesian indexing rather than linear indexing when dealing with multidimensional SubArrays. |
Implement logical indexing for SubArray. Fixes #4763
No description provided.