Use a single cell array, with one entry for each of the number of levels you want to carry this out to. Each entry will be a numeric array with the same number of columns as (cell index minus 1)
To get level M from level (M-1), take the array from level M-1 and find out how many rows it currently has, R. Now repmat() the content of that previous level vertically N times, and append a new column which is the elements 1:N repeated vertically R times each.
The only explicit loop needed is according to the number of levels you are using. (repmat or repelem might use implicit loops.)
Best Answer