1

I have the following arrays in java, i would like to multiply array_x columns by array_y rows to finally create array_z values

aray_x        array_y            array_z

4|9          4|11|12|14             |
---          ----------           -----
8|7          13|9|22|7              | 
---                               -----
3|2                                 | 
---                               -----  
9|1                                 | 

my trial code listing public class Multiplication {

public static void main(String[] args) {
    int array_x[][] = {{9, 8}, {2, 17}, {49, 4}, {13, 119}, {2, 19}, {11, 47}, {3, 73}};
    int[][] array_y = new int[][]{{4, 11, 12, 14}, {13, 9, 22, 7}};
    int array_z[][] = new int[4][2];
    for (int i = 0; i < array_x.length; i++) {
        for (int j = 0; j < array_x.length; j++) {
            array_z[i][j] = array_x[i][j] * array_y[j][i];
                System.out.print(" "+array_z[i][j]);
        }

    }

}

}

How do i achieve this - That; The 1st column of array_z is populated by the multiples of the 1st column of array_x and the 1st row of array_y. e.g 4x4=16, 8x11=88, thius array_x * array_y =array_z The 2nd column of array_z is populated by the multiples of the 2nd column of array_x and the 2nd row of array_y.

6
  • Can you clarify the matrix dimensions? if rx=7, cx=2, ry=2 and cy=4 (as in your example), what do you expect the dimsions rz and cz? Is z[i,j] = Sum(x[i,k]*y[k*j])? If you formulate it this way, you have a simple matrix multiplication. Commented Feb 13, 2013 at 22:57
  • array x has 4 rows and 2 columns while array y has 2 rows and 4 columns, i would like to multiply column x by row y Commented Feb 13, 2013 at 23:07
  • So you are trying Z = Y*X, in terms of matrix multiplication... Commented Feb 13, 2013 at 23:18
  • Just answered with the pseudocode - hope it helps. Commented Feb 13, 2013 at 23:23
  • Thanks for that, i have refined the question, hope you understand it now well Commented Feb 13, 2013 at 23:36

2 Answers 2

2

Here is a complete class to compile and run, with your data... hope this helps

public class Alphy {

    private double[][] x;

    public Alphy (double[][] x) {
        this.x = x;
    }

    public double[][] multiplyWith (double[][] y) {
        int nr = x.length, nc = x[0].length;
        double[][] z = new double[nr][nc];

        for (int i = 0 ; i < nr ; i++)
            for (int j = 0 ; j < nc ; j++)
                z[i][j] = x[i][j] * y[j][i];
        return z;
    }

    public static void print (double[][] m, String label) {
        int nr = m.length, nc = m[0].length;
        System.out.println (label);
        for (int i = 0 ; i < nr ; i++) {
            for (int j = 0 ; j < nc ; j++)
                System.out.print ("\t" + m[i][j]);
            System.out.println();
    }}

    public static void main (String[] args) {
        double[][]  X = {{4, 9}, {8, 7}, {3, 2}, {9, 1}},
                Y = {{4, 11, 12, 14}, {13, 9, 22, 7}},
                Z = new Alphy(X).multiplyWith(Y);
        Alphy.print (X, "Initial Matrix");
        Alphy.print (Y, "Multiplied by");
        Alphy.print (Z, "Gives the result");
}}
/* Output of the above class:

Initial Matrix
    4.0 9.0
    8.0 7.0
    3.0 2.0
    9.0 1.0
Multiplied by
    4.0 11.0    12.0    14.0
    13.0    9.0 22.0    7.0
Gives the result
    16.0    117.0
    88.0    63.0
    36.0    44.0
    126.0   7.0
*/
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you Manidip that worked, could you comment the your code listing
this does not seem to be right , the result of the initial and multiplied matrix will be with 4 rows and 4 columns . correct me if i am wrong.
0

My Math is a bit rusty, but is this the correct answer?

    int array_x[][] = {{4, 9}, {8, 7}, {3, 2}, {9, 1}};
    int array_y[][] = {{4, 11, 12, 14}, {13, 9, 22, 7}};

    int array_z[][] = new int[array_x[0].length][array_y.length];
    for (int i = 0; i < array_x[0].length; i++) {
        for (int j = 0; j < array_y.length; j++) {
            if (array_x.length != array_y[i].length) {
                System.out.println("Dimention missmatch " + array_x.length +" vs "+ array_y[i].length);
                System.exit(-1);
            }

            int sum = 0;
            for (int k = 0; k < array_x.length; k++) {
                sum += array_x[k][i] * array_y[j][k];
             //   System.out.println(i+"\t"+ j +"\t"+k+"\t"+  array_x[k][i] +"\t"+  array_y[j][k] +"\t"+  sum+"\t");
            }
           // System.out.println();
            array_z[i][j] = sum;
        }
    }
    System.out.println(Arrays.deepToString(array_z));

Output:

[[266, 253], [151, 231]]

Or do you need a 4X4 matrix as a result?

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.