I have the following js function that once pressed it generates a table.
function calculateMatrixFact() {
var cache = CacheValues();
// split the matrix into arrays one for each user and movie
var user_matrix = createGroups(cache.mu, 4);
// console.log(user_matrix);
var score_matrix = createGroups(cache.ms, 3);
//console.log(score_matrix);
// remove the string user_name and movie_name
for (let i = 0; i < user_matrix.length; i++) {
user_matrix[i].shift();
}
for (let j = 0; j < score_matrix.length; j++) {
score_matrix[j].shift();
}
var dot_matrix = [];
// perform the dot product
for (let j = 0; j < user_matrix.length; j++) {
for (let k = 0; k < score_matrix.length; k++) {
//console.log(user_matrix[j])
//console.log(score_matrix[k])
var dot_product = (math.multiply(user_matrix[j], score_matrix[k])).toFixed(1);
//console.log(dot_product)
dot_matrix.push(dot_product);
}
}
// create the matrix and push back the string (first column of the table)
var dot_prod_matrix = createGroups(dot_matrix, 5);
dot_prod_matrix[0].unshift("Anna");
dot_prod_matrix[1].unshift("Jonny");
dot_prod_matrix[2].unshift("Kimi");
dot_prod_matrix[3].unshift("You");
dot_prod_matrix[4].unshift("Average") //this it the row where I would like to store the avg of each columns.
// from array to HTML table
fetch = document.getElementById('matrix_factorization');
fetch.innerHTML = `<tr>
<th>User</th>
<th>Zombieland</th>
<th>Modern Times</th>
<th>The Grudge</th>
</tr>`;
for (var i = 0; i < dot_prod_matrix.length; i++) {
var newRow = fetch.insertRow(fetch.length);
for (var j = 0; j < dot_prod_matrix[i].length; j++) {
var cell = newRow.insertCell(j);
cell.innerHTML = dot_prod_matrix[i][j];
}
}
}
What I am now trying to achieve is to also calculate the avg of each column and store the result in a 5th row that I have defined. What I have tried to do is
var table = document.getElementById("matrix_factorization");
var avgVal, sumVal = 0;
var rowCount = table.rows.length - 2
console.log(rowCount)
for (var i = 1; i < table.rows.length; i++) {
sumVal = sumVal + parseInt(table.rows[i].cells[1])
}
average = sumVal/rowCount
But when I tried to print the average value I have NaN.