You can use ndgrid . This function performs exactly what you are looking for.
[b1 b2 b3]=ndgrid([1 2],[0],[2 3 4]); [b1(:) b2(:) b3(:)] ans = 1 0 2 2 0 2 1 0 3 2 0 3 1 0 4 2 0 4
To answer the complete question, you need to get [1 2],[0],[2 3 4] from your log variable:
log = [1 1 0; 0 1 1; 1 0 1; 0 0 1]; log=bsxfun(@times,log,[1 0 1]); poss=cellfun(@find,mat2cell(log,size(log,1),ones(1,size(log,2))),'UniformOutput',0) poss(cellfun(@isempty,poss))={0} basket=cell(1,size(log,2)); [basket{:}]=ndgrid(poss{:}); basket=cell2mat(cellfun(@(x) x(:),basket,'UniformOutput',0))
basket =
1 0 2 3 0 2 1 0 3 3 0 3 1 0 4 3 0 4
source share