matrices java code example
Example 1: multiply matrices java
public class Matrix {
private double[][] multiply(double[][] matrixOne, double[][] matrixTwo) {
assert matrixOne[0].length == matrixTwo.length: "width of matrix one must be equal to height of matrix two";
double[][] product = new double[matrixOne.length][matrixTwo[0].length];
for(short l = 0; l<matrixOne.length; l++) {
for(short m = 0; m<matrixTwo[0].length; m++) {
product[l][m] = 0;
}
}
for(short i = 0; i<matrixOne.length; i++) {
for(short j = 0; j<matrixTwo[0].length; j++) {
for(short k = 0; k<matrixOne[0].length; k++) {
product[i][j] += matrixOne[i][k] * matrixTwo[k][j];
}
}
}
return product;
}
}
Example 2: java matrix
int [ ] [ ] scores = { { 20, 18, 22, 20, 16 },
{ 18, 20, 18, 21, 20 },
{ 16, 18, 16, 20, 24 },
{ 25, 24, 22, 24, 25 }
};