Finding columns of a matrix that are the same for combinations of events How I can change this multi-loop to reduce the computation time?

A is a matrix (5 × 10000) where the fifth line contains values between 1 and 50 corresponding to different events of an experiment. My goal is to find the columns of the matrix which are the same for all possible combinations of 7 different events.

data = A(1:4,:);
exptEvents = A(5,:);

% Find repeats
[b,i,j] = unique(data', 'rows');

% Organize the indices of the repeated columns into a cell array
reps = arrayfun(@(x) find(j==x), 1:length(i), 'UniformOutput', false);

% Find events corresponding to these repeats
reps_Events = cellfun(@(x) exptEvents(x), reps, 'UniformOutput', false);

U = cellfun(@unique, reps_Events, 'UniformOutput', false);
repeat_counts = cellfun(@length, U);


kk=1;

for i1=1:50
    for i2=1:50 
        for i3=1:50 
            for i4=1:50
                for i5=1:50
                    for i6=1:50
                        for i7=1:50
                            if i1~=i2 &amp;&amp; i2~=i3 &amp;&amp; i3~=i4 &amp;&amp; i4~=i5 &amp;&amp; i5~=i6 &amp;&amp; i6~=i7   
                               myEvents = [i1 i2 i3 i4 i5 i6 i7];
                               v= b(cellfun(@(x)all(ismember(myEvents,x)),U),:);
                               intersection(1,kk)=v(1);
                               intersection(2,kk)=v(2);
                               intersection(3,kk)=v(3);
                               intersection(4,kk)=v(4);
                               intersection(5,kk)=i1;
                               intersection(6,kk)=i2;
                               intersection(7,kk)=i3;
                               intersection(8,kk)=i4;
                               intersection(9,kk)=i5;
                               intersection(10,kk)=i6;
                               intersection(11,kk)=i7;
                               kk=kk+1;
                            end
                        end
                    end
                end
            end
        end
    end
end
There's quite a few things that you can do to reduce computation time.

First of all, if you know that an outcome has to be the combination of seven events, you can throw out whatever has fewer of them.

Then, you can preassign the output array (growing in a loop is usually not very fast).

Finally, you want to generate all possible subsets if there are, say, 9 events from which you can choose 7. 

So you'd start

%# throw out useless stuff
tooFewIdx = repeat_counts &lt; 7;

b(tooFewIdx,:) = [];
U(tooFewIdx) = [];
repeat_counts(tooFewIdx) = [];

%# estimate how many combinations you will get
nPerms = arrayfun(@(x)nchoosek(x,7),repeat_counts);

intIdx = [0;cumsum(nPerms(:))];

%# pre-assign output
%# If this runs out of memory, try using integer formats
%# instead, e.g. zeros(...,'uint8') if no value is above 255
intersection = zeros(intIdx(end),11);

%# loop to fill intersection
for i=1:length(U)

%# fill in the experiment information
intersection((intIdx(i)+1):intIdx(i+1),1:4) = repmat(b(i,:),nPerms(i),1);

%# add all combinations of events
intersection((intIdx(i)+1):intIdx(i+1),5:11) = combnk(U{i},7);

end