lara
lara

Reputation: 874

Using setdiff to compare multiple columns in Julia

I have a matrix X with 3 columns [x1 x2 x3] and n rows. I have a second matrix Y with three columns [y1 y2 y3] and m rows. I would like to return all rows from X which are not identical to rows in Y.

I have been trying to use the setdiff function to do this but I can't work out how to use this function to compare columns simultaneously (ie compare [x1 y1] [x2 y2] [x3 y3]) and base the output on this comparison instead of basing the output on a single column comparison.

I would like the result to be: For some row i of X being equal to some row j of Y [x1 x2 x3] = [y1 y2 y3] then the row of X will be excluded in the output of the new X.

Upvotes: 2

Views: 847

Answers (1)

amrods
amrods

Reputation: 2131

setdiff iterates over both arguments, what you need to do is create a collection of rows for both matrices. This might not be the most efficient way but it works:

x = [1 2 3; 4 5 6; 7 8 9; 10 11 12]
xrows = Vector()
for i in 1:size(x, 1)
    push!(xrows, x[i, :])
end

y = [1 1 1 ; 4 5 6; 9 9 9]
yrows = Vector()
for i in 1:size(y, 1)
    push!(yrows, y[i, :])
end

vcat(setdiff(xrows, yrows)...)

Upvotes: 1

Related Questions