Azzera filtri
Azzera filtri

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

1 visualizzazione (ultimi 30 giorni)
  • target = 700
  • array = [200 250 340 100 500 360]
  3 Commenti
Walter Roberson
Walter Roberson il 26 Set 2015
So 200,200,200,100 would be accepted as long as no other permutation of those values was emitted?

Accedi per commentare.

Risposte (1)

Andrei Bobrov
Andrei Bobrov il 26 Set 2015
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),:);

Categorie

Scopri di più su Operators and Elementary Operations in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by