Java 多维数组列乘以行

Java 多维数组列乘以行,java,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

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

    }

}
publicstaticvoidmain(字符串[]args){
int数组_x[][]={{9,8},{2,17},{49,4},{13,119},{2,19},{11,47},{3,73};
int[][]数组_y=新int[][{{4,11,12,14},{13,9,22,7};
int数组_z[][]=新int[4][2];
for(int i=0;i
}

我如何实现这个-那个; 数组_z的第一列由数组_x的第一列和数组_y的第一行的倍数填充。e、 g 4x4=16,8x11=88,thius数组_x*数组_y=数组_z
array_z的第二列由array_x的第二列和array_y的第二行的倍数填充。

这里有一个完整的类,需要编译和运行,其中包含您的数据。。。希望这有帮助

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
*/
public class-Alphy{
私人双[]x;
公共阿尔菲(双[][]x){
这个.x=x;
}
公共双精度[]倍精度(双精度[][]y){
int nr=x.length,nc=x[0]。长度;
双精度[]z=新双精度[nr][nc];
对于(int i=0;i
我的数学有点生疏,但这是正确答案吗

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

或者您是否需要一个4X4矩阵作为结果?

您能澄清矩阵尺寸吗?如果rx=7、cx=2、ry=2和cy=4(如您的示例中所示),那么rz和cz的尺寸是多少?是z[i,j]=和(x[i,k]*y[k*j])?如果你这样表述,你就有了一个简单的矩阵乘法。数组x有4行2列,而数组y有2行4列,我想将列x乘以行ySo,你在尝试Z=y*x,在矩阵乘法方面…刚刚用伪代码回答了-希望有帮助。谢谢你,我改进了这个问题,希望您现在能很好地理解它谢谢您的Manidip工作,您能评论一下您的代码列表吗这似乎不正确,初始矩阵和相乘矩阵的结果将是4行4列。如果我错了,请纠正我。
[[266, 253], [151, 231]]