# Julian way of MATLAB ismember function?

**URL:** <https://discourse.julialang.org/t/julian-way-of-matlab-ismember-function/3740>\
**Category:** General Usage\
**Created:** [May 16, 2017, 8:44am UTC](https://discourse.julialang.org/t/julian-way-of-matlab-ismember-function/3740 "2017-05-16T08:44:07Z")\
**Posts on this page:** 17\
**Page:** 1

<div class="post-metadata">

**Author:** ![leejm516](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/leejm516/32/8219_2.png) [@leejm516](https://discourse.julialang.org/u/leejm516)\
**Post date:** [May 16, 2017, 8:44am UTC](https://discourse.julialang.org/t/julian-way-of-matlab-ismember-function/3740/1 "2017-05-16T08:44:07Z")

</div>

> **[Array elements that are members of set array - MATLAB ismember](https://www.mathworks.com/help/matlab/ref/double.ismember.html;jsessionid=65da250df333fe56e8de512a048c)**
>
> This MATLAB function returns an array containing logical 1 (true) where the data in A is found in B.

Let two arrays ` a = ["1", "4", "7"]` and `b = ["1","2","3,"4","5"]`.

In MATLAB, `[Lia,Locb] = ismember(a, b)` will lead to

```julia
Lia = [1, 1, 0]
Locb = [1, 4, 0]

```

Quickly looking over docs, I coudn’t find a Julian equivalent of `ismember`.  
Instead I can think two alternative ways.

```julia
# Way 1: Using comprehending lists
Lia = [each_item in b for each_item in a]
Locb = [findfirst(b, each_item) for each_item in a]

# Way 2: Vectorization (edited)
Lia = broadcast(x -> in(x, b), a)
Locb = broadcast(x -> findfirst(b, x), a)

```

Any better ways?

---

<div class="post-metadata">

**Author:** ![GunnarFarneback](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gunnarfarneback/32/1827_2.png) [@GunnarFarneback](https://discourse.julialang.org/u/GunnarFarneback)\
**Post date:** [May 16, 2017, 10:15am UTC](https://discourse.julialang.org/t/julian-way-of-matlab-ismember-function/3740/3 "2017-05-16T10:15:00Z")

</div>

If you want it to be short you can do

```julia
Lia = in.(a, [b])
LocB = findfirst.([b], a)

```

In practice I’d look at what the results of `ismember` are being used for. It’s quite possible that in Julia you wouldn’t need to form those vectors explicitly.

---

<div class="post-metadata">

**Author:** ![wonjinChoi](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/wonjinchoi/32/11602_2.png) [@wonjinChoi](https://discourse.julialang.org/u/wonjinChoi)\
**Post date:** [May 16, 2017, 10:18am UTC](https://discourse.julialang.org/t/julian-way-of-matlab-ismember-function/3740/4 "2017-05-16T10:18:49Z")

</div>

I found that there is an `indexin` function. It’s not exactly same as matlab, but…

```julia
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.

  julia> a = ['a', 'b', 'c', 'b', 'd', 'a'];

  julia> b = ['a','b','c'];

  julia> indexin(a,b)
  6-element Array{Int64,1}:
   1
   2
   3
   2
   0
   1

  julia> indexin(b,a)
  3-element Array{Int64,1}:
   6
   4
   3

```

---

<div class="post-metadata">

**Author:** ![Dan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dan/32/42581_2.png) [@Dan](https://discourse.julialang.org/u/Dan)\
**Post date:** [May 16, 2017, 10:26am UTC](https://discourse.julialang.org/t/julian-way-of-matlab-ismember-function/3740/5 "2017-05-16T10:26:35Z")

</div>

As wonjinChoi said, `indexin` should do the heavy lifting. And `Lia` can be calculated from `LocB` to save time. In code:  
`LocB = indexin(a,b) Lia = 1.*(LocB .!= 0)`

this gives the following:

```julia
julia> a = ['1', '4', '7']; b = ['1','2','3','4','5'];

julia> LocB = indexin(a,b)
3-element Array{Int64,1}:
 1
 4
 0

julia> Lia = 1.*(LocB .!= 0)
3-element Array{Int64,1}:
 1
 1
 0

```

---

<div class="post-metadata">

**Author:** ![leejm516](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/leejm516/32/8219_2.png) [@leejm516](https://discourse.julialang.org/u/leejm516)\
**Post date:** [May 16, 2017, 11:38pm UTC](https://discourse.julialang.org/t/julian-way-of-matlab-ismember-function/3740/6 "2017-05-16T23:38:38Z")

</div>

Thank you for all suggestions.

@GunnarFarneback I’m porting a MATLAB toolbox (COBRA Toolbox; [https://github.com/opencobra/cobratoolbox/](https://github.com/opencobra/cobratoolbox/)) into Julia as a practice. The whole story is too long… In this toolbox, this function is employed to check whether a newly added row(s) (metabolites) already exists in the sparse matrix.

---

<div class="post-metadata">

**Author:** ![colintbowers](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/colintbowers/32/8033_2.png) [@colintbowers](https://discourse.julialang.org/u/colintbowers)\
**Post date:** [May 17, 2017, 11:09am UTC](https://discourse.julialang.org/t/julian-way-of-matlab-ismember-function/3740/7 "2017-05-17T11:09:23Z")

</div>

Is it just by accident that your two input arrays in your example are sorted? If you can guarantee sort order of your inputs then typically you can solve this problem much faster that using `in`, `indexin`, or `findfirst` (or `ismember` in Matlab). For the case where one of your input arrays is very small relative to the other you can use `searchsorted` on each element of the small array, so you get `k` calls to an O(ln(n)) algorithm. For the case where both input arrays are of similar size you can use a simultaneous single loop over both arrays, hence the algorithm is O(n). There are probably several examples of this algorithm online but if you want one in Julia I have it so let me know and I’ll post it here.

---

<div class="post-metadata">

**Author:** ![leejm516](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/leejm516/32/8219_2.png) [@leejm516](https://discourse.julialang.org/u/leejm516)\
**Post date:** [May 18, 2017, 2:48am UTC](https://discourse.julialang.org/t/julian-way-of-matlab-ismember-function/3740/8 "2017-05-18T02:48:50Z")

</div>

Ahh, that was just by accident. Elements of the array do not need to be sorted.  
I learned some basics of algorithms and Big-Oh notations but have almost forgotten 🙂  
If you would show me some examples, I’d really appreciate it.

---

<div class="post-metadata">

**Author:** ![colintbowers](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/colintbowers/32/8033_2.png) [@colintbowers](https://discourse.julialang.org/u/colintbowers)\
**Post date:** [May 18, 2017, 3:48am UTC](https://discourse.julialang.org/t/julian-way-of-matlab-ismember-function/3740/9 "2017-05-18T03:48:00Z")

</div>

There’s no way I’ll ever manage to give a better explanation of Big-Oh notation than you’ll find here:

> <https://stackoverflow.com/questions/487258/what-is-a-plain-english-explanation-of-big-o-notation>

FYI, given that you can’t guarantee sort order of inputs, the answers provided by others here are as good as you can get (they’ll run in O(n\*ln(n)))

Cheers,

Colin

---

<div class="post-metadata">

**Author:** ![Caesar\_KG](https://avatars.discourse-cdn.com/v4/letter/c/7cd45c/32.png) [@Caesar\_KG](https://discourse.julialang.org/u/Caesar_KG)\
**Post date:** [September 7, 2018, 3:56am UTC](https://discourse.julialang.org/t/julian-way-of-matlab-ismember-function/3740/10 "2018-09-07T03:56:02Z")

</div>

Actually, indexin(a, b) return the highest index in b for each value in a that is a member of b. while ismember in Matlab return the lowest index, which may cause different results unintended.

---

<div class="post-metadata">

**Author:** ![PearGod](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/peargod/32/9259_2.png) [@PearGod](https://discourse.julialang.org/u/PearGod)\
**Post date:** [January 3, 2019, 8:20pm UTC](https://discourse.julialang.org/t/julian-way-of-matlab-ismember-function/3740/11 "2019-01-03T20:20:07Z")

</div>

If using Lia = in.(a, [b]), does it use TimSort to keep within O(n) if the vectors are already sorted? Is TimSort the “default” algorithm for vector methods that require sorting?

(Note that searchsorted doesn’t handle searching for members of a vector within another vector in the same initially requested.)

---

<div class="post-metadata">

**Author:** ![bennedich](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bennedich/32/4894_2.png) [@bennedich](https://discourse.julialang.org/u/bennedich)\
**Post date:** [January 3, 2019, 9:22pm UTC](https://discourse.julialang.org/t/julian-way-of-matlab-ismember-function/3740/12 "2019-01-03T21:22:37Z")

</div>

There’s no sorting taking place. Given vectors `a` and `b`,

```julia
in.(a, [b])

```

is equivalent to

```julia
for x = a
    in(x, b)
end

```

which in turn is equivalent to

```julia
for x = a
    in = false
    for y = b
        (in = x == y) && break
    end
end

```

In other words, the complexity is `O(nm)` where `n` is the length of vector `a` and `m` is the length of vector `b`.

As stated above, if the vectors are already sorted, you can do much better, but as far as I know there’s no built-in support for that in Julia, in which case you’d have to implement it yourself or look for an existing package.

---

<div class="post-metadata">

**Author:** ![PearGod](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/peargod/32/9259_2.png) [@PearGod](https://discourse.julialang.org/u/PearGod)\
**Post date:** [January 5, 2019, 12:12am UTC](https://discourse.julialang.org/t/julian-way-of-matlab-ismember-function/3740/13 "2019-01-05T00:12:13Z")

</div>

Thanks for the response! And the really clear explanation of in.(a,[b]) and how it breaks down to in().

---

<div class="post-metadata">

**Author:** ![Amin\_Yahyaabadi](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/amin_yahyaabadi/32/9826_2.png) [@Amin\_Yahyaabadi](https://discourse.julialang.org/u/Amin_Yahyaabadi)\
**Post date:** [June 16, 2019, 10:42pm UTC](https://discourse.julialang.org/t/julian-way-of-matlab-ismember-function/3740/14 "2019-06-16T22:42:07Z")

</div>

For strings arrays use the following:

```julia
 A= ["Hey","Hello","Hi"];
 B= ["Hey","Hi","Goodbye","Bye"];
boolianIndex=occursin.(A,B); 

```

---

<div class="post-metadata">

**Author:** ![johann.spies](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/johann.spies/32/8805_2.png) [@johann.spies](https://discourse.julialang.org/u/johann.spies)\
**Post date:** [June 20, 2019, 10:20am UTC](https://discourse.julialang.org/t/julian-way-of-matlab-ismember-function/3740/15 "2019-06-20T10:20:24Z")

</div>

This does not work in version 1.1.0

```julia
julia> A= ["Hey","Hello","Hi"];

julia> B= ["Hey","Hi","Goodbye","Bye"];

julia> boolianIndex=occursin.(A,B); 
ERROR: DimensionMismatch("arrays could not be broadcast to a common size")
Stacktrace:
 [1] _bcs1 at ./broadcast.jl:438 [inlined]
 [2] _bcs at ./broadcast.jl:432 [inlined]
 [3] broadcast_shape at ./broadcast.jl:426 [inlined]
 [4] combine_axes at ./broadcast.jl:421 [inlined]
 [5] instantiate at ./broadcast.jl:255 [inlined]
 [6] materialize(::Base.Broadcast.Broadcasted{Base.Broadcast.DefaultArrayStyle{1},Nothing,typeof(occursin),Tuple{Array{String,1},Array{String,1}}}) at ./broadcast.jl:753
 [7] top-level scope at none:0

```

---

<div class="post-metadata">

**Author:** ![BeastyBlacksmith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/beastyblacksmith/32/4741_2.png) [@BeastyBlacksmith](https://discourse.julialang.org/u/BeastyBlacksmith)\
**Post date:** [June 20, 2019, 12:43pm UTC](https://discourse.julialang.org/t/julian-way-of-matlab-ismember-function/3740/16 "2019-06-20T12:43:52Z")

</div>

Thats correct, `occursin` is for checking whether one string occurs in another string, like `occursin("Hey", "HeyHo")`.  
If you want to check whether the strings in Array `A` are in Array `B`, you can still use `in.(A, Ref(B))`.  
Or if you want the index, `indexin(A,B)`.

---

<div class="post-metadata">

**Author:** ![Amin\_Yahyaabadi](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/amin_yahyaabadi/32/9826_2.png) [@Amin\_Yahyaabadi](https://discourse.julialang.org/u/Amin_Yahyaabadi)\
**Post date:** [June 20, 2019, 12:52pm UTC](https://discourse.julialang.org/t/julian-way-of-matlab-ismember-function/3740/17 "2019-06-20T12:52:48Z")

</div>

> [@Julian way of MATLAB ismember function?](https://discourse.julialang.org/t/julian-way-of-matlab-ismember-function/3740/15):
>
> This does not work in version 1.1.0 julia\> A= ["Hey","Hello","Hi"]; julia\> B= ["Hey","Hi","Goodbye","Bye"]; julia\> boolianIndex=occursin.(A,B); ERROR: DimensionMismatch("arrays could not be broadcast to a common size") Stacktrace: [1] \_bcs1 at ./broadcast.jl:438 [inlined] [2] \_bcs at ./broadcast.jl:432 [inlined] [3] broadcast\_shape at ./broadcast.jl:426 [inlined] [4] combine\_axes at ./broadcast.jl:421 [inlined] [5] instantiate at ./broadcast.jl:255 [inlined] [6] materialize(::Base.Broa…

Oh you are right, I wrote this example without testing it.  
One of the matrices should be only one string.  
Try the following:

```julia
A= ["Hey"];
B= ["Hey","Hi","Goodbye","Bye"];
boolianIndex=occursin.(A,B); 

```

> [@Julian way of MATLAB ismember function?](https://discourse.julialang.org/t/julian-way-of-matlab-ismember-function/3740/16):
>
> Thats correct, occursin is for checking whether one string occurs in another string, like occursin("Hey", "HeyHo"). If you want to check whether the strings in Array A are in Array B, you can still use in.(A, Ref(B)). Or if you want the index, indexin(A,B).

Thank you! `in.(A, Ref(B))` works perfectly.

---

<div class="post-metadata">

**Author:** ![mbauman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mbauman/32/31082_2.png) [@mbauman](https://discourse.julialang.org/u/mbauman)\
**Post date:** [June 20, 2019, 2:24pm UTC](https://discourse.julialang.org/t/julian-way-of-matlab-ismember-function/3740/18 "2019-06-20T14:24:12Z")

</div>

> [@Amin\_Yahyaabadi](#):
>
> in.(A, Ref(B))

If `B` is more than a handful of elements, I highly recommend:

```julia
in.(A, Ref(Set(B)))

```

as that’ll enable quick lookups of elements in `B` instead of repeatedly linearly scanning through the entire thing.
