java - multiply a 2d array by a 1d array -


i've initialized 1d , 2d array , want able perform matrix multiplication on them. however, i'm not quite getting proper answer. think i've mixed loop try ensure multiply correct values, can't quite hang of it.

edit: i've fixed it, misunderstanding length method of 2d array returned (thought returned columns , not rows). below code corrected code. everyone.

public static double[] getoutputarray(double[] array1d, double[][] array2d) {     int onedlength = array1d.length;     int twodlength = array2d[0].length;     double[] newarray = new double[array2d[0].length]; // create array contain result of array multiplication       (int = 0; < twodlength; i++) { // use nested loops multiply 2 arrays         double c = 0;         (int j = 0; j < onedlength; j++) {             double l = array1d[j];             double m = array2d[j][i];             c += l * m; // sum products of each set of elements         }         newarray[i] = c;     }     return newarray; // pass newarray main method } // end of getoutputarray method 

there problems, first of all, should decide how vectors represented, multiplying left or right.

for maths: vector 1xn times matrix nxm result in 1xm, while matrix mxn times nx1 result in mx1.

i think following work you:

public static double[] getoutputarray(double[] array1d, double[][] array2d) {   int onedlength = array1d.length;   int twodlength = array2d.length;   double[] newarray = new double[twodlength]; // create array contain result of array multiplication   assert twodlength >0 && array2d[0].length == onedlength;   (int = 0; < twodlength; i++) { // use nested loops multiply 2 arrays       double c = 0;       (int j = 0; j < onedlength; j++) {           double l = array1d[j];           double m = array2d[i][j];           c += l * m; // sum products of each set of elements       }       newarray[i] = c;    }    return newarray; // pass newarray main method } // end of getoutputarray method 

i hope did not make mistake, while trying fix.


Comments

Popular posts from this blog

java - WrongTypeOfReturnValue exception thrown when unit testing using mockito -

php - Magento - Deleted Base url key -

android - How to disable Button if EditText is empty ? -