使用 std::vector 的互补向量

Complementary vector using std::vector

我正在用 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 中的索引列表吗?

我想到了这个解决方案,欢迎发表评论或提出您的建议!

        // 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
            }
        }