Skip to content

Instantly share code, notes, and snippets.

@marcoslhc
Last active April 6, 2017 01:27
Show Gist options
  • Select an option

  • Save marcoslhc/90779459daa5744a4f5725e4d2402005 to your computer and use it in GitHub Desktop.

Select an option

Save marcoslhc/90779459daa5744a4f5725e4d2402005 to your computer and use it in GitHub Desktop.
Array Reduce Matrix Product
var matrix1 = [
[1, 1, 4, 5],
[0, 3, 3, 2],
[3, 1, 1, 0]
]
var coeficients = [0.3, 0.5, 0.2];
function weirdComputation(matrix) {
var m = matrix;
return function (product, coeficient) {
product.push([
m[0][0] * coeficient + m[0][1] * coeficient + m[0][2] * coeficient + m[0][3] * coeficient,
m[1][0] * coeficient + m[1][1] * coeficient + m[1][2] * coeficient + m[1][3] * coeficient,
m[2][0] * coeficient + m[2][1] * coeficient + m[2][2] * coeficient + m[2][3] * coeficient,
]);
return product;
}
};
coeficients.reduce(weirdComputation(matrix1), []);
//[
// [3.3, 2.4, 1.5],
// [5.5, 4, 2.5],
// [2.2, 1.6, 1]
//]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment