MATLAB: Split Number into digits adding up to it.

splitsum

I need to split a number into smaller integers that add up to it.
So 4 -> [0,4],[1,3],[2,2],[3,1],[4,0]
6 -> [0,6],[1,5],[2,4],[3,3]…
I know this can be done with for loops but wanted to see if there is another way.

Best Answer

What about
f = @(n) [0:n; n:-1:0].';
Result
>> f(4)
ans =
0 4
1 3
2 2
3 1
4 0
>> f(6)
ans =
0 6
1 5
2 4
3 3
4 2
5 1
6 0
Related Question