确定大数据集中的总和(Determine a sum in a large data set)
比方说我有数据集
data = [2 4 12.3 54.2 0.3 11 5 3];我需要找到数据集中哪些变量总和为特定值,例如:8.在这种情况下,这会导致5 + 3 = 8。
我怎样才能做到这一点?
谢谢
Say for example that I have the data set
data = [2 4 12.3 54.2 0.3 11 5 3];I need to find which of the variables in the data set that sums up to a specific value, for example: 8. In this case this would result in 5 + 3 = 8.
How can I achieve this?
thanks
最满意答案
首先,这类问题并不总是有解决办法,而且可能很难找到。
您可能会发现intlinprog有用:
n = numel(data); sumTo = 8; %// the target sum x = intlinprog(zeros(n,1), 1:n, [], [], data, sumTo, zeros(n,1), ones(n,1)); sel = find(sel);sel应该包含总和等于sumTo的data元素索引。 也就是说, sum(data(sel))==sumTo 。
First of all, there's not always a solution to such problems, and it might be difficult to find.
You may find intlinprog usefull:
n = numel(data); sumTo = 8; %// the target sum x = intlinprog(zeros(n,1), 1:n, [], [], data, sumTo, zeros(n,1), ones(n,1)); sel = find(sel);sel should contain the indices of elements of data which sum equals to sumTo. That is, sum(data(sel))==sumTo.
更多推荐
发布评论