Create a matrix where every row adds to a number and within the matrix represents every possibility of the row combinations

4 ビュー (過去 30 日間)
Hi,
I am looking to make a matix with "n" columns and have each row add up to "k" which is a positive interger. The number of rows will be dictated by the requirement that every combination of possible rows is included in the matrix. If I use n=4 and k=3, it takes 20 rows to include every combination of rows where each adds up to k=3. Example matrix below:
I'm hoping to write some code that will take arbitrary values of n and k and output such a matrix.
3 0 0 0
2 1 0 0
2 0 1 0
2 0 0 1
1 2 0 0
1 1 1 0
1 1 0 1
1 0 2 0
1 0 1 1
1 0 0 2
0 3 0 0
0 2 1 0
0 2 0 1
0 1 2 0
0 1 1 1
0 1 0 2
0 0 3 0
0 0 2 1
0 0 1 2
0 0 0 3

採用された回答

Matt J
Matt J 2022 年 4 月 21 日
編集済み: Matt J 2022 年 4 月 21 日
See attached.
n=4;k=3;
diophantine(ones(1,n),k,0:k)
ans = 20×4
3 0 0 0 2 1 0 0 1 2 0 0 0 3 0 0 2 0 1 0 1 1 1 0 0 2 1 0 1 0 2 0 0 1 2 0 0 0 3 0

その他の回答 (1 件)

Bruno Luong
Bruno Luong 2022 年 4 月 21 日
A non recursive engine AllVL1 in FEX
>> allVL1(4,3)
ans =
0 0 0 3
0 0 1 2
0 0 2 1
0 0 3 0
0 1 0 2
0 1 1 1
0 1 2 0
0 2 0 1
0 2 1 0
0 3 0 0
1 0 0 2
1 0 1 1
1 0 2 0
1 1 0 1
1 1 1 0
1 2 0 0
2 0 0 1
2 0 1 0
2 1 0 0
3 0 0 0

カテゴリ

Help Center および File ExchangeMathematics についてさらに検索

製品


リリース

R2021a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by