I would like to generate all possible integer arrays of a given length L, up to a maximum element size M.
The minimum element size is 1.
If M = 3, and L = 2, the output would be the following:
[1,1]
[1,2]
[1,3]
[2,1]
[2,2]
[2,3]
[3,1]
[3,2]
[3,3]
There are M^L different combinations, so I guess the Matlab code would look something like this:
function [arrays] = allArrays(M,L)
for i = 1:(M^L)
arrays(i) = % Something here that translates i to the desired array.
end
end
I’m not sure what should go in the middle of the loop, any help would be much appreciated!
You should use
ndgrid:If you want to give
MandLas input, you should do the following trick:result: