フィルターのクリア

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

1 回表示 (過去 30 日間)
Stalin Samuel
Stalin Samuel 2015 年 9 月 26 日
回答済み: Andrei Bobrov 2015 年 9 月 26 日
  • target = 700
  • array = [200 250 340 100 500 360]
  3 件のコメント
Stalin Samuel
Stalin Samuel 2015 年 9 月 26 日
yes sir.but i need the elements without Repetition
Walter Roberson
Walter Roberson 2015 年 9 月 26 日
So 200,200,200,100 would be accepted as long as no other permutation of those values was emitted?

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

回答 (1 件)

Andrei Bobrov
Andrei Bobrov 2015 年 9 月 26 日
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),:);

カテゴリ

Help Center および File ExchangeOperators and Elementary Operations についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by