1

我在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 的第 2 列由 array_x 的第 2 列和 array_y 的第 2 行的倍数填充。

4

2 回答 2

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
*/
于 2013-02-13T23:22:21.290 回答
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 矩阵?

于 2013-02-14T00:51:24.363 回答