1

我在理解这一点时遇到了一点问题。

基本上我有一个包含大小为 1024x1024 的块的 2D 向量,我计算这些块的总能量。因此,如果块在某个阈值内,则它们可以存储在另一个 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]

到目前为止我的想法:

如果我有两个变量存储需要推回多少块,另一个变量存储向量被推回的位置。也就是说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}通过+运算符,我的意思是连接而不是元素的 SUM。

因此,如何连接这些值?

4

2 回答 2

1

您可以使用以下伪代码:

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;
  }
}
于 2013-09-18T11:09:20.760 回答
0

如果我理解您想要正确执行的操作,我会做一些简单的事情,例如:

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

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

于 2013-09-18T11:05:19.387 回答