Split Number into digits adding up to it.
Ältere Kommentare anzeigen
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.
Akzeptierte Antwort
Weitere Antworten (1)
KSSV
am 10 Sep. 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)]
Kategorien
Mehr zu Debugging and Improving Code finden Sie in Hilfe-Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!