Java学习者论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

手机号码,快捷登录

恭喜Java学习者论坛(https://www.javaxxz.com)已经为数万Java学习者服务超过8年了!积累会员资料超过10000G+
成为本站VIP会员,下载本站10000G+会员资源,购买链接:点击进入购买VIP会员
JAVA高级面试进阶视频教程Java架构师系统进阶VIP课程

分布式高可用全栈开发微服务教程

Go语言视频零基础入门到精通

Java架构师3期(课件+源码)

Java开发全终端实战租房项目视频教程

SpringBoot2.X入门到高级使用教程

大数据培训第六期全套视频教程

深度学习(CNN RNN GAN)算法原理

Java亿级流量电商系统视频教程

互联网架构师视频教程

年薪50万Spark2.0从入门到精通

年薪50万!人工智能学习路线教程

年薪50万!大数据从入门到精通学习路线年薪50万!机器学习入门到精通视频教程
仿小米商城类app和小程序视频教程深度学习数据分析基础到实战最新黑马javaEE2.1就业课程从 0到JVM实战高手教程 MySQL入门到精通教程
查看: 867|回复: 0

java矩阵的运算(加、乘与转置)

[复制链接]

该用户从未签到

发表于 2011-9-18 13:30:32 | 显示全部楼层 |阅读模式
矩阵的转置即行列互换。矩阵的加法是对应元素相加。矩阵的乘法是对应行列的点积。

import java.util.Scanner;   
  
public class Main {   
    //打印矩阵   
    public static void printMatrix(double[][] matrix,int a,int b){   
        for(int i=0; i< a; i++){   
            for(int j=0; j< b; j++){   
                System.out.printf("%9.5f ",matrix[j]);   
            }   
            System.out.println();   
        }   
        System.out.println();   
    }   
    public static void main(String[] args) {   
     // Scanner scanner = new Scanner(System.in);   
     // int a = scanner.nextInt();   
     // int b = scanner.nextInt();   
        int a = 3;   
        int b = 3;   
        double[][] matrix = new double[][]{{0.84339,0.42034,0.76091},{0.79426,0.89185,0.52491},{0.72037,0.83435,0.36943}};   
     //  while(scanner.hasNext()){   
     //  for(int i=0; i< a; i++){   
    //     for(int j=0; j< b; j++){   
    //        matrix[j] = scanner.nextDouble();   
    //      }   
    //    }   
    //  }   
        double[][] matrixT = new double[a];//矩阵转置   
        double[][] matrixA = new double[a];//矩阵加   
        double[][] matrixP = new double[a];//矩阵乘   
        //矩阵转置   
        for(int i=0; i< a; i++){   
            for(int j=0; j< b; j++){   
                matrixT[j] = matrix[j];   
                matrixA[j] = matrix[j] + matrix[j];   
                 
            }   
        }   
        for(int i=0; i< a; i++){   
            for(int j=0; j< b; j++){   
                for (int k = 0; k< b; k++) {//columns of a = rows of b   
                matrixP[j] += matrix[k]*matrixT[k][j];   
                  
                }   
            }   
        }   
        printMatrix(matrixT,3,3);   
        printMatrix(matrixA,3,3);   
        printMatrix(matrixP,3,3);   
  
    }   
}  
C:\test>java  Main
  0.84339   0.79426   0.72037
  0.42034   0.89185   0.83435
  0.76091   0.52491   0.36943
  1.68678   1.21460   1.48128
  1.21460   1.78370   1.35926
  1.48128   1.35926   0.73886
  1.46698   1.44416   1.23937
  1.44416   1.70178   1.51019
  1.23937   1.51019   1.35155
  
  矩阵乘法:
  
  public class MultiMatrix {
int[][] multiplyMatrix;
    public static void main(String args[]){
       int[][] a={{1,0,3,-1},{2,1,0,2}};
       int[][] b={{4,1,0},{-1,1,3},{2,0,1},{1,3,4}};
    MultiMatrix mm=new MultiMatrix();
    mm.mMatrix(a,b);
    mm.display();   
}
    public void mMatrix(int[][] a,int[][] b){        
        multiplyMatrix=new int[a.length][b[0].length];
        for (int i = 0; i< a.length; i++) {//rows of a
         for (int j = 0; j< b[0].length; j++) {//columns of b
               for (int k = 0; k< a[0].length; k++) {//columns of a = rows of b
                  multiplyMatrix[j]=multiplyMatrix[j]+a[k]*b[k][j];
       }      
     }
     }        
     }
    public void display(){
       for (int i = 0; i< multiplyMatrix.length; i++) {
         for (int j = 0; j< multiplyMatrix[0].length; j++) {
          System.out.print (multiplyMatrix[j]+" ");
         
      }
       System.out.println ("");
    }
     }   
}
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|手机版|Java学习者论坛 ( 声明:本站资料整理自互联网,用于Java学习者交流学习使用,对资料版权不负任何法律责任,若有侵权请及时联系客服屏蔽删除 )

GMT+8, 2024-6-7 18:19 , Processed in 0.390666 second(s), 46 queries .

Powered by Discuz! X3.4

© 2001-2017 Comsenz Inc.

快速回复 返回顶部 返回列表