Matrix Addition,Matrix Multipliction

1.Matrix Addition:

public static void main(String[] args) {
int[][] a= {{1,2,3}
{1,2,3},
{4,5,6}};
int[][] b= {{6,7,8},
{1,2,3},
{7,8,9}};
int [][] c=new int[a.length][a[0].length];
for(int row=0;row<a.length;row++)
{
for(int col=0;col<b.length;col++)
{
c[row][col]=a[row][col]+b[row][col];
System.out.print(c[row][col]+" ");
}
System.out.println();
}
}
public static void main(String[] args) {

int[][] a= {{1,2,3}
            {1,2,3},
        {4,5,6}};

int[][] b= {{6,7,8},
        {1,2,3},
        {7,8,9}};
int [][] c=new int[a.length][a[0].length]; 

for(int row=0;row<a.length;row++)
{
    for(int col=0;col<b.length;col++)
    {   
      c[row][col]=a[row][col]+b[row][col];
      System.out.print(c[row][col]+" ");
    }
      System.out.println();
}

}
public static void main(String[] args) { int[][] a= {{1,2,3} {1,2,3}, {4,5,6}}; int[][] b= {{6,7,8}, {1,2,3}, {7,8,9}}; int [][] c=new int[a.length][a[0].length]; for(int row=0;row<a.length;row++) { for(int col=0;col<b.length;col++) { c[row][col]=a[row][col]+b[row][col]; System.out.print(c[row][col]+" "); } System.out.println(); } }

Enter fullscreen mode Exit fullscreen mode

output:
7 9 11
2 4 6
11 13 15

2.Martix Multipliction:

public static void main(String[] args) {
int[][] a= {{1,2,3},
{1,2,3},
{4,5,6}};
int[][] b= {{6,7,8},
{1,2,3},
{7,8,9}};
int [][] c=new int[a.length][a[0].length];
for(int row=0;row<a.length;row++)
{
for(int col=0;col<b.length;col++)
{
for(int k = 0;k<b.length;k++)
{
c[row][col]=c[row][col]+(a[row][k]*b[k][col]);
}
System.out.print(c[row][col]+" ");
}
System.out.println();
}
}
public static void main(String[] args) {

int[][] a= {{1,2,3},
        {1,2,3},
        {4,5,6}};
int[][] b= {{6,7,8},
        {1,2,3},
        {7,8,9}};
int [][] c=new int[a.length][a[0].length];
for(int row=0;row<a.length;row++)
{
  for(int col=0;col<b.length;col++)
  {
    for(int k = 0;k<b.length;k++)
    {
      c[row][col]=c[row][col]+(a[row][k]*b[k][col]);
    }
    System.out.print(c[row][col]+" ");
  }
     System.out.println();
}

}
public static void main(String[] args) { int[][] a= {{1,2,3}, {1,2,3}, {4,5,6}}; int[][] b= {{6,7,8}, {1,2,3}, {7,8,9}}; int [][] c=new int[a.length][a[0].length]; for(int row=0;row<a.length;row++) { for(int col=0;col<b.length;col++) { for(int k = 0;k<b.length;k++) { c[row][col]=c[row][col]+(a[row][k]*b[k][col]); } System.out.print(c[row][col]+" "); } System.out.println(); } }

Enter fullscreen mode Exit fullscreen mode

output:
29 35 41
29 35 41
71 86 101

原文链接:Matrix Addition,Matrix Multipliction

© 版权声明
THE END
喜欢就支持一下吧
点赞9 分享
Stop to have a rest, do not forget others still in the running.
停下来休息的时候,不要忘记别人还在奔跑
评论 抢沙发

请登录后发表评论

    暂无评论内容