# Julia Equivalent for ismember in MATLAB for rows

**URL:** <https://discourse.julialang.org/t/julia-equivalent-for-ismember-in-matlab-for-rows/36658>\
**Category:** New to Julia\
**Created:** [March 28, 2020, 10:14pm UTC](https://discourse.julialang.org/t/julia-equivalent-for-ismember-in-matlab-for-rows/36658 "2020-03-28T22:14:38Z")\
**Posts on this page:** 15\
**Page:** 1

<div class="post-metadata">

**Author:** ![Donald\_Lacombe](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/donald_lacombe/32/13500_2.png) [@Donald\_Lacombe](https://discourse.julialang.org/u/Donald_Lacombe)\
**Post date:** [March 28, 2020, 10:14pm UTC](https://discourse.julialang.org/t/julia-equivalent-for-ismember-in-matlab-for-rows/36658/1 "2020-03-28T22:14:38Z")

</div>

Hello!

I’m trying to convert the following MATLAB code into Julia:

```julia
bt_edges =

     6 7
     6 8
     7 8

bt_edges(i,:)

ans =

     7 8

uval = ismember(bt_edges,bt_edges(i,:),'rows');

uval =

  3×1 logical array

   0
   0
   1

```

In other words, I’d like to see if this row vector `bt_edges(i,:)` matches any row in the matrix `bt_edges` and return a logical vector like in the MATLAB code.

I’ve tried the following in Julia to no avail:

```julia
julia> uval = in.(bt_edges,bt_edges[i,1])
3×2 BitArray{2}:
 1 0
 1 0
 0 0

julia> in.(bt_edges[1,:], Ref(bt_edges))
2-element BitArray{1}:
 1
 1

julia> indexin(bt_edges,bt_edges[1,:])
3×2 Array{Union{Nothing, Int64},2}:
 1 2       
 1 nothing
 2 nothing

```

If anyone can provide a solution, I’d appreciate it.

Thank you.

---

<div class="post-metadata">

**Author:** ![Daniel\_Berge](https://avatars.discourse-cdn.com/v4/letter/d/eb9ed0/32.png) [@Daniel\_Berge](https://discourse.julialang.org/u/Daniel_Berge)\
**Post date:** [March 28, 2020, 10:39pm UTC](https://discourse.julialang.org/t/julia-equivalent-for-ismember-in-matlab-for-rows/36658/2 "2020-03-28T22:39:53Z")

</div>

You are trying to check equality for each row, so you can do the following.

```julia
bt_edges=[6 7; 6 8; 7 8]

Ref(bt_edges[3,:]) .== eachrow(bt_edges)
3-element BitArray{1}:
 0
 0
 1

```

---

<div class="post-metadata">

**Author:** ![Iulian.Cioarca](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/iulian.cioarca/32/30166_2.png) [@Iulian.Cioarca](https://discourse.julialang.org/u/Iulian.Cioarca)\
**Post date:** [March 28, 2020, 10:43pm UTC](https://discourse.julialang.org/t/julia-equivalent-for-ismember-in-matlab-for-rows/36658/3 "2020-03-28T22:43:55Z")

</div>

`uval = all(in.(bt_edges[3,:]',bt_edges),dims=2)`

---

<div class="post-metadata">

**Author:** ![BLI](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bli/32/37206_2.png) [@BLI](https://discourse.julialang.org/u/BLI)\
**Post date:** [March 28, 2020, 10:58pm UTC](https://discourse.julialang.org/t/julia-equivalent-for-ismember-in-matlab-for-rows/36658/4 "2020-03-28T22:58:28Z")

</div>

My MATLAB competence is a little bit rusty, but what is the value of `i` in your MATLAB code? I assume you have set `i=3` somewhere?

Anyway, is this what you want to do?

```julia
julia> bt_edges = [6 7; 6 8; 7 8]
3×2 Array{Int64,2}:
 6 7
 6 8
 7 8
julia> uval = [bt_edges[i,:] == bt_edges[3,:] for i in 1:3]
3-element Array{Bool,1}:
 0
 0
 1

```

---

<div class="post-metadata">

**Author:** ![BLI](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bli/32/37206_2.png) [@BLI](https://discourse.julialang.org/u/BLI)\
**Post date:** [March 28, 2020, 11:11pm UTC](https://discourse.julialang.org/t/julia-equivalent-for-ismember-in-matlab-for-rows/36658/5 "2020-03-28T23:11:08Z")

</div>

I like this solution. It seems to be slightly faster than

```julia
uval = all(bt_edges[3,:]' .== bt_edges),dims=2)

```

---

<div class="post-metadata">

**Author:** ![Iulian.Cioarca](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/iulian.cioarca/32/30166_2.png) [@Iulian.Cioarca](https://discourse.julialang.org/u/Iulian.Cioarca)\
**Post date:** [March 29, 2020, 8:17am UTC](https://discourse.julialang.org/t/julia-equivalent-for-ismember-in-matlab-for-rows/36658/6 "2020-03-29T08:17:35Z")

</div>

> [@BLI](#):
>
> uval = all(bt\_edges[3,:]’ .== bt\_edges),dims=2)

I can’t see much difference between the two:

```julia
using BenchmarkTools
bt_edges = ones(100_000,2)
bt_edges = vcat(bt_edges,[7 8])
m,n = size(bt_edges)
k = 100_001

#1. "eachrow"
@benchmark Ref(bt_edges[k,:]) .== eachrow(bt_edges)
  memory estimate: 5.36 MiB
  allocs estimate: 100016
  --------------
  minimum time: 4.382 ms (0.00% GC)
  median time: 4.934 ms (0.00% GC)
  mean time: 5.680 ms (12.84% GC)
  maximum time: 15.244 ms (61.94% GC)

#2. broadcasted "in"
@benchmark uval = all(in.(bt_edges[k,:]',bt_edges),dims=2)
  memory estimate: 41.58 KiB
  allocs estimate: 20
  --------------
  minimum time: 1.882 ms (0.00% GC)
  median time: 1.916 ms (0.00% GC)
  mean time: 1.937 ms (0.43% GC)
  maximum time: 12.750 ms (84.85% GC)

#3. "comprehension"
@benchmark uval = [bt_edges[i,:] == bt_edges[k,:] for i in 1:m]
  memory estimate: 29.06 MiB
  allocs estimate: 898482
  --------------
  minimum time: 313.920 ms (0.00% GC)
  median time: 316.718 ms (0.93% GC)
  mean time: 319.315 ms (0.58% GC)
  maximum time: 336.991 ms (0.88% GC)

#4. "broadcasted =="
@benchmark uval = all((bt_edges[k,:]' .== bt_edges),dims=2)
  memory estimate: 41.58 KiB
  allocs estimate: 20
  --------------
  minimum time: 1.868 ms (0.00% GC)
  median time: 1.913 ms (0.00% GC)
  mean time: 1.930 ms (0.43% GC)
  maximum time: 12.869 ms (84.81% GC)

```

---

<div class="post-metadata">

**Author:** ![Sukera](https://avatars.discourse-cdn.com/v4/letter/s/ce7236/32.png) [@Sukera](https://discourse.julialang.org/u/Sukera)\
**Post date:** [March 29, 2020, 8:57am UTC](https://discourse.julialang.org/t/julia-equivalent-for-ismember-in-matlab-for-rows/36658/7 "2020-03-29T08:57:27Z")

</div>

> [@Donald\_Lacombe](#):
>
> and return a logical vector like in the MATLAB code.

Note that logical indexing creates a temporary array (the vector saving the indices), potentially leading to slower than necessary code if they are created in a hot loop.

From the [docs](https://docs.julialang.org/en/v1/manual/noteworthy-differences/#Noteworthy-differences-from-MATLAB-1):

> In MATLAB, an idiomatic way to remove unwanted values is to use logical indexing, like in the expression `x(x>3)` or in the statement `x(x>3) = []` to modify `x` in-place. In contrast, Julia provides the higher order functions [`filter`](https://docs.julialang.org/en/v1/base/collections/#Base.filter) and [`filter!`](https://docs.julialang.org/en/v1/base/collections/#Base.filter!), allowing users to write `filter(z->z>3, x)` and `filter!(z->z>3, x)` as alternatives to the corresponding transliterations `x[x.>3]` and `x = x[x.>3]` . Using [`filter!`](https://docs.julialang.org/en/v1/base/collections/#Base.filter!) reduces the use of temporary arrays.

Since you seem to be iterating over the rows of the matrix, maybe you can tell us more about what you want to do with those logical indices?

Also, you can get the unique rows of your matrix by a simple call to `unique`:

```julia
julia> A = vcat((a,b,a,a,a,a,b,b,b)...)
9×3 Array{Int64,2}:                    
 1 2 3                               
 3 4 5                               
 1 2 3                               
 1 2 3                               
 1 2 3                               
 1 2 3                               
 3 4 5                               
 3 4 5                               
 3 4 5                                                                      

julia> unique(A, dims=1)               
2×3 Array{Int64,2}:                    
 1 2 3                               
 3 4 5

```

---

<div class="post-metadata">

**Author:** ![Donald\_Lacombe](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/donald_lacombe/32/13500_2.png) [@Donald\_Lacombe](https://discourse.julialang.org/u/Donald_Lacombe)\
**Post date:** [March 29, 2020, 3:43pm UTC](https://discourse.julialang.org/t/julia-equivalent-for-ismember-in-matlab-for-rows/36658/8 "2020-03-29T15:43:29Z")

</div>

First, thank you to all of you for providing answers; it is much appreciated!

In terms of what I want to do, the next lines of MATLAB code are the following:

```julia
if sum(uval)==1
            edgep = [edgep;bt_edges(i,:)];
        end

```

The variable `uval` is created and then subjected to the test. The logical vector `uval` is summed and if it equals 1, then the edgep variable is created, which basically builds a matrix and appends the next row. Apparently, MATLAB allows for this type of recursive appending, but I’ve had to try different things in Julia to make it work, and I have not been successful.

My ultimate goal is to convert some Delaunay triangularization code from MATLAB to Julia, which I’ll share when done.

Thank you all again.

---

<div class="post-metadata">

**Author:** ![Iulian.Cioarca](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/iulian.cioarca/32/30166_2.png) [@Iulian.Cioarca](https://discourse.julialang.org/u/Iulian.Cioarca)\
**Post date:** [March 29, 2020, 9:35pm UTC](https://discourse.julialang.org/t/julia-equivalent-for-ismember-in-matlab-for-rows/36658/9 "2020-03-29T21:35:30Z")

</div>

In terms of “growing” an array you can use `append!` or `vcat` functions, although it’s not always the most efficient way.

---

<div class="post-metadata">

**Author:** ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)\
**Post date:** [March 29, 2020, 10:29pm UTC](https://discourse.julialang.org/t/julia-equivalent-for-ismember-in-matlab-for-rows/36658/10 "2020-03-29T22:29:34Z")

</div>

Concatenating rows is really inefficient, because you have to reallocate the entire array each time, and since Julia (like Matlab) arrays are column major, it’s especially problematic.

If you care about performance, I would suggest that you think in columns instead, then you can append data without having to make a new array each time. To do this, start with a vector, append data, and then in the end reshape into a matrix. Doing this could be 10-100x faster than appending rows (depending on sizes etc.)

---

<div class="post-metadata">

**Author:** ![BLI](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bli/32/37206_2.png) [@BLI](https://discourse.julialang.org/u/BLI)\
**Post date:** [March 29, 2020, 10:32pm UTC](https://discourse.julialang.org/t/julia-equivalent-for-ismember-in-matlab-for-rows/36658/11 "2020-03-29T22:32:22Z")

</div>

Hm. I just did `@btime` a couple of times on my media computer, and for a couple of cases there, #2 was some 10% faster. But perhaps that was a coincidence. `@benchmark` probably gives a better picture.

---

<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:** [March 29, 2020, 10:36pm UTC](https://discourse.julialang.org/t/julia-equivalent-for-ismember-in-matlab-for-rows/36658/12 "2020-03-29T22:36:40Z")

</div>

> [@DNF](#):
>
> If you care about performance, I would suggest that you think in columns instead, then you can append data without having to make a new array each time.

Unless there’s some actual linear algebra taking place in the code, an even better option may be to work with a vector of vectors or a vector of tuples.

---

<div class="post-metadata">

**Author:** ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)\
**Post date:** [March 29, 2020, 10:51pm UTC](https://discourse.julialang.org/t/julia-equivalent-for-ismember-in-matlab-for-rows/36658/13 "2020-03-29T22:51:44Z")

</div>

BTW, I think your Matlab code, too, would benefit from being column-oriented.

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [March 30, 2020, 6:25am UTC](https://discourse.julialang.org/t/julia-equivalent-for-ismember-in-matlab-for-rows/36658/14 "2020-03-30T06:25:53Z")

</div>

> [@Donald\_Lacombe](#):
>
> In terms of what I want to do, the next lines of MATLAB code are the following

Translating Matlab code line-by-line to Julia may not be the best approach: even when you can make it work, it is very likely that it will be suboptimal and unnecessarily complicated.

Instead, I would recommend that you invest into

1. understanding your algorithm,
2. learning Julia, possibly on a simpler project,
3. exploring the many nice data structures that are available in packages

and build up your code from small, simple functions (which is the opposite of prevalent Matlab style).

For example, since the dimension should determine the number of edges,

> **[GitHub - JuliaArrays/StaticArrays.jl: Statically sized arrays for Julia](https://github.com/JuliaArrays/StaticArrays.jl)**
>
> Statically sized arrays for Julia. Contribute to JuliaArrays/StaticArrays.jl development by creating an account on GitHub.

Alternatively,

> **[GitHub - JuliaArrays/ElasticArrays.jl: Resizeable multi-dimensional arrays...](https://github.com/JuliaArrays/ElasticArrays.jl)**
>
> Resizeable multi-dimensional arrays for Julia. Contribute to JuliaArrays/ElasticArrays.jl development by creating an account on GitHub.

can provide matrices that grow.

---

<div class="post-metadata">

**Author:** ![Donald\_Lacombe](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/donald_lacombe/32/13500_2.png) [@Donald\_Lacombe](https://discourse.julialang.org/u/Donald_Lacombe)\
**Post date:** [March 30, 2020, 3:50pm UTC](https://discourse.julialang.org/t/julia-equivalent-for-ismember-in-matlab-for-rows/36658/15 "2020-03-30T15:50:32Z")

</div>

I agree 100% with this but I’m looking for a solution in Julia, for the time being, that is quick and dirty. I’ve tried to find what I need but it turn out rolling my own code is the solution.

I think one of the appealing aspects of Julia is that one can open an IJulia session and interactively try out ideas.

Of course, there are more elegant solutions, but again, this is a minor part of my bigger project which is building a spatial econometrics package for Julia.

I’m almost done and need this particular code to just make sure I have an alpha version to make sure everything works before optimizing.

I would like to thank everyone for their suggestions and the great ideas.
