#include
int main()
{
int i ,j
int a[10][10]
for(i=0i<10i++)
for(j=0j<10j++)
{
a[i][j]=0
if(i==(9-j)&&j==(9-i))a[i][j]=1
if(i==j)a[i][j]=1
}
for(i=0i<10i++)
{
for(j=0j<10j++)
{
printf("%i "改橡世如缺,a[i][j])
}
printf("\n")
}
getch()
}
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=0i<a.lengthi++) {
羡轮 for(int j=0j<b[0].lengthj++) {
for(int k=0k<a[0].lengthk++) {
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=0i<mi++)
for(int j=0j<nj++)
a[i][j]=1.0
System.out.println("输入连续的数构成数组b:")
for(int i=0i<ni++)
for(int j=0j<kj++)
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=0i<c.lengthi++) {
for(int j=0j<c[0].lengthj++) {
System.out.printf("%-8.1f",c[i][j]) //保留1位小数
}
System.out.println()
}
} else {
System.out.println("无效")
}
System.out.println()
}
/**
* 矩阵:由 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
}
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)