Java教程

Java添加两个矩阵

我们可以使用二进制+运算符在Java中添加两个矩阵。矩阵也称为数组数组。我们可以添加,减去和相乘矩阵。
在Java中添加两个矩阵
要减去两个矩阵,请使用-运算符。让我们看一个简单的示例,添加两个3行3列的矩阵。
public class MatrixAdditionExample{
    public static void main(String args[]){
        //creating two matrices int a[][]={
            {1,3,4},
            {2,4,3},
            {3,4,5}
        };
        int b[][]={
            {
            1,3,4},
            {2,4,3},
            {1,2,4}
        };
        int c[][]=new int[3][3];
        for(int i=0;i<3;i++){
            for(int j=0;j<3;j++){
                c[i][j]=a[i][j]+b[i][j];
                //use - for subtractionSystem.out.print(c[i][j]+" ");
            }
            System.out.println();
        //new line }
    }
}
输出:
2 6 84 8 64 6 9
昵称: 邮箱:
Copyright © 2022 立地货 All Rights Reserved.
备案号:京ICP备14037608号-4