I'm trying to multiply 2 arrays in Java. I know how to multiply 2 arrays with each other, but I'm struggling to translate that into Java using 2D arrays (It's my first time using 2D arrays).
I've tried creating a new column variable for the first array, another one for the second one, and another one for the result, but it's not working.
This is what I've written so far
public double[][] producto(double[][] m1, double[][] m2){
double[][] r=new double[m1.length][m1[0].length];
for(int fila=0, col1 = 0, col=0;fila<m1.length && col1<m1[fila].length;fila++,col1++,col++){
for(int col2=0; col2<m1[fila].length;col2++){
r[fila][col]+=m1[fila][col1]*m2[fila][col2];
}
}
return r;
}
When I use {{1,1,1},{1,1,1},{1,1,1}}
and {{2,2,2},{2,2,2},{2,2,2}}
as inputs I get tze actual output {{6,0,0},{0,6,0},{0,0,6}}
while I would expect it to be {{6,6,6},{6,6,6},{6,6,6}}
.