RELATEED CONSULTING
相关咨询
选择下列产品马上在线沟通
服务时间:8:30-17:00
你可能遇到了下面的问题
关闭右侧工具栏

新闻中心

这里有您想知道的互联网营销解决方案
两矩阵相加java代码,两个矩阵的相加

java实现矩阵相加、相乘,判断是否上(下)三角矩阵、对称矩阵、相等的算法

class Matrix

创新互联主营西盟网站建设的网络公司,主营网站建设方案,重庆APP软件开发,西盟h5重庆小程序开发公司搭建,西盟网站营销推广欢迎西盟等地区企业咨询

{

private int value[][]; //存储矩阵元素的二维数组

public Matrix(int m, int n) //构造m行n列的空矩阵

{

this.value=new int[m][n];

}

public Matrix(int n) //构造n行n列的空矩阵

{

this(n,n);

}

public Matrix()

{

this(10,10);

}

public Matrix(int mat[][]) //构造矩阵,由数组mat提供矩阵元素

{

this(mat.length,mat[0].length);

for (int i=0; imat.length; i++)

for (int j=0; jmat[i].length; j++)

this.value[i][j] = mat[i][j];

}

public int get(int i, int j) //获得矩阵第i行第j列的元素,O(1)

{

return value[i][j];

}

public void set(int i, int j, int k) //设置矩阵第i行第j列的元素,O(1)

{

value[i][j]=k;

}

public void add(Matrix b) //this和b两个矩阵相加,改变当前矩阵

{

for (int i=0; ithis.value.length; i++)

for (int j=0; jthis.value[i].length; j++)

this.value[i][j] += b.value[i][j];

}

public String toString() //行主序遍历,访问矩阵全部元素

{

String str="";

for (int i=0; ivalue.length; i++)

{

for (int j=0; jvalue[i].length; j++)

str += " "+value[i][j];

str += "\n";

}

return str;

}

public Matrix transpose() //矩阵的转置

{

Matrix trans = new Matrix(value[0].length, value.length);

for (int i=0; ithis.value.length; i++)

for (int j=0; jthis.value[i].length; j++)

trans.value[j][i]=this.value[i][j];

return trans;

}

//判断一个矩阵是否为上三角矩阵

public boolean isUpperTriangularMatrix() {

int i, j = 0;

int c = this.value[1][0];

for(i=1; ithis.value.length; i++)

for(j=0; ji; j++)

if(this.value[i][j] != c)

break;

if(i=this.value.length)

return true;

return false;

}

//判断一个矩阵是否为下三角矩阵

public boolean isLowerTriangularMatrix() {

int i, j = 0;

int c = this.value[0][1];

for(i=0; ithis.value.length-1; i++)

for(j=i+1; jthis.value[0].length; j++)

if(this.value[i][j] != c)

break;

if(i=this.value.length-1)

return true;

return false;

}

//判断一个矩阵是否为对称矩阵

public boolean isSymmetricMatrix () {

int i, j = 0;

for(i=1; ithis.value.length; i++)

for(j=0; ji; j++)

if(this.value[i][j] != this.value[j][i])

break;

if(i=this.value.length)

return true;

return false;

}

//比较两个矩阵是否相等

public boolean equals(Matrix b) {

int i, j = 0;

if(this.value.length != b.value.length || this.value[0].length != b.value[0].length)

return false;

for(i=0; ithis.value.length; i++)

for(j=0; jthis.value[0].length; j++)

if(this.value[i][j] != b.value[j][i])

break;

if(i=this.value.length)

return true;

return false;

}

//计算两个矩阵的乘积

public Matrix multiply(Matrix b){

int i, j, k;

int sum;

Matrix mtr;

if(this.value[0].length != b.value.length) {

return null;

}

mtr = new Matrix(this.value.length, b.value[0].length);

for(i=0; ithis.value.length; i++)

{

for(k=0; kb.value[0].length; k++){

for(sum=0,j=0; jthis.value[0].length; j++){

sum += this.value[i][j] * b.value[j][k];

mtr.value[i][k] = sum;

}

}

}

return mtr;

}

}

public class Test

{

public static void main(String args[])

{

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

Matrix a=new Matrix(m1);

int m2[][]={{1,0,0},{0,1,0}};

Matrix b=new Matrix(m2);

System.out.print("Matrix a:\n"+a.toString());

System.out.print("Matrix b:\n"+b.toString());

a.add(b);

System.out.print("Matrix a:\n"+a.toString());

System.out.println("a的转置矩阵:\n"+a.transpose().toString());

int m3[][] = {{1,2,1},{0,3,1},{0,0,2}};

int m4[][] = {{1,0,0},{2,1,0},{3,2,1}};

int m5[][] = {{1,0,2},{0,1,0},{2,0,2}};

Matrix mtr1 = new Matrix(m3);

Matrix mtr2 = new Matrix(m4);

Matrix mtr3 = new Matrix(m5);

if(mtr1.isUpperTriangularMatrix())

System.out.println("上三角矩阵:\n" + mtr1.toString());

if(mtr2.isLowerTriangularMatrix())

System.out.println("下三角矩阵:\n" + mtr2.toString());

if(mtr3.isSymmetricMatrix())

System.out.println("对称矩阵:\n" + mtr3.toString());

System.out.println(mtr1.toString() + "\n乘以\n" + mtr2.toString() + "\n=\n");

Matrix tempM = mtr1.multiply(mtr2);

System.out.println(tempM.toString());

}

}

定义两个3*3的矩阵,用java数组编程并计算出两个矩阵相加和相乘的结果~

定义两3*3数组,分别输出矩阵之和跟相乘的结果,代码如下:

public class Problem {

public static void main(String[] args){

int arrayA[][]={{1,2,3},

{4,5,6},

{7,8,9}};

int arrayB[][]={{1,2,3},

{4,5,6},

{7,8,9}};

int arrayC[][] = new int[3][3];

int arrayD[][] = new int[3][3];

int i,j,k;

for (i = 0;i 3;i++){

for (j = 0; j 3; j++){

arrayC[i][j] = arrayA[i][j]+arrayB[i][j];

for (k = 0; k 3; k++){

arrayD[i][j] += arrayA[i][k]*arrayB[k][j];

}

System.out.print(arrayC[i][j]);

System.out.print(" ");

}

System.out.println();

}

System.out.println();

for (i = 0;i 3;i++){

for (j = 0; j 3; j++){

System.out.print(arrayD[i][j]);

System.out.print(" ");

}

System.out.println();

}

}

}

java中用二维数组实现两个矩阵相加

int[][] list1 = {{1, 2, 3}, {1, 2, 3}, {1, 2, 3}};

int[][] list2 = {{1, 2, 3}, {1, 2, 3}, {1, 2, 3}};

int[][] result = new int[3][3];

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

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

result[i][j] = list1[i][j] + list2[i][j];

}

}

这里的前提是两个矩阵大小一样

java题: 自己定义两个4*4阶矩阵,求两个矩阵的和、差。

看来楼主还是新手,我把代码尽可能的给你写的详细了点,主要还是记住,数组的操作就用for循环,几维的数组就嵌套几个循环就行,比如本例中的是二维数组,所以用两个for循环进行数组的输入和数组的输出就行了。加油,多多练习就ok。代码如下:

#includestdio.h

void main()

{

int i,j,a[4][5]={0},b[4][5]={0},c[4][5]={0};

printf("请输入a矩阵的各个元素:\n");

for(i=0;i4;i++)

for(j=0;j5;j++)

scanf("%d",a[i][j]);

printf("请输入b矩阵的各个元素:\n");

for(i=0;i4;i++)

for(j=0;j5;j++)

scanf("%d",b[i][j]);

printf("您输入的a矩阵是:\n");

for(i=0;i4;i++)

{

for(j=0;j5;j++)

printf("%d  ",a[i][j]);

printf("\n");

}

printf("\n");

printf("\n");

printf("您输入的a矩阵是:\n");

for(i=0;i4;i++)

{

for(j=0;j5;j++)

printf("%d  ",b[i][j]);

printf("\n");

}

for(i=0;i4;i++)

for(j=0;j5;j++)

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

printf("两个矩阵相加的结果为:\n");

printf("\n");

printf("\n");

for(i=0;i4;i++)

{

for(j=0;j5;j++)

printf("%d  ",c[i][j]);

printf("\n");

}

}

用JAVA编写一个 两个2*2的矩阵求和,刚开始学JAVA 很多地方不是很懂。麻烦讲一下。

public static void main(String[] args)

{

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

int[][] b = {{1,2,3},{4,5,6},{7,8,9}};

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];

}

}

for(int i = 0;i 3;i++)

{

for(int j = 0;j 3;j++)

{

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

}

System.out.println();

}

}

用java编程:实现两个矩阵相加.

封装性不用担心,java正是以其良好的封装性著称。

小可献丑了:

//没有仔细调试,按照提示输入数据

//每一行(Row)数据用空格隔开,结束后回车

import java.io.*;

import java.util.*;

public class Arrtest{

public static void main(String args[]){

try{

System.out.print("set the row of the arr:\n");

BufferedReader in=new BufferedReader(

new InputStreamReader(System.in));

String row=in.readLine();

System.out.print("set the line of the arr:\n");

BufferedReader in2=new BufferedReader(

new InputStreamReader(System.in));

String line=in2.readLine();

int R=Integer.parseInt(row);

int L=Integer.parseInt(line);

Arr arr=new Arr();

System.out.print("Great the first arr:\n");

double arr1[][]=arr.greatArr(R,L);

System.out.print("Great the second arr:\n");

double arr2[][]=arr.greatArr(R,L);

arr.showArr(arr.addArr(arr1,arr2,R,L),R,L);

}catch(Exception e){e.getMessage(); }

}

}

class Arr {

double[][] greatArr(int row,int line){

double arr[][]=new double[row][line];

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

try{

System.out.print("input the row("+(i+1)+") numbers:\n");

BufferedReader in=new BufferedReader(

new InputStreamReader(System.in));

String s=in.readLine();

StringTokenizer s_part=new StringTokenizer(s," ");

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

arr[i][j]=Double.parseDouble(s_part.nextToken());

}

}catch(Exception e){e.getMessage(); }

}

return arr;

}

double[][] addArr(double[][] arr1,double[][] arr2,int row,int line){

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

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

arr1[i][j]=arr1[i][j]+arr2[i][j];

}

}

return arr1;

}

void showArr(double[][] arr,int row,int line){

System.out.print("the result:\r\n");

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

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

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

}

System.out.print("\r\n");

}

}

}


名称栏目:两矩阵相加java代码,两个矩阵的相加
URL分享:http://lswzjz.com/article/dssoesp.html