How can do intersection and union of two vectors?
Show older comments
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:
result_union=[256,85,1,22,6,8,25,4,2,5,589,78,25,3];
result_intersect=[1,6,25];
Please, help me how can can I do it?
Accepted Answer
More Answers (1)
Titus Edelhofer
on 8 Jun 2015
0 votes
Hi,
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 ...
Titus
2 Comments
Sahar abdalah
on 8 Jun 2015
Titus Edelhofer
on 8 Jun 2015
There is an ordering parameter for intersect and union:
intersect(A,B, 'stable')
although for union is does not help (directly), since it's using the values from A first and then from B. I guess you need to work it up "by hand".
Titus
Categories
Find more on Matrix Indexing in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!