找出向量中所有值之间的相似距离,并将其子集化



Given是一个具有双值的向量。我想知道这个向量的任何元素之间的哪些距离彼此之间的距离相似。在最好的情况下,结果是原始值的子集的向量,其中子集应该至少有n个成员。

//given
vector<double> values = {1,2,3,4,8,10,12}; //with simple values as example
//some algorithm
//desired result as:
vector<vector<double> > subset;
//in case of above example I would expect some result like:
//subset[0] = {1,2,3,4}; //distance 1
//subset[1] = {8,10,12}; //distance 2
//subset[2] = {4,8,12}; // distance 4
//subset[3] = {2,4};    //also distance 2 but not connected with subset[1]
//subset[4] = {1,3};    //also distance 2 but not connected with subset[1] or subset[3]
//many others if n is just 2. If n is 3 (normally the minimum) these small subsets should be excluded.

这个例子被简化为整数的距离可以被迭代并测试向量,而双精度或浮点则不是这样。

到目前为止我的想法

我想到了一些计算距离并将其存储在向量中的方法。创建一个差距离矩阵,并对该矩阵进行阈值处理,以获得相似距离的一些容差。

//Calculate distances: result is a vector
vector<double> distances;
for (int i = 0; i < values.size(); i++)
    for (int j = 0; j < values.size(); j++)
    {
        if (i >= j)
            continue;
        distances.push_back(abs(values[i] - values[j]));
    }
//Calculate difference of these distances: result is a matrix
Mat DiffDistances = Mat::zero(Size(distances.size(), distances.size()), CV_32FC1);
for (int i = 0; i < distances.size(); i++)
    for (int j = 0; j < distances.size(); j++)
    {
        if (i >= j)
            continue;
        DiffDistances.at<float>(i,j) = abs(distances[i], distances[j]);
    }
//threshold this matrix with some tolerance in difference distances
threshold(DiffDistances, DiffDistances, maxDistTol, 255, CV_THRESH_BINARY_INV);
//get points with similar distances
vector<Points> DiffDistancePoints;
findNonZero(DiffDistances, DiffDistancePoints);

在这一点上,我陷入了寻找与我的相似距离相对应的原始值的困境。应该可以找到它们,但追溯指数似乎非常复杂,我想知道是否有更简单的方法来解决这个问题。

这里有一个解决方案,只要没有分支意味着没有比2*threshold更接近的值,它就可以工作。这是有效的相邻区域,因为如果我正确理解@Phann,相邻键的差异应该小于阈值。

这个解决方案绝对不是最快的,也不是最好的解决方案。但你可以把它作为一个起点:

#include <iostream>
#include <vector>
#include <algorithm>
int main(){
    std::vector< double > values = {1,2,3,4,8,10,12};
    const unsigned int nValues = values.size();
    std::vector< std::vector< double > > distanceMatrix(nValues - 1);
    // The distanceMatrix has a triangular shape
    // First vector contains all distances to value zero
    // Second row all distances to value one for larger values
    // nth row all distances to value n-1 except those already covered
    std::vector< std::vector< double > > similarDistanceSubsets;
    double threshold = 0.05;
    std::sort(values.begin(), values.end());
    for (unsigned int i = 0; i < nValues-1; ++i) {
        distanceMatrix.at(i).resize(nValues-i-1);
        for (unsigned j = i+1; j < nValues; ++j){
            distanceMatrix.at(i).at(j-i-1) = values.at(j) - values.at(i);
        }
    }
    for (unsigned int i = 0; i < nValues-1; ++i) {
        for (unsigned int j = i+1; j < nValues; ++j) {
            std::vector< double > thisSubset;
            double thisDist = distanceMatrix.at(i).at(j-i-1);
            // This distance already belongs to another cluster
            if (thisDist < 0) continue;
            double minDist  = thisDist - threshold;
            double maxDist  = thisDist + threshold;
            thisSubset.push_back(values.at(i));
            thisSubset.push_back(values.at(j));
            //Indicate that this is already clustered
            distanceMatrix.at(i).at(j-i-1) = -1;
            unsigned int lastIndex = j;
            for (unsigned int k = j+1; k < nValues; ++k) {
                thisDist = distanceMatrix.at(lastIndex).at(k-lastIndex-1);
                // This distance already belongs to another cluster
                if (thisDist < 0) continue;
                // Check if you found a new valid pair
                if ((thisDist > minDist) && (thisDist < maxDist)){
                    // Update the valid distance interval
                    minDist = thisDist - threshold;
                    minDist = thisDist - threshold;
                    // Add the newly found point
                    thisSubset.push_back(values.at(k));
                    // Indicate that this is already clustered
                    distanceMatrix.at(lastIndex).at(k-lastIndex-1) = -1;
                    // Continue the search from here 
                    lastIndex = k;
                }
            }
            if (thisSubset.size() > 2) {
                similarDistanceSubsets.push_back(thisSubset);
            }
        }
    }
    for (unsigned int i = 0; i < similarDistanceSubsets.size(); ++i) {
        for (unsigned int j = 0; j < similarDistanceSubsets.at(i).size(); ++j) {
            std::cout << similarDistanceSubsets.at(i).at(j);
            if (j != similarDistanceSubsets.at(i).size()-1) {
                std::cout << " ";
            }
            else {
                std::cout << std::endl;
            }
        }
    }
}

这个想法是预先计算距离,然后从最小和较大的邻居开始寻找每一对粒子,如果上面有另一对有效的粒子。如果是这样,这些粒子都被收集在一个子集中,并添加到子集向量中。对于每个新值,必须更新有效的相邻区域,以确保相邻距离的差异小于阈值。然后,程序继续使用下一个最小值及其较大的邻居,依此类推。

这里有一个与您的算法略有不同的算法,即向量的长度为nO(n^3),效率不是很高。

它基于这样一个前提,即您希望拥有大小至少为2的子集。所以你可以考虑向量的所有两个元素子集,然后找到所有其他匹配的元素。

所以给定一个函数

std::vector<int> findSubset(std::vector<int> v, int baseValue, int distance) {
  // Find the subset of all elements in v that differ by a multiple of 
  // distance from the base value
}

你可以做

std::vector<std::vector<int>> findSubsets(std::vector<int> v) {
  for(int i = 0; i < v.size(); i++) {
    for(int j = i + 1; j < v.size(); j++) {
      subsets.push_back(findSubset(v, v[i], abs(v[i] - v[j])));
    }
  }
  return subsets;
}

唯一剩下的问题是跟踪重复项,也许您可以为已经找到的所有子集保留一个(baseValue % distancedistance)对的哈希列表。

相关内容

  • 没有找到相关文章

最新更新