フィルターのクリア

How can I generate a binary matrix with a condition on the rows and columns?

1 回表示 (過去 30 日間)
Ideth Kara
Ideth Kara 2022 年 8 月 18 日
コメント済み: Ideth Kara 2022 年 8 月 19 日
Hello everyone!
i have a matrix (M,N)
i want to fill it with "0" and "1" values under the following condition : sum in each row equal to 1 and the sum of each colomun equal to k
this is an example of the required result , which k=2
1 0 0 0 0
1 0 0 0 0
0 1 0 0 0
0 1 0 0 0
0 0 1 0 0
0 0 1 0 0
0 0 0 1 0
0 0 0 1 0
0 0 0 0 1
0 0 0 0 1
Would you please help me ?

採用された回答

Walter Roberson
Walter Roberson 2022 年 8 月 18 日
M = 10; N = 5;
k = 2;
if M ~= N * k
error('inconsistent sizes, would not be able to create a matrix with the required properties')
end
output = repelem(eye(N), k, 1)
output = 10×5
1 0 0 0 0 1 0 0 0 0 0 1 0 0 0 0 1 0 0 0 0 0 1 0 0 0 0 1 0 0 0 0 0 1 0 0 0 0 1 0 0 0 0 0 1 0 0 0 0 1

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeMatrices and Arrays についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by