random number with fixed summation

I randomly generated the matrix f1; where R is 8 and T is 11. In the following we have f1.
The summation of each column should have a fixed value k, for example k=4. I found the "randfixedsum" function that perfectly generates random number with fixed summation. But the elements are not zero and 1. Could you please help me to change f1 to a matrix that the summation of each column is exactly k.
f1 =
1 0 1 0 0 0 0 1 1 0 0
1 0 0 0 0 1 1 0 1 0 0
1 0 0 1 1 1 0 0 0 0 0
1 0 1 1 0 0 1 1 1 1 1
1 1 0 1 0 0 0 1 1 1 0
0 1 1 0 1 0 1 0 1 1 0
0 1 1 0 0 0 0 1 1 0 0
0 1 1 1 1 0 1 0 1 1 0

 採用された回答

Roger Stafford
Roger Stafford 2016 年 11 月 14 日

0 投票

Use the ‘randperm’ function on each column of f1:
f1 = zeros(8,11)
for ix = 1:size(f1,2)
f1(randperm(size(f1,1),k),ix) = 1;
end

1 件のコメント

rezi raf
rezi raf 2016 年 11 月 15 日
thank you so much!

サインインしてコメントする。

その他の回答 (1 件)

Walter Roberson
Walter Roberson 2016 年 11 月 14 日

0 投票

There are multiple ways to handle this, but the easiest way is just to round() the numbers.

カテゴリ

ヘルプ センター および File ExchangeCreating and Concatenating Matrices についてさらに検索

タグ

質問済み:

2016 年 11 月 14 日

コメント済み:

2016 年 11 月 15 日

Community Treasure Hunt

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

Start Hunting!

Translated by