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

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

矩陣用java代碼實(shí)現(xiàn) 矩陣乘法java代碼

如何用java編寫一個(gè)矩陣的轉(zhuǎn)置?

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

創(chuàng)新互聯(lián)建站服務(wù)緊隨時(shí)代發(fā)展步伐,進(jìn)行技術(shù)革新和技術(shù)進(jìn)步,經(jīng)過(guò)10余年的發(fā)展和積累,已經(jīng)匯集了一批資深網(wǎng)站策劃師、設(shè)計(jì)師、專業(yè)的網(wǎng)站實(shí)施團(tuán)隊(duì)以及高素質(zhì)售后服務(wù)人員,并且完全形成了一套成熟的業(yè)務(wù)流程,能夠完全依照客戶要求對(duì)網(wǎng)站進(jìn)行網(wǎng)站制作、成都網(wǎng)站建設(shè)、建設(shè)、維護(hù)、更新和改版,實(shí)現(xiàn)客戶網(wǎng)站對(duì)外宣傳展示的首要目的,并為客戶企業(yè)品牌互聯(lián)網(wǎng)化提供全面的解決方案。

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轉(zhuǎn)置:");

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編程實(shí)現(xiàn)矩陣連續(xù)相乘問(wèn)題

代碼如下:

//按照步驟查找矩陣相乘結(jié)果,i=1表示第一次結(jié)果,i=2表示第二次結(jié)果,以此類推,如果i不合法則返回null

private static int[][] getMatrixsMultiByStep(int i, int[][]... matrixs) {

MapString, int[][] result = getMatrixsMultiResult(matrixs);

if(i=0 || iresult.size()) {

return null;

}

return result.get("" + i);

}

//對(duì)個(gè)矩陣相乘,臨時(shí)結(jié)果和最終結(jié)果保存在Map中

private static MapString, int[][] getMatrixsMultiResult(int[][]... matrixs) {

MapString, int[][] result = new HashMapString, int[][]();

int[][] param = matrixs[0];

for(int i=1; imatrixs.length; i++) {

int[][] matrix = matrixs[i];

param = getMatrixMultiResult(param, matrix);

if(param == null) {

return null;

}

result.put("" + i, param);

}

return result;

}

//兩個(gè)矩陣相乘,結(jié)果為一個(gè)矩陣,返回值為空表示兩個(gè)矩陣不能相乘

private static int[][] getMatrixMultiResult(int[][] matrix1, int[][] matrix2) {

if(matrix2.length = 0) {

return matrix1;

}

if(matrix1.length=0 || matrix1[0].length!=matrix2.length) {

return null;

}

int[][] result = new int[matrix1.length][matrix2[0].length];

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

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

result[i][j] = 0;

for(int k=0; kmatrix1[0].length; k++) {

result[i][j] += matrix1[i][k]*matrix2[k][j];

}

}

}

return result;

}

測(cè)試程序:

public static void main(String[] args) {

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

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

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

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

int[][] value = getMatrixsMultiByStep(2, p1, p2, p3, p4);

if(value != null) {

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

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

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

}

System.out.println();

}

} else {

System.out.println("矩陣無(wú)法相乘!");

}

}

輸出結(jié)果:

218 293 368

412 553 694

509 683 857

PS:JDK要5.0以上,二維數(shù)組模擬矩陣,兼容普通矩陣相乘哦

用java:利用二維數(shù)組,任意給出一個(gè)矩陣,編寫實(shí)現(xiàn)矩陣轉(zhuǎn)置的代碼

借花獻(xiàn)佛

/**

*?實(shí)現(xiàn)二維數(shù)組的轉(zhuǎn)置

*?@author?HAN

*

*/

