矩阵乘加减法运算

二维矩阵为例

矩阵1: a(2)b(2)=[A,B],[C,D]

矩阵2:c(2)d(2)= [E,F],[G,H]

乘法操作

矩阵1*矩阵2得:

矩阵3=[A*E+B*G,A*F+B*H],[C*E+D*G,C*F+D*H]

加减法运算操作

同维度矩阵,相对应位进行加减算术

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是Java代码示例: ```java public class Matrix { private int rows; private int cols; private int[][] data; public Matrix(int rows, int cols) { this.rows = rows; this.cols = cols; this.data = new int[rows][cols]; } public int getRows() { return rows; } public int getCols() { return cols; } public int getValue(int row, int col) { return data[row][col]; } public void setValue(int row, int col, int value) { data[row][col] = value; } public Matrix add(Matrix other) { if (this.rows != other.getRows() || this.cols != other.getCols()) { throw new IllegalArgumentException("Matrix dimensions must be equal"); } Matrix result = new Matrix(rows, cols); for (int i = 0; i < rows; i++) { for (int j = 0; j < cols; j++) { result.setValue(i, j, this.getValue(i, j) + other.getValue(i, j)); } } return result; } public Matrix subtract(Matrix other) { if (this.rows != other.getRows() || this.cols != other.getCols()) { throw new IllegalArgumentException("Matrix dimensions must be equal"); } Matrix result = new Matrix(rows, cols); for (int i = 0; i < rows; i++) { for (int j = 0; j < cols; j++) { result.setValue(i, j, this.getValue(i, j) - other.getValue(i, j)); } } return result; } @Override public boolean equals(Object obj) { if (obj == this) { return true; } if (!(obj instanceof Matrix)) { return false; } Matrix other = (Matrix) obj; if (this.rows != other.getRows() || this.cols != other.getCols()) { return false; } for (int i = 0; i < rows; i++) { for (int j = 0; j < cols; j++) { if (this.getValue(i, j) != other.getValue(i, j)) { return false; } } } return true; } @Override public String toString() { StringBuilder sb = new StringBuilder(); for (int i = 0; i < rows; i++) { for (int j = 0; j < cols; j++) { sb.append(this.getValue(i, j)).append(" "); } sb.append("\n"); } return sb.toString(); } } ``` 在这个示例中,我们定义了一个Matrix类,包含以下方法: - 构造函数:创建一个指定行和列的矩阵,并初始化所有元素为0。 - getRows()和getCols()方法:获取矩阵的行数和列数。 - getValue()和setValue()方法:获取和设置矩阵中指定位置的值。 - add()方法:将当前矩阵和另一个矩阵相加,返回结果矩阵。 - subtract()方法:将当前矩阵和另一个矩阵,返回结果矩阵。 - equals()方法:检查两个矩阵是否相等。 - toString()方法:将矩阵转换为字符串,以便输出到控制台。 我们可以使用以下代码来测试Matrix类的功能: ```java Matrix m1 = new Matrix(2, 2); m1.setValue(0, 0, 1); m1.setValue(0, 1, 2); m1.setValue(1, 0, 3); m1.setValue(1, 1, 4); System.out.println("m1:\n" + m1); Matrix m2 = new Matrix(2, 2); m2.setValue(0, 0, 5); m2.setValue(0, 1, 6); m2.setValue(1, 0, 7); m2.setValue(1, 1, 8); System.out.println("m2:\n" + m2); Matrix m3 = m1.add(m2); System.out.println("m1 + m2:\n" + m3); Matrix m4 = m1.subtract(m2); System.out.println("m1 - m2:\n" + m4); System.out.println("m1 equals m2? " + m1.equals(m2)); System.out.println("m1 equals m1? " + m1.equals(m1)); ``` 输出结果如下: ``` m1: 1 2 3 4 m2: 5 6 7 8 m1 + m2: 6 8 10 12 m1 - m2: -4 -4 -4 -4 m1 equals m2? false m1 equals m1? true ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值