|
Hello, I would like to efficiently do the following:
Input:
N = array size
k = maximum array element size
Output:
All arrays of size n with elements in the range 1..k
Example:
N = 3
k = 3
Output
(1,1,1) (1,1,2)(1,1,3)(1,2,1),(1,2,2),(1,2,3),(1,3,2),(1,3,3),(3,1,1),(3,1,2),(3,1,3),(3,1,2),(3,1,3),(3,2,1),(3,2,2),(3,2,3),(3,3,1),(3,3,2),(3,3,3)
The current implementation repeats the (1,2,..,k) N times and does
for s = 1:N-1
ext = [ext 1:k];
end;
result = unique(nchoosek(extended_input,N),'rows');
This works put is painfully inefficient for larger k due to the creation of the large redundant array ext and then creating its permutations via nchoosek .
Can I do this with native Matlab functions or do I need to write my own creation routine?
Thanks,
Stephan
|