You keep asking questions very closely related, and getting answers for them, you should try to connect all the bits by yourself.
In this case, start with the excellent answer from Horchler, inject this result in the (just as excellent) answer from Luis Mendo to get all possible combinations (which you were given many times in your several questions), then build your final matrix.
In practice:
%% // initial data
A = {[2 31 40],[11 17],[5 8]} ;
%% // count a few things before we start
nBlock = numel(A) ; %// number of elements sub matrix in "A"
nElem = cellfun( @numel , A ) ; %// number of elements in each sub matrix of "A"
nLines = prod(nElem) ; %// number of lines in the final matrix
%% // Horchler solution (to get all the sub-matrix you requested)
B = cellfun(@(n) bsxfun(@plus, n, 100*eye(numel(n))), A, 'UniformOutput', false) ;
%% // connect both solution
Lines = arrayfun( @linspace , ones(1,nBlock) , nElem , nElem , 'UniformOutput',false) ;
%% // Luis Mendo solution
% // https://stackoverflow.com/questions/21895335/generate-a-matrix-containing-all-combinations-of-elements-taken-from-n-vectors
nBlock = numel(Lines); %// number of vectors
combs = cell(1,nBlock); %// pre-define to generate comma-separated list
[combs{end:-1:1}] = ndgrid(Lines{end:-1:1}); %// the reverse order in these two
%// comma-separated lists is needed to produce the rows of the result matrix in lexicographical order
combs = cat(nBlock+1, combs{:}); %// concat the n n-dim arrays along dimension n+1
combs = reshape(combs,[],nBlock); %// reshape to obtain desired matrix
%% // Finalisation (can be optimized but it works as it is)
for ii=nLines:-1:1
tmpLine = [] ;
for jj=1:nBlock
tmpLine = [ tmpLine B{1,jj}(combs(ii,jj),:) ] ; %// %#ok<AGROW>
end
C(ii,:) = tmpLine ;
end
gives you
>> C
C =
102 31 40 111 17 105 8
102 31 40 111 17 5 108
102 31 40 11 117 105 8
102 31 40 11 117 5 108
2 131 40 111 17 105 8
2 131 40 111 17 5 108
2 131 40 11 117 105 8
2 131 40 11 117 5 108
2 31 140 111 17 105 8
2 31 140 111 17 5 108
2 31 140 11 117 105 8
2 31 140 11 117 5 108