You can by using cellfun, but as stated here, it is not a good idea.
For doing so, with one loop:
c{1}=[1 2 3 4 5 6]; c{2}=[1 3 5 7];c{3}=[2 4 6 8];
c{4}=[1 4 6];c{5}=[3 7];
cSize = numel( c);
isect=cell(1,cSize)
for k=1:cSize
isect{k}=cellfun(@(in) intersect(in,c{k}),c,'UniformOutput',false);
end
This procedure may be repeated to eliminate the other for:
c{1}=[1 2 3 4 5 6]; c{2}=[1 3 5 7];c{3}=[2 4 6 8];
c{4}=[1 4 6];c{5}=[3 7];
isect=cellfun(@(in) cellfun(@(in2) intersect(in,in2),c,'UniformOutput',false),c,'UniformOutput',false);
isect{i}{j}
is the intersection from c{i}
to {j}
Note: cellfun will do the loop internally over the cell value, so in fact, you are not removing the loops.
Although this was not the initial question, finding the subsets:
c{1}=[1 2 3 4 5 6]; c{2}=[1 3 5 7];c{3}=[2 4 6 8];
c{4}=[1 4 6];c{5}=[3 7];c{6}=[];
isSubset=cell2mat(cellfun(@(in) cellfun(@(in2) isequal(intersect(in,in2),in)|isempty(in),c),c,'UniformOutput',false)');
Results:
isSubset =
1 0 0 0 0 0
0 1 0 0 0 0
0 0 1 0 0 0
1 0 0 1 0 0
0 1 0 0 1 0
1 1 1 1 1 1
Which returns a boolean if k
is a subset of m
by doing isSubset(k,m)
.