Permutations of integers with sum restrictions
조회 수: 1 (최근 30일)
이전 댓글 표시
Hi Matlab gurus,
I would like to find all possible permutations of [-1 0 1] for a varying with varying length with a restriction on the overall sum across on permutation.
npermutek([-1 0 1],n) already allows me to find all possible permutations with varying length n. Is there a way to add the additional sum restriction across all permutations (e.g., n = 3 but output should only include permutations with a sum of 2)?
Since the length goes up to n=40 I running npermutek([-1 0 1],n) would be unfeasible without additional restrictions.
Any help on this issues would be greatly appreciated.
Thanks, Christoph
댓글 수: 0
채택된 답변
Suraj Mankulangara
2018년 3월 13일
Hi Christoph
There are a couple of ways that you can do this:
1. Edit the source code of the "npermutek" function. Where the permutation row is calculated, find the sum of that row. Only add those rows to the matrix that meet the sum restriction
2. Alternatively, you can do:
permutedMatrix = npermutek([2 4 5], 2); % Returns permutations of length 2
permutedMatrix(sum(permutedMatrix, 2) <= maxSum, :) % Returns only those permutations whose sum is less than or equal to maxSum
The second method will allow you to continue using npermutek without making any modifications, if that is of interest to you.
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Special Functions에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!