将向量推回到不同的位置

时间:2013-09-18 10:50:50

标签: c++ vector stl

我对此有一点认识。

基本上我有一个2D矢量包含大小为1024x1024的块,我计算这些块的总能量。如果块因此在特定阈值内,则它们可以存储在另一个2D矢量内。问题是,我需要捕获(推回)多个块,直到阈值变为负值。这是一个例子:

blocks[0] = 0.124 <- This is not pushed back
blocks[1] = 0.123 <- This is not pushed back
blocks[2] = 0.456 <- This is not pushed back 
blocks[3] = 1.23 <- This is pushed back to vector[0]
blocks[4] = 2.45 <- This is pushed back to vector[0]
blocks[5] = 7.23 <- This is pushed back to vector[0]
blocks[6] = 8.12 <- This is pushed back to vector[0]
blocks[7] = 0.12 <- This is not pushed back
blocks[8] = 0.124 <- This is not pushed back
blocks[9] = 0.125 <- This is not pushed back
blocks[10] = 8.123 <- This is pushed back to vector[1]
blocks[11] = 8.123 <- This is pushed back to vector[1]
blocks[12] = 8.123 <- This is pushed back to vector[1]
blocks[13] = 0.12 <- This is not pushed back

所以基本上,当块的阈值为真时,然后将块插入位置[i]的2D矢量中,直到块变为负值。当该值再次变为真时,块被推回到位置[i + 1]

到目前为止我的想法:

如果我有两个变量来存储需要推回的块数,另一个变量存储在向量被推回的位置...... I.e。 currentpos[1]因此下一个位置为currentpos[2]

您可以提供任何帮助,伪代码会更好。

编辑:

这是阈值函数:

bool threshold (vector<double> val)
{

//short threshold = 10000;
float sum = 0.0;
for(unsigned i=0; (i < val.size()); i++)
{
    sum += (val[i]*val[i]);
}
return (sum > 0.082);

}

然后我有以下内容:

std::vector<vector<double> > blocks = splitVector(1024, 1024); // this is fine, it works!
// then
std::vector<vector<double> > clusters;
for(unsigned i=0; (i < blocks.size()); i++)
{
   if(threshold(blocks[i])) { 
   // true
   clusters[i].push_back(d[i]);
   }else{
    // false do not do anything
   }
}

但我遇到的问题是,“群集”只会包含已返回的“正”块数量。那么blocks.size() = 745 (size)哪里clusters.size() = 6这更有意义呢?

编辑 - 此示例有效:

#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

bool energy(const std::vector<double> &vals)
{
float sum = 0.0;
for(unsigned i=0; (i < vals.size()); i++)
{
    sum += (vals[i]*vals[i]);
}
//cout << sum << endl;
return (sum >= 5);

}

int main(int argc, char *argv[]) {

std::vector<vector<double> > vals {

    {0, 0, 0, 0, 0}, // This has an energy of "0" -> does not count
    {1, 1, 1, 1, 1}, // This has an energy of "5" -> push_back to vector[0]
    {1, 1, 1, 1, 1}, // This has an energy of "5" -> push_back to vector[0]
    {1, 1, 1, 1, 1}, // This has an energy of "5" -> push_back to vector[0]
    {1, 1, 1, 1, 1}, //This has an energy of "5" -> push_back to vector[0]
    {0, 0, 0, 0, 0}, // This has an energy of "0" -> does not count && start a new vector
    {1, 2, 3, 4, 5}, // This has an energy of "55" -> push_back to vector[1]
    {1, 2, 3, 4, 5}, // This has an energy of "55" -> push_back to vector[1]
    {1, 2, 3, 4, 5}, // This has an energy of "55" -> push_back to vector[1]
    {1, 2, 3, 4, 5} // This has an energy of "55" -> push_back to vector[1]
};
std::vector<vector<double> > clusters; 

//std::for_each(vals.begin(), vals.end(), energy);

int j = 0;

for(unsigned i=0; (i < vals.size()); i++)
{
    if(energy(vals[i]))
    {
        clusters.resize(j + 1);

        clusters[j] = vals[i];
    }else if(!energy(vals[i]) && energy(vals[i+1]))
    {
        j++;
    }
}

 }

但是,不是“连接”值,而是覆盖它们,因此clusters[0]只包含{1, 1, 1, 1, 1}运算符的值{1, 1, 1, 1, 1} + {1, 1, 1, 1, 1} + {1, 1, 1, 1, 1}而不是+ ,我的意思是连接而不是元素的总和。

因此如何连接值?

2 个答案:

答案 0 :(得分:1)

您可以使用以下psuedocode:

bool flag = false;
int j = 0;
for i in blocks
{
 if(blocks[i] > 0)
 {
  vector[j].push_back(blocks[i]);
 flag = true;
 }
 else
  if(flag)
  {
   j++;
   flag = flase;
  }
}

答案 1 :(得分:0)

如果我理解你想要做的正确,我会做一些简单的事情:

bool wasNeg = true;
for(blocks) {
    if(isNegative()) {
      wasNeg = true;
    } else {
        if(wasNeg) {
            outVec.push_back(vector());
        }
        outVec.back().push_back(value);
        wasNeg = false;
    }
 }

这总是插入输出向量中的最新向量。只有当两者之间存在负值时,才会通过推送新的向量来分隔块。