简体   繁体   English

在c ++中组合两个不同向量的比率

[英]Combine the ratios of two different vectors in c++

I don't know how to explain it so it if this question exists, just point me to the right topic.我不知道如何解释,所以如果这个问题存在,请指出正确的主题。 I have searched the forum but couldn't find any answer (maybe there is a keyword in mathematics or statistics for this that I am missing).我已经搜索了论坛,但找不到任何答案(也许我遗漏了数学或统计学中的关键字)。

How do I produce a vector with combined weights from two vectors?如何从两个向量生成具有组合权重的向量?

For example given two vectors:例如给定两个向量:

vector_1 = {3, 3, 4}
vector_2 = {5, 5}

We calculate their weights which is the element divided by the sum of the elements in the vector.我们计算它们的权重,即元素除以向量中元素的总和。

weights_1 = {0.3, 0.3, 0.4}
weights_2 = {0.5, 0.5}

These are then combined to produce this vector.然后将它们组合以产生此向量。 The combined weights is the combined ratios of the two vectors.组合权重是两个向量的组合比率。

combined_weights = {0.3, 0.2, 0.1, 0.4}

is there a function that can calculate the combined weights?有没有可以计算组合权重的函数?

combined_weights = calculate(weights_1, weights_2)

The process is:过程是:

Step 1: combined_weights = {0.3} 

0.3 is the first element of weights_1. 0.3 是 weights_1 的第一个元素。

Step 2: combined_weights = {0.3, 0.2, 0.1} 

The sum of 0.2 and 0.1 is the the second element of weights_1. 0.2 和 0.1 之和是 weights_1 的第二个元素。 The sum of the combined_weights vector is equal to the first element of weights_2. combine_weights 向量的总和等于 weights_2 的第一个元素。

Step 3: combined_weights = {0.3, 0.2, 0.1, 0.4} 

From the combined_weights vector we can get both weights_1 and weights_2, ie从combined_weights向量我们可以得到weights_1和weights_2,即

    weights_1 = {0.3, 0.2 + 0.1, 0.4}
    weights_2 = {0.3 + 0.2, 0.1 + 0.4}

My goal is to make vector_1 and vector_2 have similar size.我的目标是使 vector_1 和 vector_2 具有相似的大小。

new_vector_1 = {3, 2, 1, 4}
new_vector_2 = {3, 2, 1, 4}

在此处输入图片说明

More generally,更普遍,

在此处输入图片说明

Rather than dividing each weight by it's total, you can find the LCM of your totals, and multiply, which keeps you in integer arithmetic您可以找到总数的LCM ,然后乘以,而不是将每个重量除以它的总数

int total1 = std::accumulate(weights1.begin(), weights1.end(), 0);
int total2 = std::accumulate(weights2.begin(), weights2.end(), 0);
int lcm = std::lcm(total1, total2);

We want to do destructive things below, so we may as well do that to adjusted values我们想在下面做破坏性的事情,所以我们也可以对调整后的值这样做

std::deque<int> working1;
std::transform(weights1.begin(), weights1.end(), std::back_inserter(working1), [=](int w){ return w * lcm / total1; });

std::deque<int> working2;
std::transform(weights2.begin(), weights2.end(), std::back_inserter(working2), [=](int w){ return w * lcm / total2; });

Comparing the front elements, you pop the smaller, add it to the output (unless zero), and decrement the larger by that value.比较前面的元素,弹出较小的元素,将其添加到输出(除非为零),然后将较大的元素递减该值。 Repeat that process until both copies are empty重复这个过程直到两个副本都是空的

std::vector<int> combined;

while (!working1.empty() && !working2.empty())
{
    int & top1 = working1.front();
    int & top2 = working2.front();
    if (top1 < top2)
    {
        if (top1 > 0) { combined.push_back(top1) }
        top2 -= top1;
        working1.pop_front();
    }
    else
    {
        if (top2 > 0) { combined.push_back(top2) }
        top1 -= top2;
        working2.pop_front();
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM