资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

java代码实现矩阵 Java矩阵

如何用java编写一个矩阵的转置?

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

成都创新互联专业为企业提供鱼台网站建设、鱼台做网站、鱼台网站设计、鱼台网站制作等企业网站建设、网页设计与制作、鱼台企业网站模板建站服务,10年鱼台做网站经验,不只是建网站,更提供有价值的思路和整体网络服务。

int b[][] = new int [4][7];

for (int i = 0; i 4; i++) {

for (int j = 0; j 7; j++) {

b[i][j] = a[j][i];

}

}

System.out.println("矩阵a:");

for (int i = 0; i 7; i++) {

for (int j = 0; j 4; j++) {

if (j 3) {

System.out.print(a[i][j] + " ");

} else {

System.out.println(a[i][j] + " ");

}

}

}

System.out.println("矩阵a转置:");

for (int i = 0; i 4; i++) {

for (int j = 0; j 7; j++) {

if (j 6) {

System.out.print(b[i][j] + " ");

} else {

System.out.println(b[i][j] + " ");

}

}

}

代码写得烂不要喷我,我想解决思路就是这样吧!

用java怎么写矩阵乘法?

import java.util.Scanner; 

public class Matrix { 

public double[][] create() { 

Scanner sc = new Scanner(System.in) ; 

System.out.print("请输入矩阵的行高:"); 

int a = sc.nextInt() ; 

System.out.print("请输入矩阵的列宽:"); 

int b = sc.nextInt() ; 

double[][] x = new double[a][b] ; 

for(int i=0;ilt;a;i++){ 

for(int j=0;jlt;b;j++){ 

System.out.print("请输入元素x["+i+"]["+j+"]的值:" ); 

x[i][j] = sc.nextDouble() ; 

return x ; 

public double[][] multiply(double[][] x,double[][] y){ 

double[][] result = null ; 

int a = x[0].length ; 

int b = y.length ; 

if(a != b){ 

System.out.println("输入的维数不匹配,不能进行运算"); 

}else{ 

int c = x.length ; 

int d = y[0].length ; 

result = new double[c][d] ; 

for(int i=0;ilt;c;i++){ 

for(int j=0;jlt;d;j++){ 

double sum = 0 ; 

for(int k=0;klt;a;k++){ 

sum += x[i][k]*y[k][j] ; 

result[i][j] = sum ; 

return result ; 

public void print(double[][] x){ 

System.out.println("矩阵为:"); 

for(int i=0;ilt;x.length;i++){ 

for(int j=0;jlt;x[i].length;j++){ 

System.out.print(x[i][j] + " ") ; 

System.out.println(); 

测试类: 

public class TestMatrix { 

public static void main(String[] args) { 

Matrix m = new Matrix() ; 

//double[][] x = {{1,2},{3,2}} ; 

//double[][] y = {{1,2,1},{2,3,3}} ; 

System.out.println("创建第一个数组:") ; 

double[][] x = m.create() ; 

m.print(x) ; //用来验证输入的是否和你一样的,没啥作用 

System.out.println("创建第二个数组:"); 

double[][] y = m.create() ; 

m.print(y) ; //用来验证输入的是否和你一样的,没啥作用 

double[][] result = m.multiply(x, y) ; 

if(result == null){ 

return ; //如果输入的矩阵不能运算就不输出结果了。 

m.print(result) ; 

}

Java是一门面向对象编程语言,不仅吸收了C++语言的各种优点,还摒弃了C++里难以理解的多继承、指针等概念,因此Java语言具有功能强大和简单易用两个特征。Java语言作为静态面向对象编程语言的代表,极好地实现了面向对象理论,允许程序员以优雅的思维方式进行复杂的编程。 Java具有简单性、面向对象、分布式、健壮性、安全性、平台独立与可移植性、多线程、动态性等特点。Java可以编写桌面应用程序、Web应用程序、分布式系统和嵌入式系统应用程序等。

Java编写一个程序实现矩阵的运算加减乘除,(并对其中的异常进行处理)

/**

* 矩阵:由 m × n 个数Aij排成的m行n列的数表称为m行n列的矩阵,简称m × n矩阵

* 说白了就是一个二维数组,下面的程序用整形作为数据类型,其他类型运算大同小异

*/

public class MatrixUtils {

/**

* 矩阵运算:加(减法与之类似)

*/

public static int[][] matrixAdd(int[][] addend, int[][] summand) {

if (addend == null || addend.length == 0) {

throw new IllegalArgumentException("addend matrix is empty!");

}

if (summand == null || summand.length == 0) {

throw new IllegalArgumentException("summand matrix is empty!");

}

//矩阵加减要求两个矩阵类型一致,即行列数相同

int row = addend.length;

int col = addend[0].length;

if (row != summand.length || col != summand[0].length) {

throw new IllegalArgumentException("summand and summand not the same type!");

}

int[][] sum = new int[row][col];

for (int i = 0; i  row; i++) {

for (int j = 0; j  col; j++) {

sum[i][j] = addend[i][j] + summand[i][j];

// sum[i][j] = addend[i][j] - summand[i][j]; //减法

}

}

return sum;

}

/**

* 矩阵运算:乘法,没找到除法的运算规则

*/

public static int[][] matrixMultiply(int[][] addend, int[][] summand) {

if (addend == null || addend.length == 0) {

throw new IllegalArgumentException("addend matrix is empty!");

}

if (summand == null || summand.length == 0) {

throw new IllegalArgumentException("summand matrix is empty!");

}

//两个矩阵的乘法仅当第一个矩阵A的列数和另一个矩阵B的行数相等时才能定义。如A是m×n矩阵和B是n×p矩阵,它们的乘积C是一个m×p矩阵 

int row = addend.length;

int col = summand[0].length;

if (addend[0].length != summand.length) {

throw new IllegalArgumentException("summand and summand not the same type!");

int[][] sum = new int[row][col];

for (int i = 0; i  row; i++) {

for (int j = 0; j  col; j++) {

for (int z = 0; z  addend[0].length; z++) {

sum[i][j] += addend[i][z] * summand[z][j];

System.out.println("sum[" + i+  "]["+ j+"]= " + sum[i][j]);

}

}

}

return sum;

}

}


网页名称:java代码实现矩阵 Java矩阵
分享URL:http://cdkjz.cn/article/dodcggs.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

业务热线:400-028-6601 / 大客户专线   成都:13518219792   座机:028-86922220