Setdiff along a dimension

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.

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

You may use something like:

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.

1 Like

Thank you for your answer