導航:首頁 > 編程語言 > java矩陣的轉置

java矩陣的轉置

發布時間:2025-02-19 12:04:00

A. 如何用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}};
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] + " ");
}
}
}

代碼寫得爛不要噴我,我想嫌哪蘆解決思路就是這樣吧!

B. 用java聲明Matrix類表示矩陣,使用二維數組存儲矩陣元素,實現以下方法:

public class Matrix {
private static String matrix_A;
private int mx[][], m, n;
public Matrix(int r, int c) {
m = r;
n = c;
mx = new int[m][n];
iniMatrix();
}
public Matrix() {
m = 3;
n = 3;
mx = new int[3][3];
iniMatrix();
}
public void iniMatrix()// 隨機取數
{
int i, j;
for (i = 0; i <= m - 1; i++)
for (j = 0; j <= n - 1; j++)
mx[i][j] = (int) (Math.random() * 100);
}
public void tranMatrix()// 轉置矩陣
{
int i, j, t;
int mt[][] = new int[m][n];
for (i = 0; i <= m - 1; i++)
for (j = 0; j <= n - 1; j++)
mt[i][j] = mx[i][j];
t = m;
m = n;
n = t;
mx = new int[m][n];
for (i = 0; i <= m - 1; i++)
for (j = 0; j <= n - 1; j++)
mx[i][j] = mt[j][i];
}
public void printMatrix()// 輸出矩陣所有值
{
int i, j;
for (i = 0; i <= m - 1; i++) {
for (j = 0; j <= n - 1; j++)
System.out.print(" " + mx[i][j]);
System.out.println();
}
}
//判斷一個矩陣是否為上三角矩陣
public boolean isUpperTriangularMatrix() {
int i, j = 0;
int c = this.mx[1][0];

for(i=1; i<this.mx.length; i++)
for(j=0; j<i; j++)
if(this.mx[i][j] != c)
break;
if(i>=this.mx.length)
return true;
return false;
}
public void addMatrix(Matrix b)// 矩陣相加
{
int i, j;
for (i = 0; i <= m - 1; i++)
for (j = 0; j <= n - 1; j++)
mx[i][j] = mx[i][j] + b.mx[i][j];
}
public static void main(String args[]) {
Matrix ma = new Matrix(4, 3);
Matrix mb = new Matrix(4, 3);
System.out.println("The matrix_A:");
ma.printMatrix();
System.out.println("The matrix_B:");
mb.printMatrix();
if(ma.isUpperTriangularMatrix())
System.out.println("上三角矩陣:\n" + ma.isUpperTriangularMatrix());
System.out.println("Matrix_A + Matrix_B:");
ma.addMatrix(mb);
ma.printMatrix();
System.out.println("Transpose Matrix_A:");
mb.tranMatrix();
mb.printMatrix();
System.out.println("Transpose Matrix_A+Matrix_B:");
mb.tranMatrix();
mb.printMatrix();
}
}

C. java行列值的互換

二維數組的行列值互換,又叫矩陣的轉置,數組a轉置後應該是1,4;2,5;3,6;

完整的矩陣轉置的Java程序如下

D. 用java編寫程序用一個二維數組a[ ][ ]存放3*3矩陣,求它的轉置矩陣b[ ][ ]。

public class T {

public static void main(String[] args) {
int i,j;
int[][] a = {{1,1,1},{2,2,2},{3,3,3}};
int[][] b = new int[3][3];
System.out.print("初始矩陣:\n");
for(i=0;i<3;i++) {
for(j=0;j<3;j++) {
System.out.print(a[i][j]+" ");
}
System.out.print("\n");
}
System.out.print("轉置矩陣:\n");
for(i=0;i<3;i++) {
for(j=0;j<3;j++) {
b[i][j] = a[j][i];
System.out.print(b[i][j]+" ");
}
System.out.print("\n");
}
}
}

閱讀全文

與java矩陣的轉置相關的資料

熱點內容
幼兒園設計pdf 瀏覽:645
干接點輸入單片機 瀏覽:541
亞馬遜雲伺服器查看 瀏覽:163
如何用免費雲伺服器 瀏覽:610
php的輸出命令 瀏覽:264
在家怎麼製作解壓小玩具 瀏覽:99
javascript源碼輔助閱讀 瀏覽:384
pythonui開發工具 瀏覽:595
adr指標源碼 瀏覽:217
程序員轉架構管理 瀏覽:959
企業伺服器為什麼不能被拷貝 瀏覽:119
用c編程實現txt搜索 瀏覽:854
JAVA小數隨機數 瀏覽:337
app加拿大pc怎麼操控的 瀏覽:698
光影app蘋果怎麼下載不了 瀏覽:971
php會員注冊代碼 瀏覽:511
csgo如何用128tick伺服器 瀏覽:571
百度網度怎麼解壓 瀏覽:946
windowsopencv源碼 瀏覽:946
origin平滑演算法 瀏覽:875