Intersection of three and more arrays

Hi, is it possible in matlab to find intersection of three and more arrays and index of intersection value in each of three array? Like [C,ia,ib] = intersect(A,B) but for three or more arrays.

 Accepted Answer

Bruno Luong
Bruno Luong on 15 Nov 2018
My FEX returns the index of the arrays

8 Comments

Thank you, it gives good result, but not i wanted, i think i asked incorretly. Is it possible with you function find their first intersection and its index? for example a=[5,6,4,7,8], b= [7,8,6,5,4] and c=[4,8,5,6,7], first intersection of three arrays is 6
Bruno Luong
Bruno Luong on 15 Nov 2018
Edited: Bruno Luong on 15 Nov 2018
What is the first intersection of
a = [5 6] and b = [6 5] ?
Sorry I don't know what is a "first intersection".
Intersection is common elements, an element of the intersection can appear anywhere in the sets, e.g. above, it can appears fisrt in one set and last in other set.
If you want to figure out what is the first in a particular set, sort the index output to find the lowest.
jonas
jonas on 15 Nov 2018
Edited: jonas on 15 Nov 2018
@Baisseyev,
In your example, would not 5 and 6 both be the "first intersection", granted that you scan each array from left to right and find the first intersection.
i have 7 arrays, each contain elemnts from 1 one to 60 in different order, i want to find first element which will be repeated in this 7 arrays for example it is 31 but in first array its index 5, in second 15 and so on so i want find this number(31) and its indexes in this 7 arrays (5, 15 ...)
Please define "first element". Your prose is so far meaningless for those who read.
Nobody knows why you pick 31 as the first of the 7 arrays which nobody know the contents.
As with above: why you pick 6 as the first for : a=[5,6,4,7,8], b= [7,8,6,5,4] and c=[4,8,5,6,7]?
To me the first intersection in a is 5; in b is 7; in c is 4.
31 is an example, in a=[5,6,4,7,8], b= [7,8,6,5,4] and c=[4,8,5,6,7], is the first to be repeated in a, b, c it like this if you look at first element in each array it is 5, 7, 4 then 6,8,8, then 4,6,5 then 7,5,6 till here 5 (here mistake not 6) was the first element which was repeat in all three arrays, so i want to find this 5 and indexes in this arrays. Maybe here needed another function, not intersection
IIUC, what you want is the common element with the lowest max indexes over all the set.
a = [5,6,4,7,8],
b = [7,8,6,5,4],
c = [4,8,5,6,7],
sets = {a,b,c};
nset = length(sets);
idx = cell(1,nset);
[s,idx{:}] = mintersect(sets{:});
idx = cat(1,idx{:});
imax = accumarray(repmat((1:length(s))',nset,1),idx,[],@max);
[~,iminmax] = min(imax);
firstelem = s(iminmax) % returns 5
Yes, thank you very much for spending your time

Sign in to comment.

More Answers (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!