Skip to content

Commit 9a239ec

Browse files
authored
Change indexin() to return first rather than last matching index (#25998)
That's more natural, and consistent with MATLAB's ismember() and R's match().
1 parent 4c4aaaf commit 9a239ec

File tree

3 files changed

+15
-9
lines changed

3 files changed

+15
-9
lines changed

NEWS.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -426,6 +426,8 @@ This section lists changes that do not have deprecation warnings.
426426
* The `fieldnames` and `propertynames` functions now return a tuple rather than
427427
an array ([#25725]).
428428

429+
* `indexin` now returns the first rather than the last matching index ([#25998]).
430+
429431
Library improvements
430432
--------------------
431433

@@ -1300,3 +1302,4 @@ Command-line option changes
13001302
[#25655]: https://github.com/JuliaLang/julia/issues/25655
13011303
[#25725]: https://github.com/JuliaLang/julia/issues/25725
13021304
[#25745]: https://github.com/JuliaLang/julia/issues/25745
1305+
[#25998]: https://github.com/JuliaLang/julia/issues/25998

base/array.jl

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2139,7 +2139,7 @@ argmin(a) = findmin(a)[2]
21392139
"""
21402140
indexin(a, b)
21412141
2142-
Return an array containing the highest index in `b` for
2142+
Return an array containing the first index in `b` for
21432143
each value in `a` that is a member of `b`. The output
21442144
array contains `nothing` wherever `a` is not a member of `b`.
21452145
@@ -2160,15 +2160,18 @@ julia> indexin(a, b)
21602160
21612161
julia> indexin(b, a)
21622162
3-element Array{Union{Nothing, Int64},1}:
2163-
6
2164-
4
2163+
1
2164+
2
21652165
3
21662166
```
21672167
"""
21682168
function indexin(a, b::AbstractArray)
2169-
indexes = keys(b)
2170-
bdict = Dict(zip(b, indexes))
2171-
return Union{eltype(indexes), Nothing}[
2169+
inds = keys(b)
2170+
bdict = Dict{eltype(b),eltype(inds)}()
2171+
for (val, ind) in zip(b, inds)
2172+
get!(bdict, val, ind)
2173+
end
2174+
return Union{eltype(inds), Nothing}[
21722175
get(bdict, i, nothing) for i in a
21732176
]
21742177
end

test/arrayops.jl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1415,9 +1415,9 @@ end
14151415
# PR #8622 and general indexin tests
14161416
@test indexin([1,3,5,7], [5,4,3]) == [nothing,3,1,nothing]
14171417
@test indexin([1 3; 5 7], [5 4; 3 2]) == [nothing CartesianIndex(2, 1); CartesianIndex(1, 1) nothing]
1418-
@test indexin((2 * x + 1 for x in 0:3), [5,4,3,5,6]) == [nothing,3,4,nothing]
1419-
@test indexin(6, [1,3,6,6,2]) == fill(4, ())
1420-
@test indexin([6], [1,3,6,6,2]) == [4]
1418+
@test indexin((2 * x + 1 for x in 0:3), [5,4,3,5,6]) == [nothing,3,1,nothing]
1419+
@test indexin(6, [1,3,6,6,2]) == fill(3, ())
1420+
@test indexin([6], [1,3,6,6,2]) == [3]
14211421
@test indexin([3], 2:5) == [2]
14221422
@test indexin([3.0], 2:5) == [2]
14231423

0 commit comments

Comments
 (0)