Skip to content
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

Make indexin first argument accept any iterable #23845

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions base/array.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2210,9 +2210,9 @@ indmin(a) = findmin(a)[2]
"""
indexin(a, b)

Returns a vector containing the highest index in `b` for
each value in `a` that is a member of `b` . The output
vector contains 0 wherever `a` is not a member of `b`.
Returns an array containing the highest index in `b` for
each value in `a` that is a member of `b`. The output
array contains 0 wherever `a` is not a member of `b`.

# Examples
```jldoctest
Expand All @@ -2236,9 +2236,9 @@ julia> indexin(b,a)
3
```
"""
function indexin(a::AbstractArray, b::AbstractArray)
function indexin(a, b::AbstractArray)
bdict = Dict(zip(b, 1:length(b)))
[get(bdict, i, 0) for i in a]
map(i -> get(bdict, i, 0), a)
end

function _findin(a, b)
Expand Down
15 changes: 8 additions & 7 deletions test/arrayops.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1370,13 +1370,14 @@ function i7197()
end
@test i7197() == (2,2)

# PR #8622 and general indexin test
function pr8622()
x=[1,3,5,7]
y=[5,4,3]
return indexin(x,y)
end
@test pr8622() == [0,3,1,0]
# PR #8622 and general indexin tests
@test indexin([1,3,5,7], [5,4,3]) == [0,3,1,0]
@test indexin([1 3; 5 7], [5 4; 3 2]) == [0 2; 1 0]
@test indexin((2 * x + 1 for x in 0:3), [5,4,3,5,6]) == [0,3,4,0]
@test indexin(6, [1,3,6,6,2]) == 4
@test indexin([6], [1,3,6,6,2]) == [4]
@test indexin(3, 2:5) == 2
@test indexin(3.0, 2:5) == 2

#6828 - size of specific dimensions
let a = Array{Float64}(10)
Expand Down