public?class?transposition_Arrays2D_ch6_4?{

final?static?double?PI=3.1415;

public?static?void?main(String[]?args)?{

/*StaticTest?st1=new?StaticTest();

StaticTest?st2=new?StaticTest();

st1.method2("HAN");*/

/*****定義要用于轉(zhuǎn)置的二維數(shù)組*******/

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

/*****構(gòu)造結(jié)果新二維數(shù)組用于存放轉(zhuǎn)置結(jié)果*******/

/*定義結(jié)果數(shù)組變量,注意?一定要先開辟一個(gè)內(nèi)存,

否則只是地址傳遞,也就是說(shuō)兩個(gè)數(shù)組名實(shí)際上指向的是同一塊內(nèi)存*/

//而構(gòu)造二維數(shù)組可以為維度來(lái)進(jìn)行,不一定是一個(gè)矩陣,即每一行的長(zhǎng)度不一定相同

int?result_arr[][]=new?int[arr2D.length][];//先實(shí)現(xiàn)第一維

for(int?i=0?;?iarr2D.length;i++){?//再實(shí)現(xiàn)第二維

result_arr[i]=new?int[arr2D[i].length];

}

// int?result_arr[][]=Arrays.copyOf(arr2D,?arr2D.length);

//上面這個(gè)命令行行不通!

/*****輸出用于轉(zhuǎn)置的二維數(shù)組*******/

for?(int?x[]:arr2D){

for(int?e:x){

System.out.print(e+"?");

}

System.out.println();

}

System.out.println();

/*******進(jìn)行元素倒置******/

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

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

result_arr[j][i]=arr2D[i][j];?//轉(zhuǎn)置核心

}

}

/*****show?the?result?in?the?result?matrix*******/

for?(int?x[]:result_arr){

for(int?e:x){

System.out.print(e+"?");

}

System.out.println();

}

}

}

//import?java.util.Arrays;

//public?class?transposition_Arrays2D?{

//

// public?static?void?main(String[]?args)?{

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

// /*定義結(jié)果數(shù)組變量,注意?一定要先開辟一個(gè)內(nèi)存,

// 否則只是地址傳遞,也就是說(shuō)兩個(gè)數(shù)組名實(shí)際上指向的是同一塊內(nèi)存*/

// int?result_arr[][]=new?int[arr2D.length][];

// for(int?i=0?;?iarr2D.length;i++){

// result_arr[i]=new?int[arr2D[i].length];

// }

//

//????????//?進(jìn)行元素倒置

//????????for(int?i=0?;?iarr2D.length;i++){

//???????? for(int?j=0;?jarr2D[i].length;j++){

//???????? result_arr[j][i]=arr2D[i][j];

//???????? }

//????????}

//????????

//????????//?show?the?result?in?matrix

//????????for?(int?x[]:result_arr){

//???????? for(int?e:x){

//???????? System.out.print(e);

//???????? }

//???????? System.out.println();

//????????}

//????????

// }

//

//}

用JAVA編寫矩陣

public static void main(String[] args) throws Exception {

print(create(getNum()));

}

private static int getNum() {

Scanner scanner = new Scanner(System.in);

int n = 0;

do {

System.out.println("請(qǐng)輸入要生成的階數(shù):");

String input = scanner.next();

if (isDigital(input)) {

n = Integer.parseInt(input);

if (n = 0) {

System.out.println("階數(shù)必須大于0");

}

}

} while (n == 0);

return n;

}

private static int[][] create(int n) {

int[][] num = new int[n][n];

int ax = 1, ay = 0, x = 0, y = 0;

for (int m = 1; m = n * n; m++) {

num[x][y] = m;

int tmpx = x + ax;

int tmpy = y + ay;

if (tmpx = n || tmpx 0 || tmpy = n || tmpy 0 || num[tmpx][tmpy] != 0) {

if (ax == 0) {

ax = -ay;

ay = 0;

} else if (ay == 0) {

ay = ax;

ax = 0;

}

}

x += ax;

y += ay;

}

return num;

}

private static void print(int[][] num) {

int length = String.valueOf(num.length * num.length).length();

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

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

String tmp = String.valueOf(num[i][j]);

while (tmp.length() length) {

tmp = " " + tmp;

}

System.out.print(tmp + " ");

}

System.out.println();

}

}

