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!웹사이트 선택
번역된 콘텐츠를 보고 지역별 이벤트와 혜택을 살펴보려면 웹사이트를 선택하십시오. 현재 계신 지역에 따라 다음 웹사이트를 권장합니다:
또한 다음 목록에서 웹사이트를 선택하실 수도 있습니다.
사이트 성능 최적화 방법
최고의 사이트 성능을 위해 중국 사이트(중국어 또는 영어)를 선택하십시오. 현재 계신 지역에서는 다른 국가의 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)
