R = dlmread('retail.dat');
top_p = 24;   % first 24 products
top_n = 600;  % first 600 baskets

[n,m] = size(R);
X = sparse(top_n, top_p);
for i = 1:top_n
    if R(i, 1) < top_p
        % first item might be zero 
        X(i, R(i,1)+1) = 1;
    end
    for j = 2:m
        % second to last item cannot be zero
        if R(i,j) == 0 || R(i,j) >= top_p
            X(i, R(i, 2:j-1)+1) = 1;
        	break
        end
    end
end

[L,s] = apriori(X, 0.002);

for i = 1:size(L,1)
    fprintf('itemset %2d (support=%f):', i, s(i));
    for j = 1:size(L,2)
        if L(i,j)
            fprintf(' %d', j);
        end
    end
    fprintf('\n');
end
