将矢量推回不同位置

Pushing vectors back at different positions

本文关键字:位置      更新时间:2023-10-16

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

基本上,我有一个包含大小为 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向量中,直到块变为负数。当值再次变为 true 时,块被推回位置 [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。

因此,如何连接这些值?

您可以使用以下伪代码:

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;
  }
}

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

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

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