I need to find all possible combinations of taking r from n numbers in array without duplicates, with repetition, for example from arr = [ 1 2 3]
If my r = 2
My result should be:
1 1
1 2
1 3
2 2
2 3
3 3
I have wrote two functions, one of them is recursive, I get the error: "Out of memory. The likely cause is an infinite recursion within the program."
My code:
function repeat(chosen, arr, index, r, start, ends)
if(index == r)
for i = 1:r
arr(chosen(i))
end
end
for i = start:ends
chosen(index) = i;
repeat(chosen, arr, index, r, i, ends)
end
end
function combs(arr, n, r)
chosen = zeros(1, r+1, 'int8');
repeat(chosen, arr, 1, r, 1, n);
end