使用std :: vector的互补向量

时间:2015-03-22 15:34:12

标签: c++ matlab dll std

我在C ++中编写了一个现有的Matlab库。 Matlab中有一个波浪号运算符,~vec是带有1的二进制向量,其中vec为零,其他地方为0。

更准确地说,我在Matlab中有这些代码行

        allDepthIdx = [1:nVec]'; 
        goodIdx = allDepthIdx(~offVec);
        goodValues = vec(~offVec);

我正在寻找一种有效的方法来查找索引goodIdx = allDepthIdx(~offVec);。我有办法使用std::vector查找1..nVec而不是offVec中的索引列表吗?

1 个答案:

答案 0 :(得分:0)

我提出了这个解决方案,随意发表评论,或提出你的建议!

        // First, I sort offVec
        std::sort(offVec.begin(), offVec.end());
        int k(0), idx(-1);

        std::vector<real32_T> goodDepthIdx;
        std::vector<real32_T> goodVal;

        // For j in 1..nVec, I check if j is in offVec
        for (int j = 0; j < nVec; j++)
        {
            k = 0;
            idx = offVec.at(k);

            // I go through offVec as long as element is strictly less than j
            while (idx < j)
            {
                idx = offVec.at(k++);
            }

            if (idx != j) // idx not in offElemsVec
            {
                goodDepthIdx.push_back(j); // Build vector with indices 
                                           // in 1..nVec not in offVec
                goodVal.push_back(vec.at(j)); // Build vector with values
                                           // at these indices
            }
        }
相关问题