How can do intersection and union of two vectors?
73 views (last 30 days)
Hello everyone, I have two vector A and B :
A=[256 1 6 8 4 5 78 25];
B=[85 22 1 25 2 589 6 3];
I want to have other vector result that consist of the union or the intersection of the elements in A and B like this:
Please, help me how can can I do it?
Stephen23 on 8 Jun 2015
Edited: Stephen23 on 8 Jun 2015
Intersection: simply use intersect with the stable option:
>> A = [256,1,6,8,4,5,78,25];
>> B = [85,22,1,25,2,589,6,3];
1 6 25
Union: to keep the order of the two vectors (like they were zipped or interleaved together), then you could concatenate the vectors and then use unique like this:
256 85 1 22 6 8 25 4 2 5 589 78 3
If the vectors have different lengths then you could use this FEX submission to zip them together (instead of the concatenation and rehsape in my example):
More Answers (1)
Titus Edelhofer on 8 Jun 2015
did you try to enter union or intersect in the documentation search? It should give you at least for the intersection a good result ;-).
Regarding the union it depends on if you require exact the ordering that you gave ...