62,632
社区成员
发帖
与我相关
我的任务
分享
public class Matrix {
//Line And Row Number: line-a, row-b
private int a,b;
//Private 2-Dimension Integer Matrix
private int[][] mMatrix;
/**
* Constructor
*
* @param a lineNum
*
* @param b rowNum
*
* @param matrixValue MatrixValue
*/
public Matrix(int a, int b, int[] matrixValue) {
this.a = a;
this.b = b;
this.mMatrix = new int[a][b];
int valueIndex = 0;
for(int i=0; i<a; i++) {
for(int j=0; j<b; j++) {
mMatrix[i][j] = valueIndex < matrixValue.length ? matrixValue[valueIndex++] : 0;
}
}
}
public int[][] getMMatrix() {
return mMatrix;
}
//Plus With Another Matrix
public void martixPlus(Matrix matrix) {
if(b == matrix.a) {
int[][] plusResult = new int[a][matrix.b];
int[][] pMatrix = matrix.getMMatrix();
for(int i=0; i<a; i++) {
for(int j=0; j<matrix.b; j++) {
for(int k=0; k<b; k++) {
plusResult[i][j] += mMatrix[i][k] * pMatrix[k][j];
}
}
}
printPlusResult(mMatrix, pMatrix, plusResult);
}else {
System.out.println("Can Not Execute MatrixPlus...");
}
}
private void printPlusResult(int[][] mMatrix, int[][] pMatrix, int[][] plusResult) {
System.out.println("MatrixPlusResult:");
int lineIndex = mMatrix.length > mMatrix[0].length ? mMatrix.length : mMatrix[0].length;
for(int i=0; i < lineIndex; i++) {
System.out.print("|");
if(i < mMatrix.length) {
for(int j=0; j<mMatrix[i].length; j++) {
System.out.print(mMatrix[i][j] + "\t");
}
}else {
for(int j=0; j<mMatrix[0].length; j++) {
System.out.print(" \t");
}
}
if(Math.round(lineIndex/2) == i) {
System.out.print("|\t*\t|");
}else {
System.out.print("|\t \t|");
}
if(i < pMatrix.length) {
for(int j=0; j<pMatrix[i].length; j++) {
System.out.print(pMatrix[i][j] + "\t");
}
}else {
for(int j=0; j<pMatrix[0].length; j++) {
System.out.print(" \t");
}
}
if(Math.round(lineIndex/2) == i) {
System.out.print("|\t=\t|");
}else {
System.out.print("|\t \t|");
}
if(i < plusResult.length) {
for(int j=0; j<plusResult[i].length; j++) {
System.out.print(plusResult[i][j] + "\t");
}
}
System.out.println("|");
}
}
public static void main(String[] args) {
int[] a = new int[]{1,2,3,4,5,6};
int[] b = new int[]{1,2,3,4,5,6,7,8,9,10};
Matrix m1 = new Matrix(3,2, a);
Matrix m2 = new Matrix(2,5, b);
m1.martixPlus(m2);
}
}
/*输出控制有点儿小问题,自己解决一下吧~
MatrixPlusResult:
|1 2 | |1 2 3 4 5 | |13 16 19 22 25 |
|3 4 | * |6 7 8 9 10 | = |27 34 41 48 55 |
|5 6 | | | |41 52 63 74 85 |
*/
public class Matrix {
public static void main(String[] args) {
int[][] MatrixOne = new int[3][2];
int[][] MatrixTwo = new int[2][1];
System.out.println(MatrixOne.length);
System.out.println(MatrixTwo[0].length);
int[][] MatrixThree = new int[MatrixOne.length][MatrixTwo[0].length];
int i, j;
JOptionPane.showMessageDialog(null,
"input the values of the array one:");
for (i = 0; i < MatrixOne.length; i++) {
for (j = 0; j < MatrixOne[i].length; j++) {
String inputOneString = JOptionPane
.showInputDialog("Enter the number:");
MatrixOne[i][j] = Integer.parseInt(inputOneString);
}
}
JOptionPane.showMessageDialog(null,
"input the values of the array two:");
for (i = 0; i < MatrixTwo.length; i++) {
for (j = 0; j < MatrixTwo[i].length; j++) {
String inputTwoString = JOptionPane
.showInputDialog("Enter the number:");
MatrixTwo[i][j] = Integer.parseInt(inputTwoString);
}
}
MatrixThree = multiplyMatrix(MatrixOne, MatrixTwo);
String output = "The nultiply matrix is";
// for (i = 0; i < MatrixOne.length; i++) {
// for (j = 0; j < MatrixTwo[0].length; j++) {
// if ((i + 1) % MatrixOne.length == 0)
// output += MatrixThree[i][j] + "\n";
// else
// output += MatrixThree[i][j] + " ";
// }
// }
for(int a[]:MatrixThree){
for(int aa:a){
output+=aa+" ";
}
output+="\n";
}
JOptionPane.showMessageDialog(null, output);
}
public static int[][] multiplyMatrix(int[][] a, int[][] b) {
int[][] c = new int[a.length][b[0].length];
int i, j, k;
for (i = 0; i < a.length; i++) {
for (j = 0; j < b[0].length; j++) {
for (k = 0; k < a[0].length; k++) {
c[i][j] = c[i][j] + a[i][k] * b[k][j];
}
}
}
return c;
}
}