【矩阵运算和Java二维数组对矩阵常见运算的实现】

0.前言

本篇文章主要总结了矩阵的常见运算,以及Java中利用二维数组进行运算的实现

1.矩阵运算

1.矩阵加法

  1. 运算前提:多个矩阵的行数和列数相同,我们称它们为同型矩阵,只有是同型矩阵才能进行加减运算。
  2. 运算规则:两个矩阵相同位置的元素进行相加。

[ 1 2 3 4 5 6 ] + [ 1 2 3 4 5 6 ] = [ 2 4 6 8 10 12 ] \left[ \begin{matrix} 1 & 2 & 3 \\ 4 & 5 & 6 \\ \end{matrix} \right] + \left[ \begin{matrix} 1 & 2 & 3 \\ 4 & 5 & 6 \\ \end{matrix} \right] = \left[ \begin{matrix} 2 & 4 & 6 \\ 8 & 10 & 12 \\ \end{matrix} \right] [142536]+[142536]=[28410612]

2.矩阵减法

  1. 运算规则:同矩阵加法,对应元素相减即可。

3.矩阵乘法

  1. 运算前提:两个矩阵的乘法仅当第一个矩阵的列数和另一个矩阵的行数相等。
  2. 运算规则:两个矩阵,第一个矩阵的行和第二个矩阵的列对应元素相乘再相加,m 行 n 列的矩阵 * n 行 k 列的矩阵,得到一个 m 行 k 列的矩阵。
    [ 1 2 3 4 5 6 ] ∗ [ 1 2 3 4 5 6 ] = [ 1 ∗ 1 + 2 ∗ 3 + 3 ∗ 5 = 22 1 ∗ 2 + 2 ∗ 4 + 3 ∗ 6 = 28 4 ∗ 1 + 3 ∗ 5 + 6 ∗ 5 = 49 4 ∗ 2 + 5 ∗ 4 + 6 ∗ 6 = 64 ] = [ 22 28 49 64 ] \left[ \begin{matrix} 1 & 2 & 3 \\ 4 & 5 & 6 \\ \end{matrix} \right] * \left[ \begin{matrix} 1 & 2 \\ 3 & 4 \\ 5 & 6 \\ \end{matrix} \right] = \left[ \begin{matrix} 1*1 + 2*3+3*5 =22& 1* 2 + 2* 4 + 3* 6=28\\ 4 * 1 + 3 * 5 + 6* 5 = 49 & 4*2+5*4+6*6= 64\\ \end{matrix} \right] = \left[ \begin{matrix} 22 & 28 \\ 49 & 64 \\ \end{matrix} \right] [142536]135246=[11+23+35=2241+35+65=4912+24+36=2842+54+66=64]=[22492864]

4.矩阵转置

  1. 运算规则:将矩阵的第 i 行 第 j 列 转换为 第 j 行 第 i 列
    [ 1 2 3 4 5 6 ] T = [ 1 4 2 5 3 6 ] \left[ \begin{matrix} 1 & 2 & 3 \\ 4 & 5 & 6 \\ \end{matrix} \right] ^T = \left[ \begin{matrix} 1 & 4 \\ 2 & 5 \\ 3 & 6 \\ \end{matrix} \right] [142536]T=123456

2.Java实现

public class Matrix {
    public static void main(String[] args) {
        int[][] a = {{1, 2, 3}, {4, 5, 6}};
        int[][] b = {{1, 2}, {3, 4}, {5, 6}};

        System.out.println(Arrays.deepToString(randomMatrix(3, 2)));
    }

    /**
     * 生成随机矩阵 m行n列
     * @return
     */
    public static int[][] randomMatrix(int m, int n){
        int[][] ans = new int[m][n];
        // 生成随机矩阵
        Random rand = new Random();
        for (int i = 0; i < m; i++)
            for (int j = 0; j < n; j++) {
                ans[i][j] = rand.nextInt(10);
            }
        return ans;
    }


    /**
     * 矩阵加减法
     */
    public static int[][] addOrSub(int[][] a, int[][] b, boolean isPlus) {
        // 不满足加减法条件
        if (a.length != b.length || a[0].length != b[0].length) return new int[0][0];

        for (int i = 0; i < a.length; i++)
            for (int j = 0; j < a[0].length; j++) {
                if (isPlus)
                    a[i][j] += b[i][j];
                else
                    a[i][j] -= b[i][j];
            }
        return a;
    }

    /**
     * 矩阵乘法
     */
    public static int[][] multiply(int[][] a, int[][] b) {
        int m = a.length;
        int n = a[0].length;
        int n1 = b.length;
        int k = b[0].length;
        // 不满足乘法条件
        if (n != n1) return new int[0][0];

        // 构建一个 m 行 k 列的矩阵
        int[][] ans = new int[m][k];
        for (int i = 0; i < m; i++) {
            for (int j = 0; j < k; j++) {
                int temp = 0;
                for (int l = 0; l < n; l++) {
                    temp += a[i][l] * b[l][j];
                }
                ans[i][j] = temp;
            }
        }
        return ans;
    }

    /**
     * 转置
     */
    public static int[][] zhi(int[][] a) {
        int m = a.length;
        int n = a[0].length;
        int[][] ans = new int[n][m];
        for (int i = 0; i < n; i++)
            for (int j = 0; j < m; j++)
                ans[i][j] = a[j][i];
        return ans;
    }
}

  • 3
    点赞
  • 30
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是基于二维数组类的矩阵运算方法的实现代码,包括矩阵加法矩阵减法矩阵乘法和矩阵转置: ```java public class Matrix { private int rows; // 矩阵行数 private int cols; // 矩阵列数 private double[][] data; // 矩阵数据 public Matrix(int rows, int cols) { this.rows = rows; this.cols = cols; this.data = new double[rows][cols]; } // 矩阵加法 public Matrix add(Matrix other) { if (this.rows != other.rows || this.cols != other.cols) { throw new IllegalArgumentException("Matrix sizes do not match."); } Matrix result = new Matrix(rows, cols); for (int i = 0; i < rows; i++) { for (int j = 0; j < cols; j++) { result.data[i][j] = this.data[i][j] + other.data[i][j]; } } return result; } // 矩阵减法 public Matrix subtract(Matrix other) { if (this.rows != other.rows || this.cols != other.cols) { throw new IllegalArgumentException("Matrix sizes do not match."); } Matrix result = new Matrix(rows, cols); for (int i = 0; i < rows; i++) { for (int j = 0; j < cols; j++) { result.data[i][j] = this.data[i][j] - other.data[i][j]; } } return result; } // 矩阵乘法 public Matrix multiply(Matrix other) { if (this.cols != other.rows) { throw new IllegalArgumentException("Invalid matrix sizes for multiplication."); } Matrix result = new Matrix(this.rows, other.cols); for (int i = 0; i < this.rows; i++) { for (int j = 0; j < other.cols; j++) { double sum = 0; for (int k = 0; k < this.cols; k++) { sum += this.data[i][k] * other.data[k][j]; } result.data[i][j] = sum; } } return result; } // 矩阵转置 public Matrix transpose() { Matrix result = new Matrix(cols, rows); for (int i = 0; i < rows; i++) { for (int j = 0; j < cols; j++) { result.data[j][i] = this.data[i][j]; } } return result; } } ``` 以上是一个简单的实现。需要注意的是,矩阵运算需要满足一定的条件,如加法减法矩阵大小必须相同,乘法的左矩阵列数必须等于右矩阵行数。在实际应用中,还需要考虑更多的情况。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值