美文网首页
Matrix Operation

Matrix Operation

作者: 綿綿_ | 来源:发表于2019-04-01 17:36 被阅读0次

    Add

    public class MatrixAdd {
        static void Matrixadd(double a[][],double b[][],double c[][],int m,int n)
        {
            int i,j;
            for(i=0;i<m;i++)
                for(j=0;j<n;j++)
                {
                    c[i][j]=a[i][j]+b[i][j];
                }       
        }
        public static void main(String[] args)
        {
            double a[][]= {
                    {1.0,2.0,3.0},
                    {4.0,5.0,6.0},
                    {7.0,8.0,9.0}
            };
            double b[][]= {
                    {6.0,3.0,7.0},
                    {9.0,2.0,6.0},
                    {4.0,3.0,9.0}
            };
            double[][] c=new double [3][3];
            int m,n,i,j;
            m=n=3;
            System.out.println("the result of matrix plus is : ");
            Matrixadd(a,b,c,m,n);
            for(i=0;i<m;i++)
            {
                for(j=0;j<n;j++)
                {
                    System.out.printf("%10.6f", c[i][j]);
                }
                System.out.print("\n");
            }
        }
    }
    

    Minus

        static void MatrixMinus(double a[][],double b[][],double c[][],int m,int n)
        {
            int i,j;
            for(i=0;i<m;i++)
                for(j=0;j<n;j++)
                {
                    c[i][j]=a[i][j]-b[i][j];
                }       
        }
    

    Multiply

    public class MatrixMul {
        static void matrixMul(double a[][],double b[][],double c[][],int m,int n,int k)
        {
            int i,j,l;
            for(i=0;i<m;i++)
            {
                for(j=0;j<n;j++)
                {
                    c[i][j]=0;
                    for(l=0;l<k;l++)
                    {
                        c[i][j] +=(a[i][l]*b[l][j]);
                    }
                }
            }
        }
    
        public static void main(String[] args)
        {
            double a[][]= {
                    {1.0,2.0,3.0},
                    {4.0,5.0,6.0},
                    {7.0,8.0,9.0}
            };
            double b[][]= {
                    {6.0,3.0,7.0},
                    {9.0,2.0,6.0},
                    {4.0,3.0,9.0}
            };
            double[][] c=new double [3][3];
            int m,n,k,i,j;
            m=n=k=3;
            System.out.println("the result of matrix plus is : ");
            matrixMul(a,b,c,m,n,k);
            for(i=0;i<m;i++)
            {
                for(j=0;j<n;j++)
                {
                    System.out.printf("%10.6f", c[i][j]);
                }
                System.out.print("\n");
            }
        }
    }
    

    相关文章

      网友评论

          本文标题:Matrix Operation

          本文链接:https://www.haomeiwen.com/subject/moavbqtx.html