Split Number into digits adding up to it.
Mostrar comentarios más antiguos
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.
2 comentarios
"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]"
What about [1,1,1,1],[1,1,2],[1,2,1],[2,1,1],etc., and [4]?
How many zeros are allowed? What about [0,0,0,4], [0,0,2,2], etc.?
Alberto Lorenzo
el 14 de Sept. de 2020
Respuesta aceptada
Más respuestas (1)
KSSV
el 10 de Sept. de 2020
n = 4 ;
[X,Y] = meshgrid(0:n,0:n) ;
thesum = X+Y ;
idx = thesum==n ;
iwant = [X(idx) Y(idx)]
n = 6 ;
[X,Y] = meshgrid(0:n,0:n) ;
thesum = X+Y ;
idx = thesum==n ;
iwant = [X(idx) Y(idx)]
Categorías
Más información sobre App Building en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!