我需要帮助设置一个C++模块来迭代通过下面的2D图,以便通过检测在每个步骤中看到的最大阈值差异来累积每个聚类的平均值。例如,当阈值达到点之间的25时,当前集群中的所有点应该取平均值,并分配一个参数以打印输出。
我希望这是有意义的,如果需要的话,请提出问题进行澄清。

我正在寻找一种在C++中实现这一点的简单方法。
发布于 2020-06-04 14:11:17
下面是一个示例代码,它完成了您的请求:
#include <iostream>
#include <vector>
using namespace std;
int main()
{
std::vector<float> samples = {1.f,2.f,3.f,30.f,31.f,32.f};
std::vector<float> clustersAvg; // the averages per cluster
float clusterAcc =0.f; // current cluster accumulated values
int clusterSize = 0; // current cluster size
float lastSample = samples[0];
for (const auto&sample:samples)
{
if (abs(sample - lastSample) <= 25) // check if threshold is reached or not
{
clusterAcc += sample;
clusterSize ++;
}
else
{
// threshold reached, let's average what we accumulated so far
clustersAvg.push_back(clusterAcc/clusterSize);
clusterAcc = sample;
clusterSize = 1;
}
lastSample = sample; // keep track of sample for next threshold checking
}
clustersAvg.push_back(clusterAcc/clusterSize); // last accumulation
// print results
for (const auto & avg:clustersAvg)
{
cout << "avg: " << avg << endl;
}
}发布于 2020-06-04 14:19:19
有数百万种可能的解决方案。
这是下一个。
所有解的算法都将是相似的。检查样本数据中是否存在大于阈值的“跳跃”。我们通过对数据进行迭代,然后将当前元素与下一个元素进行比较来发现这一点。我们减去这些值,然后检查sbsolute值是否大于treshold。然后我们展示结果。
如果不是,那么我们将构建一个运行总和,并为以后的平均计算计算样本。
#include <iostream>
#include <vector>
int main() {
// Test Data. Here in a vector, can be any other container
std::vector sample{
1,2,1,3,2,1,3,4,2,3,5,2,3,
30,29,32,28,33,32,34,31,35,34,31,
82,81,83,86,84,86,87,85,86,82,
111,112,113,112,114,115,113,112,113 };
// Some threshold example
constexpr int threshold{ 20 };
// Here we count the elements in one group of somehow similare sample values
int groupCounter{};
// And this is the sum of one group of somehow similar sample values
double groupSum{};
// Check all sample data
for (size_t i{}; i < sample.size(); ++i) {
// Accumulate the data
groupSum += sample[i];
++groupCounter;
// If we have a "jump" or are at the last value
if ((i == sample.size() - 1) || (std::abs(sample[i + 1] - sample[i]) > threshold)) {
// Prevent division by 0
if (groupCounter > 0) {
// Show resulting avaerage of this group
std::cout << "Average value:\t" << groupSum / groupCounter << '\n';
}
else {
std::cerr << "\nError: No further data\n";
}
// Reset values to do calculations for the next run
groupCounter = 0;
groupSum = 0.0;
}
}
return 0;
}使用C++算法,您将获得:
#include <iostream>
#include <vector>
#include <algorithm>
#include <numeric>
#include <iterator>
// Abbreviations for easier writing
using Container = std::vector<double>;
using Iter = Container::iterator;
int main() {
// The test data
Container sample{ 1,2,1,3,2,1,3, 30,29,32,28,33,32, 82,81,83,86,84,86, 112,114,115,113,112,113};
// Some threshold
constexpr double threshold = 20.0;
// Simple For loop. Go over container from start to end
for (Iter result{}, start = {sample.begin()}; start != sample.end(); start = result)
{
// Search the "jump" in data
result = std::adjacent_find(start, sample.end(),
[threshold](const double& d1, const double& d2) { return std::abs(d1 - d2) > threshold; });
// Set result to the next element after the found value
result = (result != sample.end()) ? std::next(result) : sample.end();
// Show result of calculation to user
std::cout << std::accumulate(start, result, 0.0) / std::distance(start, result) << '\n';
}
return 0;
}https://stackoverflow.com/questions/62187406
复制相似问题