Home > OS >  How to calculate the sum and product of each element of multiple arrays?
How to calculate the sum and product of each element of multiple arrays?

Time:10-19

Please bear with me this is difficult to explain. I will first explain how to do it successfully with only one set of data.

First, say I have an array like so yValuesMinusMean = [-5, -4, -1, 10]

I have another array like so xValuesMinusMean = [ 2.75,3.75,6.75,5.75 ]

Both of the above arrays can have numerous values. However, the length of both arrays is the same. So if the first one has 4, then the second one will definitely have 4.

I want to calculate the sum and product of the arrays. This is what I mean:

var sumOfXTimesYValues = this.calculateProductAndSum(yValuesMinusMean, xValuesMinusMean);

    calculateProductAndSum(yValuesMinusMean = [], xValuesMinusMean = []) {

        let total = 0;

        for(let i = 0; i < yValuesMinusMean.length; i  ) {

            let product = (yValuesMinusMean[i] * xValuesMinusMean[i]);
            
            total  = product;
        };
        return total;
    },

The result of this: console.log('sumOfXTimesYValues', sumOfXTimesYValues); is

17

LOGIC : (-5 * 2.75) (-4 * 3.75) (-1 * 6.75) (10 * 5.25) = 17

So far, everything works. However, I want to make it so that instead of xValuesMinusMean being a single array with multiple numerical values, it will be a single array containing multiple arrays, with each array having the same number of elements as in yValuesMinusMean. Like so:

xValuesMinusMean = [ [ 2.75,3.75,6.75,5.75 ], [-2,-1,2,1]. .... ]

END GOAL: sumOfXTimesYValues = [17, 22, ...]

Logic for the second array item: (-5 * -2) (-4 * -1) (-1 * 2) (10 * 1) = 22

Essentially, you're multiplying each value in each array in xValuesMinusMean with a value in yValuesMinusMean in the same order. So, -5 is the 0th item in the yValuesMinusMean array, and -2 is the 0th item in the array within xValuesMinusMean. So -5 * -2.

My next steps would be to do something like this:

    xValuesMinusMean.forEach(element => {
        for(let i = 0; i < xValuesMinusMean.length; i  ) {
            let product = (newCategoryArray[i] * xValuesMinusMean[i]);
            total  = product;
        };
    });

However, it yields the following: sumOfXTimesYValues = 352, which isn't correct. How would I be able to achieve the end goal?

END GOAL: sumOfXTimesYValues = [17, 22, ...]

CodePudding user response:

Create a generic function for computing a scalar product (you've already got it):

function scalarProduct(a, b) {
    let res = 0;
    for(let i = 0; i < a.length; i  ) {
        res  = a[i] * b[i];
    }
    return res;
}

and then map it over your matrix (array of vectors):

result = xValuesMinusMean.map(vec => scalarProduct(vec, yValuesMinusMean))

CodePudding user response:

You can have one reduce function where you will take product of arrays and store it in accumulator.

const val =[ [ 2.75,3.75,6.75,5.25 ], [-2,-1,2,1]];
const yvalues = [-5, -4, -1, 10];
console.log(val.map(o=>o.reduce((a,e,i)=>a =e*yvalues[i],0)));

CodePudding user response:

Looks like your calculation is not correct first set of arrays will also return 22.

Live Demo :

const yValuesMinusMean = [-5, -4, -1, 10];

const xValuesMinusMean = [[2.75, 3.75, 6.75, 5.75], [-2, -1, 2, 1]];

const finalArr = [];

xValuesMinusMean.forEach(arr => {
  let cal = 0;
  arr.forEach((item, index) => {
    cal  = item * yValuesMinusMean[index]
  });
  finalArr.push(cal);
});

console.log(finalArr); // [22, 22]

  • Related