private static boolean isDigital(String input) {

if (input == null || input.length() == 0) return false;

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

char ch = input.charAt(i);

if (!Character.isDigit(ch)) {

System.out.println("輸入的階數(shù)必須為數(shù)字");

return false;

}

}

return true;

}

運(yùn)行時(shí)輸入要生成的階數(shù)就可以了,比如生成問(wèn)題上的矩陣輸入4就可以了。

用java怎么寫矩陣乘法?

import?java.util.Scanner;?

public class Matrix {?

public double[][] create() {?

Scanner sc = new Scanner(System.in) ;?

System.out.print("請(qǐng)輸入矩陣的行高:");?

int a = sc.nextInt() ;?

System.out.print("請(qǐng)輸入矩陣的列寬:");?

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("請(qǐng)輸入元素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("輸入的維數(shù)不匹配,不能進(jìn)行運(yùn)算");?

}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();?

}?

}?

}?

測(cè)試類:?

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("創(chuàng)建第一個(gè)數(shù)組:") ;?

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

m.print(x) ; //用來(lái)驗(yàn)證輸入的是否和你一樣的,沒啥作用?

System.out.println("創(chuàng)建第二個(gè)數(shù)組:");?

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

m.print(y) ; //用來(lái)驗(yàn)證輸入的是否和你一樣的,沒啥作用?

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

if(result == null){?

return ; //如果輸入的矩陣不能運(yùn)算就不輸出結(jié)果了。?

}?

m.print(result) ;?

}?

}

Java是一門面向?qū)ο缶幊陶Z(yǔ)言,不僅吸收了C++語(yǔ)言的各種優(yōu)點(diǎn),還摒棄了C++里難以理解的多繼承、指針等概念,因此Java語(yǔ)言具有功能強(qiáng)大和簡(jiǎn)單易用兩個(gè)特征。Java語(yǔ)言作為靜態(tài)面向?qū)ο缶幊陶Z(yǔ)言的代表,極好地實(shí)現(xiàn)了面向?qū)ο罄碚?,允許程序員以優(yōu)雅的思維方式進(jìn)行復(fù)雜的編程。 Java具有簡(jiǎn)單性、面向?qū)ο?、分布式、健壯性、安全性、平臺(tái)獨(dú)立與可移植性、多線程、動(dòng)態(tài)性等特點(diǎn)。Java可以編寫桌面應(yīng)用程序、Web應(yīng)用程序、分布式系統(tǒng)和嵌入式系統(tǒng)應(yīng)用程序等。

Java編寫一個(gè)程序?qū)崿F(xiàn)矩陣的運(yùn)算加減乘除,(并對(duì)其中的異常進(jìn)行處理)

/**

*?矩陣:由?m?×?n?個(gè)數(shù)Aij排成的m行n列的數(shù)表稱為m行n列的矩陣,簡(jiǎn)稱m?×?n矩陣

*?說(shuō)白了就是一個(gè)二維數(shù)組,下面的程序用整形作為數(shù)據(jù)類型,其他類型運(yùn)算大同小異

*?

*/

public?class?MatrixUtils?{

/**

*?矩陣運(yùn)算:加(減法與之類似)

*/

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

}

//矩陣加減要求兩個(gè)矩陣類型一致,即行列數(shù)相同

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;

}

/**

*?矩陣運(yùn)算:乘法,沒找到除法的運(yùn)算規(guī)則

*/

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

}

//兩個(gè)矩陣的乘法僅當(dāng)?shù)谝粋€(gè)矩陣A的列數(shù)和另一個(gè)矩陣B的行數(shù)相等時(shí)才能定義。如A是m×n矩陣和B是n×p矩陣,它們的乘積C是一個(gè)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;

}

}


網(wǎng)站欄目:矩陣用java代碼實(shí)現(xiàn) 矩陣乘法java代碼
當(dāng)前路徑:http://weahome.cn/article/dodgijc.html

其他資訊

在線咨詢

微信咨詢

電話咨詢

028-86922220(工作日)

18980820575(7×24)

提交需求

返回頂部