确定大数据集中的总和(Determine a sum in a large data set)

编程入门 行业动态 更新时间:2024-10-25 22:29:18
确定大数据集中的总和(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.

更多推荐

本文发布于:2023-07-25 19:45:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1265241.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:总和   数据   Determine   sum   data

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!