function recursive(startNumber,maxSum)
currentArr = (startNumber);
matrix = recursiveHelper(startNumber, maxSum, 0, currentArr, [1 0 -1], {});
disp('combinations are');
disp(matrix);
end
function combinationMatrix = recursiveHelper(startNumber, maxSum, currentSum, currentArr, possibleChanges, combinationMatrix)
if startNumber < 1
return;
end
currentSum = currentSum + startNumber;
if currentSum + startNumber - 1 > maxSum
combinationMatrix{end + 1} = currentArr;
return;
end
for i = 1:length(possibleChanges)
newNumber = startNumber + possibleChanges(i);
if newNumber < 1
continue;
end
currentArr = [currentArr newNumber];
if currentSum + newNumber < maxSum
combinationMatrix = recursiveHelper(newNumber, maxSum, currentSum, currentArr, possibleChanges, combinationMatrix);
end
currentArr = currentArr(1:end-1);
end
end
3 Comments
Direct link to this comment
https://fr.mathworks.com/matlabcentral/answers/644798-combination-of-choices-recursive-function#comment_1126623
Direct link to this comment
https://fr.mathworks.com/matlabcentral/answers/644798-combination-of-choices-recursive-function#comment_1126623
Direct link to this comment
https://fr.mathworks.com/matlabcentral/answers/644798-combination-of-choices-recursive-function#comment_1126678
Direct link to this comment
https://fr.mathworks.com/matlabcentral/answers/644798-combination-of-choices-recursive-function#comment_1126678
Direct link to this comment
https://fr.mathworks.com/matlabcentral/answers/644798-combination-of-choices-recursive-function#comment_1136593
Direct link to this comment
https://fr.mathworks.com/matlabcentral/answers/644798-combination-of-choices-recursive-function#comment_1136593
Sign in to comment.