public class Matrix {
成都创新互联公司专注为客户提供全方位的互联网综合服务,包含不限于网站制作、网站建设、临江网络推广、小程序设计、临江网络营销、临江企业策划、临江品牌公关、搜索引擎seo、人物专访、企业宣传片、企业代运营等,从售前售中售后,我们都将竭诚为您服务,您的肯定,是我们最大的嘉奖;成都创新互联公司为所有大学生创业者提供临江建站搭建服务,24小时服务热线:18982081108,官方网址:www.cdcxhl.com
// 表示行和列
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();
}
}
为了经验我也是蛮拼的了
/**
* 矩阵:由 m × n 个数Aij排成的m行n列的数表称为m行n列的矩阵,简称m × n矩阵
* 说白了就是一个二维数组,下面的程序用整形作为数据类型,其他类型运算大同小异
*
*/
public class MatrixUtils {
/**
* 矩阵运算:加(减法与之类似)
*/
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!");
}
//矩阵加减要求两个矩阵类型一致,即行列数相同
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;
}
/**
* 矩阵运算:乘法,没找到除法的运算规则
*/
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!");
}
//两个矩阵的乘法仅当第一个矩阵A的列数和另一个矩阵B的行数相等时才能定义。如A是m×n矩阵和B是n×p矩阵,它们的乘积C是一个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;
}
}
import java.util.Scanner;
public class Matrix {
public double[][] create() {
Scanner sc = new Scanner(System.in) ;
System.out.print("请输入矩阵的行高:");
int a = sc.nextInt() ;
System.out.print("请输入矩阵的列宽:");
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("请输入元素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("输入的维数不匹配,不能进行运算");
}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();
}
}
}
测试类:
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("创建第一个数组:") ;
double[][] x = m.create() ;
m.print(x) ; //用来验证输入的是否和你一样的,没啥作用
System.out.println("创建第二个数组:");
double[][] y = m.create() ;
m.print(y) ; //用来验证输入的是否和你一样的,没啥作用
double[][] result = m.multiply(x, y) ;
if(result == null){
return ; //如果输入的矩阵不能运算就不输出结果了。
}
m.print(result) ;
}
}
Java是一门面向对象编程语言,不仅吸收了C++语言的各种优点,还摒弃了C++里难以理解的多继承、指针等概念,因此Java语言具有功能强大和简单易用两个特征。Java语言作为静态面向对象编程语言的代表,极好地实现了面向对象理论,允许程序员以优雅的思维方式进行复杂的编程。 Java具有简单性、面向对象、分布式、健壮性、安全性、平台独立与可移植性、多线程、动态性等特点。Java可以编写桌面应用程序、Web应用程序、分布式系统和嵌入式系统应用程序等。
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();
}
}
import java.util.Scanner;
public class Matrix {
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println("请输入n*n数组,n=");
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int[][] a = new int[n][n];//定义n*n数组
int result = 0;
for(int i=0; in; i++){
for(int j=0; jn; j++){
System.out.println("请输入第"+i+"行,第"+j+"列元素:");
a[i][j]=sc.nextInt();
}
result +=a[i][i];
}
System.out.println("对角线元素和为:"+result);
}
}