按行乘以多维数组列

时间:2013-02-13 22:45:12

标签: java

我在java中有以下数组,我想将array_x列乘以array_y行以最终创建array_z值

aray_x        array_y            array_z

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

我的试用代码列表 公共类乘法{

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]);
        }

    }

}

}

我如何实现这一目标 - 那; array_z的第1列由array_x的第1列和array_y的第1行的倍数填充。例如4x4 = 16,8x11 = 88,thius array_x * array_y = array_z array_z的第二列由array_x的第二列和array_y的第二行的倍数填充。

2 个答案:

答案 0 :(得分:2)

这是一个完整的编译和运行类,包含您的数据......希望这有帮助

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
*/

答案 1 :(得分:0)

我的数学有点生疏,但这是正确答案吗?

    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));

输出:

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

或者你需要一个4X4矩阵吗?