首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >多维数组逐行乘以

多维数组逐行乘以
EN

Stack Overflow用户
提问于 2013-02-14 06:45:12
回答 2查看 2.6K关注 0票数 1

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

代码语言:javascript
复制
aray_x        array_y            array_z

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

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

代码语言:javascript
复制
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行的倍数填充。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2013-02-14 07:22:21

下面是一个要编译和运行的完整类,其中包含您的数据...希望这能有所帮助

代码语言:javascript
复制
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
*/
票数 2
EN

Stack Overflow用户

发布于 2013-02-14 08:51:24

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

代码语言:javascript
复制
    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));

输出:

代码语言:javascript
复制
[[266, 253], [151, 231]]

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/14864700

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档