Find all subset index of an array whose sums equal or nearest to a given target
1 次查看(过去 30 天)
显示 更早的评论
- target = 700
- array = [200 250 340 100 500 360]
3 个评论
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
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),:);
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Operators and Elementary Operations 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!