diff --git a/base/array.jl b/base/array.jl index c8e60b3aad180..3664104406245 100644 --- a/base/array.jl +++ b/base/array.jl @@ -1385,6 +1385,13 @@ end indmax(a) = findmax(a)[2] indmin(a) = findmin(a)[2] +# similar to Matlab's ismember +# returns a vector containing the highest index in b for each value in a that is a member of b +function indexin{T}(a::AbstractArray{T}, b::AbstractArray{T}) + bdict = Dict(b, 1:length(b)) + [get(bdict, i, 0) for i in a] +end + # findin (the index of intersection) function findin(a, b::Range1) ind = Array(Int, 0) diff --git a/base/exports.jl b/base/exports.jl index df404408e7b2c..6fb610f79c8fe 100644 --- a/base/exports.jl +++ b/base/exports.jl @@ -505,6 +505,7 @@ export hcat, hvcat, ind2sub, + indexin, indmax, indmin, infs, diff --git a/doc/stdlib/base.rst b/doc/stdlib/base.rst index 225c629b8c33c..843d62375af29 100644 --- a/doc/stdlib/base.rst +++ b/doc/stdlib/base.rst @@ -358,6 +358,12 @@ Iterable Collections .. function:: contains(itr, x) -> Bool Determine whether a collection contains the given value, ``x``. + +.. function:: 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``. .. function:: findin(a, b)