Reputation: 26333
I am writting in C++ an existing Matlab library. There is in Matlab a tilde operator, and ~vec
is the binary vector with 1s where vec
is zero and 0s elsewhere.
More precisely, I have these lines of code in Matlab
allDepthIdx = [1:nVec]';
goodIdx = allDepthIdx(~offVec);
goodValues = vec(~offVec);
I am looking for an efficient way to find the indices goodIdx = allDepthIdx(~offVec);
. I there a way, using std::vector
, to find the list of indices which are in 1..nVec
and not in offVec
?
Upvotes: 0
Views: 347
Reputation: 26333
I came up with this solution, feel free to comment, or to propose yours !
// 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
}
}
Upvotes: 0