真实的国产乱ⅩXXX66竹夫人,五月香六月婷婷激情综合,亚洲日本VA一区二区三区,亚洲精品一区二区三区麻豆

成都創(chuàng)新互聯(lián)網(wǎng)站制作重慶分公司

兩矩陣相加java代碼,兩個(gè)矩陣的相加

java實(shí)現(xiàn)矩陣相加、相乘,判斷是否上(下)三角矩陣、對稱矩陣、相等的算法

class Matrix

創(chuàng)新互聯(lián)主營西盟網(wǎng)站建設(shè)的網(wǎng)絡(luò)公司,主營網(wǎng)站建設(shè)方案,重慶APP軟件開發(fā),西盟h5重慶小程序開發(fā)公司搭建,西盟網(wǎng)站營銷推廣歡迎西盟等地區(qū)企業(yè)咨詢

{

private int value[][]; //存儲矩陣元素的二維數(shù)組

public Matrix(int m, int n) //構(gòu)造m行n列的空矩陣

{

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

}

public Matrix(int n) //構(gòu)造n行n列的空矩陣

{

this(n,n);

}

public Matrix()

{

this(10,10);

}

public Matrix(int mat[][]) //構(gòu)造矩陣,由數(shù)組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) //設(shè)置矩陣第i行第j列的元素,O(1)

{

value[i][j]=k;

}

public void add(Matrix b) //this和b兩個(gè)矩陣相加,改變當(dāng)前矩陣

{

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() //矩陣的轉(zhuǎn)置

{

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;

}

//判斷一個(gè)矩陣是否為上三角矩陣

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;

}

//判斷一個(gè)矩陣是否為下三角矩陣

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;

}

//判斷一個(gè)矩陣是否為對稱矩陣

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;

}

//比較兩個(gè)矩陣是否相等

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;

}

//計(jì)算兩個(gè)矩陣的乘積

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的轉(zhuǎn)置矩陣:\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());

}

}

定義兩個(gè)3*3的矩陣,用java數(shù)組編程并計(jì)算出兩個(gè)矩陣相加和相乘的結(jié)果~

定義兩3*3數(shù)組,分別輸出矩陣之和跟相乘的結(jié)果,代碼如下:

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中用二維數(shù)組實(shí)現(xiàn)兩個(gè)矩陣相加

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

}

}

這里的前提是兩個(gè)矩陣大小一樣

java題: 自己定義兩個(gè)4*4階矩陣,求兩個(gè)矩陣的和、差。

看來樓主還是新手,我把代碼盡可能的給你寫的詳細(xì)了點(diǎn),主要還是記住,數(shù)組的操作就用for循環(huán),幾維的數(shù)組就嵌套幾個(gè)循環(huán)就行,比如本例中的是二維數(shù)組,所以用兩個(gè)for循環(huán)進(jìn)行數(shù)組的輸入和數(shù)組的輸出就行了。加油,多多練習(xí)就ok。代碼如下:

#includestdio.h

void?main()

{

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

printf("請輸入a矩陣的各個(gè)元素:\n");

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

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

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

printf("請輸入b矩陣的各個(gè)元素:\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("兩個(gè)矩陣相加的結(jié)果為:\n");

printf("\n");

printf("\n");

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

{

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

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

printf("\n");

}

}

用JAVA編寫一個(gè) 兩個(gè)2*2的矩陣求和,剛開始學(xué)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編程:實(shí)現(xiàn)兩個(gè)矩陣相加.

封裝性不用擔(dān)心,java正是以其良好的封裝性著稱。

小可獻(xiàn)丑了:

//沒有仔細(xì)調(diào)試,按照提示輸入數(shù)據(jù)

//每一行(Row)數(shù)據(jù)用空格隔開,結(jié)束后回車

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");

}

}

}


網(wǎng)頁名稱:兩矩陣相加java代碼,兩個(gè)矩陣的相加
轉(zhuǎn)載來于:http://weahome.cn/article/dssoesp.html

其他資訊

在線咨詢

微信咨詢

電話咨詢

028-86922220(工作日)

18980820575(7×24)

提交需求

返回頂部