Find all subset index of an array whose sums equal or nearest to a given target

  • target = 700
  • array = [200 250 340 100 500 360]

댓글 수: 3

To confirm, one of the outputs would be 200, 200, 200, 100? Because subset implies sets, and every value in a set is there an indefinite number of times -- set {200, 100} is the same as set {200, 200, 200, 100} so from any set you can "withdraw" any element multiple times.
Likewise, if we find any duplicates in the array, they should be treated the same as if they had only appeared once, correct?
yes sir.but i need the elements without Repetition
So 200,200,200,100 would be accepted as long as no other permutation of those values was emitted?

댓글을 달려면 로그인하십시오.

답변 (1개)

a = dec2bin((0:bin2dec(sprintf('%d',ones(1,numel(array)))))')-'0';
k = bsxfun(@times,a,array);
d = abs(sum(k,2) - 700);
out = k(d == min(d),:);

카테고리

도움말 센터File Exchange에서 Entering Commands에 대해 자세히 알아보기

태그

질문:

2015년 9월 26일

답변:

2015년 9월 26일

Community Treasure Hunt

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

Start Hunting!

Translated by