Skip to content

Commit

Permalink
rewrite indexin function according to @timholy and using comprehe…
Browse files Browse the repository at this point in the history
…nsion

rename to indexin

rename to indexin
  • Loading branch information
wlbksy committed Jul 25, 2013
1 parent a631ca0 commit 88b0c57
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 0 deletions.
7 changes: 7 additions & 0 deletions base/array.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1371,6 +1371,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)
Expand Down
1 change: 1 addition & 0 deletions base/exports.jl
Original file line number Diff line number Diff line change
Expand Up @@ -491,6 +491,7 @@ export
hcat,
hvcat,
ind2sub,
indexin,
indmax,
indmin,
invperm,
Expand Down
6 changes: 6 additions & 0 deletions doc/stdlib/base.rst
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,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)

Expand Down

0 comments on commit 88b0c57

Please sign in to comment.