0

Given a pair of equal length arrays . .

void someFunc (const float **inputChannelData)
{
    const float* L = inputChannelData[0];
    const float* R = inputChannelData[1];
    ...

I can see that it is pretty easy to count the number of occurrences of a constant value using std::count . .

std::count(L, L+someIntegerOffset, 0.0f)

... but is there a standard algorithm (or idiom) that counts the element-wise (same index in each array) number of identical values in two arrays?

A Matlab equivalent would look like sum(L==R)

learnvst
  • 15,455
  • 16
  • 74
  • 121

2 Answers2

4

std::count has a predicate form std::count_if, and documentation suggest it gets the argument by lvalue.

So I believe I could hack together a lambda that captures the two arrays, from &arg gets the distance/index, then checks both values. I doubt I would use it that way though instead of old-fashioned for loop.

Balog Pal
  • 16,195
  • 2
  • 23
  • 37
1

You can use zip for this, look here Sequence-zip function for c++11?

And then count_if with predicate, that tuple has only identical elements.

Also, you can simply create array of pairs and do the same.

Something like this for count with pairs.

void someFunc(const float** inputChannelData, size_t offset)
{
   const float* R = inputChannelData[0];
   const float* L = inputChannelData[1];
   std::vector<std::pair<int, int>> pairs;
   std::transform(L, L + offset, R, std::back_inserter(pairs),
   [](float l, float r)
   {
      return std::make_pair(l, r);
   });
   size_t equal_pairs = std::count_if(pairs.begin(), pairs.end(),
   [](const std::pair<float, float>& p)
   {
      return p.first == p.second;
   });
}
Community
  • 1
  • 1
ForEveR
  • 55,233
  • 2
  • 119
  • 133