十年网站开发经验 + 多家企业客户 + 靠谱的建站团队
量身定制 + 运营维护+专业推广+无忧售后,网站问题一站解决
1、矩阵类
让客户满意是我们工作的目标,不断超越客户的期望值来自于我们对这个行业的热爱。我们立志把好的技术通过有效、简单的方式提供给客户,将通过不懈努力成为客户在信息化领域值得信任、有价值的长期合作伙伴,公司提供的服务项目有:域名注册、网页空间、营销软件、网站建设、田林网站维护、网站推广。
public class Matrix {
/* 矩阵相乘 */
public static double[][] multiplyMatrix(double[][] a,double[][] b){
if(a[0].length != b.length) {
return null;
}
double[][] c=new double[a.length][b[0].length];
for(int i=0;ia.length;i++) {
for(int j=0;jb[0].length;j++) {
for(int k=0;ka[0].length;k++) {
c[i][j] += a[i][k] * b[k][j];
}
}
}
return c;
}
2、测试函数(main函数)
public static void main(String[] args) {
Scanner input=new Scanner(System.in);
int m = n = k = 6;
double [][]a=new double[m][n]; //定义一个m*n的矩阵
double [][]b=new double[n][k]; //定义一个n*k的矩阵
System.out.println("输入连续的数构成数组a:");
for(int i=0;im;i++)
for(int j=0;jn;j++)
a[i][j]=1.0;
System.out.println("输入连续的数构成数组b:");
for(int i=0;in;i++)
for(int j=0;jk;j++)
b[i][j]=1.0;
double [][]c=multiplyMatrix(a, b);
System.out.println("Matrix a:");
printMatrix(a); //打印a
System.out.println("Matrix b:");
printMatrix(b); //打印b
System.out.println("Matrix c:");
printMatrix(c); //打印c
input.close();
}
}
3、打印函数(可以放到main函数所在类中)
public static void printMatrix(double[][] c) {
if (c!=null) {
for(int i=0;ic.length;i++) {
for(int j=0;jc[0].length;j++) {
System.out.printf("%-8.1f",c[i][j]); //保留1位小数;
}
System.out.println();
}
} else {
System.out.println("无效");
}
System.out.println();
}
public class Matrix {
// 表示行和列
private int mRow, mColumn;
// 构造方法
public Matrix(int row, int column) {
mRow = row;
mColumn = column;
}
// 获取0-20随机数
private int random() {
double random1 = Math.random();// 这个Math类的方法可以获取0.0-1.0之间的随机数
double random2 = random1 * 20; // 0.0 - 20.0
return (int) random2;
}
// 创建矩阵
private void createMatrix() {
int totalCount = mRow * mColumn; // 总共有那么多
for (int count = 1; count = totalCount; count++) {
int number = random();// 上面的方法
System.out.print(number 10 ? "0" + number : number); // 输出数字,如果数字小于10,前面加0补全两位
System.out.print(" "); // 分隔符,随便填
if (count % mRow == 0) {
System.out.println(); // 换行
}
}
}
public static void main(String[] args) {
Matrix matrix = new Matrix(3, 5); // 几行几列传进去
matrix.createMatrix();
}
}
为了经验我也是蛮拼的了
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; ithis.mx.length; i++)
for(j=0; ji; 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();
}
}