Split Number into digits adding up to it.
4 ビュー (過去 30 日間)
古いコメントを表示
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 件のコメント
採用された回答
Ameer Hamza
2020 年 9 月 10 日
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
2 件のコメント
その他の回答 (1 件)
KSSV
2020 年 9 月 10 日
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)]
0 件のコメント
参考
カテゴリ
Help Center および File Exchange で Line Plots についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!