Reputation: 249
I'm clustering a set of points using DBScan algorithm. I have a set of IDs for a set of points and I have a set of clusters, where each cluster has a set of points. I would like to correlate between the clusters and the points ID.
For example I have a set of ids { 1,2,3,4}, Now if I have two cluster and two clusters has two points, then those two points of the first cluster should have the ids 1,2 and for the second one 3,4. Also If I have 4 clusters and each cluster has one point, then the Ids of the points should be 1,2,3, and 4. Furthermore, If I have two clusters but one cluster has 3 points and the other one has one point, then the Ids of the points should be 1,2,3 for the first cluster's points and for the second cluster's point is 4.
I tried to code it, but I'm stopped at calculating the formula to achieve that scenario.
std::vector<int>_IDs;
// for each cluster
for( int j = 0; j<clusters.size();j++ )
{
// for each point in that cluster
for ( int i=0; i < clusters[j].m_Points.size(); i++)
{
// assign its ID from the _IDs array based and save it in Clusters Vector
clusters[j].m_IDs.push_back(_IDs[j+ i*clusters[j].m_Points.size()]);
}
}
Upvotes: 0
Views: 126
Reputation: 10979
I would write something like that:
std::vector<int>_IDs; // those come in from wherever
std::vector<int>::const_iterator id = _IDs.begin();
// for each cluster
for( int j = 0; j<clusters.size(); ++j )
{
// for each point in that cluster
for ( int i=0; i < clusters[j].m_Points.size(); i++)
{
// some external logic should take care that there are enough ids
// for all points in clusters. sanity check it here.
assert(id != _IDs.end());
// assign its ID from the _IDs array based and save it in Clusters Vector
clusters[j].m_IDs.push_back(*id);
++id;
}
}
Upvotes: 1