# Setdiff along a dimension

**URL:** https://discourse.julialang.org/t/setdiff-along-a-dimension/42801
**Category:** Performance
**Created:** [July 9, 2020, 8:01pm UTC](https://discourse.julialang.org/t/setdiff-along-a-dimension/42801 "2020-07-09T20:01:58Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![mleprovost](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mleprovost/32/7166_2.png) [@mleprovost](https://discourse.julialang.org/u/mleprovost)
#### Post date: [July 9, 2020, 8:01pm UTC](https://discourse.julialang.org/t/setdiff-along-a-dimension/42801/1 "2020-07-09T20:01:58Z")

</div>

Hello,

I am transferring some code from Matlab. In Matlab, the function `setdiff` can take as input two 2D-arrays A and B, and returns the rows of A that are not in B.

In Julia, `setdiff` is working on collection of iterables, and not dimension of arrays. How can I improve the following code? In particular `Atmp` has type `Array{SubArray{Int64,1,Array{Int64,2},Tuple{Int64,Base.Slice{Base.OneTo{Int64}}},true},1}` which is not very convenient.

```julia
A = [0 0; 0 1; 0 2; 0 3; 1 0; 1 1]

B = [0 1; 2 0; 1 0]

Atmp = setdiff(eachslice(A; dims = 1), eachslice(B;dims = 1))

@show typeof(Atmp)
cardinal = size(Atmp,1)
Asetdiff = zeros(Int64,cardinal,2)

@inbounds for i=1:cardinal
       Asetdiff[i,:] = Atmp[i]
end

Asetdiff

```

4×2 Array{Int64,2}:  
0 0  
0 2  
0 3  
1 1

---

<div class="post-metadata">

### Author: ![lucas711642](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lucas711642/32/12051_2.png) [@lucas711642](https://discourse.julialang.org/u/lucas711642)
#### Post date: [July 9, 2020, 8:47pm UTC](https://discourse.julialang.org/t/setdiff-along-a-dimension/42801/2 "2020-07-09T20:47:02Z")

</div>

You may use something like:

```julia
copyto!(Asetdiff', Iterators.flatten(Atmp))

```

to make the code more generic. If you plan to work with arbitrary dimensions, you can replace the transposition with `PermutedDimsArray`.

---

<div class="post-metadata">

### Author: ![mleprovost](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mleprovost/32/7166_2.png) [@mleprovost](https://discourse.julialang.org/u/mleprovost)
#### Post date: [July 9, 2020, 9:25pm UTC](https://discourse.julialang.org/t/setdiff-along-a-dimension/42801/3 "2020-07-09T21:25:30Z")

</div>

Thank you for your answer
