Setdiff line by line without using loops in matlab

Let, say, two matrices

A = [1,2,3;
     2,4,5;
     8,3,5]
B=  [2,3;
     4,5;
     8,5]

How to execute sedifffor each row in A and B, respectively, without using loops or cellfun, in other words, execution setdiff(A(i,:),B(i,:))for everyone i. In this example, I want to get

[1;
 2;
 3]

I am trying to do this for two very large matrices for my fluid simulator, so I cannot compromise on performance.

UPDATE:

you can assume that the second dimension (the number of columns) of the response will be fixed, for example. the answer will always be some matrix n by m, and not some dangling array of different column sizes.

Another example:

A B m 3 m 2 , m 1. , m n1, m n2 m n3 . -

A = [1,2,3,4,5;
     8,4,7,9,6]
B = [2,3;
     4,9]

C = [1,4,5;
     8,7,6]
+4
1

# 1 bsxfun -

mask = all(bsxfun(@ne,A,permute(B,[1 3 2])),3);
At = A.'; %//'
out = reshape(At(mask.'),[],size(A,1)).'

-

>> A
A =
     1     2     3     4     5
     8     4     7     9     6
>> B
B =
     2     3
     4     9
>> mask = all(bsxfun(@ne,A,permute(B,[1 3 2])),3);
>> At = A.'; %//'
>> out = reshape(At(mask.'),[],size(A,1)).'
out =
     1     4     5
     8     7     6

# 2 diff sort -

sAB = sort([A B],2)
dsAB = diff(sAB,[],2)~=0

mask1 = [true(size(A,1),1) dsAB]
mask2 = [dsAB true(size(A,1),1)]

mask = mask1 & mask2
sABt = sAB.'

out = reshape(sABt(mask.'),[],size(A,1)).'
+5

Source: https://habr.com/ru/post/1606705/


All Articles