将矢量推回不同位置
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;
}
}
这始终插入输出向量中的最新向量。仅当两者之间存在负值时,才通过推送新向量来分隔块。
相关文章:
- 将值指定给向量(2D)的向量中的某个位置
- 使用Unreal C++获取VR耳机的世界位置/方向
- 写入位置0x0000000C时发生访问冲突
- 如何将两个不同矢量的同一位置的两个元素组合在一起
- GL_SHADERSTORAGE_BUFFER位置是否与其他着色器位置冲突
- 如何使用cudaMallocManaged在指针位置初始化对象?(C++)
- 无法获取webot::PositionSensor对象中位置传感器的值
- 非常量变量只读位置的赋值
- 如何定义更改car类中car位置的方法
- 使用迭代器时如何访问对象在向量中的位置?
- 更改.cpp程序的输入文件中数据的位置会意外更改输出
- 求最大元素位置的分治算法
- 你好。。。id_public变量不应该给出结果为 81 和 86 吗?为什么它为两个派生类占用不同的内存位置?
- 为什么我的点没有在 OpenGL 中绘制鼠标所在的位置?
- 如何将元素添加到向量的字符串位置
- 如何选择在 csv 文件中输出的位置
- 无法打开 fstream C++文件,即使它与 .cpp 位于同一位置
- 如何在特定位置追加 json 文件
- 我的目标是编写一个程序来计算和存储字符串在字符数组中出现的位置
- OpenGL - 在 NDC 中计算位置适用于着色器,但不适用于'regular'程序