...

/

Calculate the Error Sum of Two Vectors

Calculate the Error Sum of Two Vectors

Learn to calculate the error sum of two vectors.

We'll cover the following...

Given two similar vectors that differ only by quantization or resolution, we can use the inner_product() algorithm to calculate an error sum, defined as:

Where e is the error sum, the sum of the square of the difference between a series of points in two vectors. We can use the inner_product() algorithm, from the <numeric> header, to calculate the error sum between two vectors.

How to do it

In this recipe we define two vectors, each with a sine wave. One vector has values of type double and the other has type int. This gives us vectors that differ in quantization, because the int type cannot represent fractional values. We then use inner_ product() to calculate the error sum between the two vectors:

  • In our main() function we define our vectors and a handy index variable:

int main() {
constexpr size_t vlen{ 100 };
vector<double> ds(vlen);
vector<int> is(vlen);
size_t index{};
...

ds is the vector of double sine waves and ...