Creating random integer row array each element different upper limit and sum of elements add up to a number
現在この質問をフォロー中です
- フォローしているコンテンツ フィードに更新が表示されます。
- コミュニケーション基本設定に応じて電子メールを受け取ることができます。
エラーが発生しました
ページに変更が加えられたため、アクションを完了できません。ページを再度読み込み、更新された状態を確認してください。
古いコメントを表示
I am looking for a intutuve code to generate 1x6 array where each element is random integer (with in specific upper limit) and sums to a given number.
For example a random array where element lower limit [ 0 0 0 0 0 0] and upper limit [ 9 12 25 6 14 7]. The sum of the random integer number adds up to 24, e.g.
[4 3 8 1 3 4 1]
採用された回答
Ameer Hamza
2020 年 9 月 24 日
編集済み: Ameer Hamza
2020 年 9 月 28 日
See this excellent FEX package by John: https://www.mathworks.com/matlabcentral/fileexchange/49795-randfixedlinearcombination. However, It generates fractional numbers. Following code proposes one modification. I don't know how it will change the probability distribution.
lb = [0 0 0 0 0 0];
ub = [9 12 25 6 14 7];
n = numel(lb);
s = 24;
x = round(randFixedLinearCombination(1, s, [1 1 1 1 1 1], lb, ub));
k = sum(x) - s;
if k > 0
idx = find(x >= (lb+1));
idx = idx(randperm(numel(idx), k));
x(idx) = x(idx) - 1;
else
idx = find(x <= (ub-1));
idx = idx(randperm(numel(idx), -k));
x(idx) = x(idx) + 1;
end
8 件のコメント
The logic is very intutive, however the there is no inbuild function named "randFixedLinearCombination()" in Matlab 2020. The randFixedLinearCombination() is user contributed is available in Matlab central and must be placed in the matlab path.
I though a segmented iterative approach shared here
I believe it must have a minus in the second if-branch
idx = randperm(n, -k)
You download randFixedLinearCombination from the File Exchange. It is part of MATLAB, in the sense that you need do nothing more than download it. NO cost. Ameer gave you the link.
John: Thanks for the comment.
Bruno: Thanks for pointing out. You are correct. It should be -k.
Biswanath: As John mentioned, download this package from the link and place it in MATLAB's path: https://www.mathworks.com/help/matlab/matlab_env/add-remove-or-reorder-folders-on-the-search-path.html
Bruno Luong
2020 年 9 月 27 日
編集済み: Bruno Luong
2020 年 9 月 27 日
The rounding method is still flawed. The results might get out of bound once they are added by +/-1. You have to select the subset more careful than randperm.
Yes, thanks for pointing out the issue. I have updated the code. I am not sure if there still are some cases where it will fail to meet the constraint.
Bruno Luong
2020 年 9 月 27 日
編集済み: Bruno Luong
2020 年 9 月 27 日
I think you can do better if you replace the first filtering with <= >= tests rather than strict inequality.
It still not perfect though.
Yes, I guess this will fail when the bounds are very tight in accordance with the sum constraints. The point about inequality is also valid. For the given bounds, the method does not seem to fail.
その他の回答 (0 件)
カテゴリ
ヘルプ センター および File Exchange で Descriptive Statistics についてさらに検索
参考
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!Web サイトの選択
Web サイトを選択すると、翻訳されたコンテンツにアクセスし、地域のイベントやサービスを確認できます。現在の位置情報に基づき、次のサイトの選択を推奨します:
また、以下のリストから Web サイトを選択することもできます。
最適なサイトパフォーマンスの取得方法
中国のサイト (中国語または英語) を選択することで、最適なサイトパフォーマンスが得られます。その他の国の MathWorks のサイトは、お客様の地域からのアクセスが最適化されていません。
南北アメリカ
- América Latina (Español)
- Canada (English)
- United States (English)
ヨーロッパ
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
