数据结构之矩阵实现

  • Post author:
  • Post category:其他



(1)矩阵的概念:




(2)代码实现:


import java.util.Random;

/**
 * @date 2017-5-30
 * @author liuffei
 * @description 创建矩阵,实现矩阵加法,乘法,转置等运算
 */
public class Matrix {
   
	private int matrix[][] = null;
	
	public Matrix(){}
	
	/**
	 * 构造函数,创建row行col列的矩阵
	 * @param row 行数
	 * @param col 列数
	 */
	public Matrix(int row,int col){
		matrix = new int[row][col];
	}
	 
	/**
	 * 给创建的矩阵赋值
	 * @param row 行数
	 * @param col 列数
	 * @param originalMatrix 
	 */
	public Matrix(int row,int col,int[][] originalMatrix){
		matrix = new int[row][col];
		for(int i = 0;i < row;i++){
			for(int j = 0;j < col;j++){
				matrix[i][j] = originalMatrix[i][j];
			}
		}
	}
	
	/**
	 * 当前矩阵加法
	 * @param addMatrix 相加的矩阵
	 */
	public  void add(int[][] addMatrix){
		if(null == matrix || null == addMatrix){
			return;
		}else{
			if(matrix.length != addMatrix.length && matrix[0].length != addMatrix[0].length){
				return;
			}else{
				for(int i = 0;i < matrix.length;i++){
					for(int j = 0;j < matrix[i].length;j++){
						matrix[i][j] += addMatrix[i][j];
					}
				}
			}
		}
	}
	
	/**
	 * 当前矩阵减法
	 * @param addMatrix 相减的矩阵
	 */
	public void sub(int[][] subMatrix){
		if(null == matrix || null == subMatrix){
			return;
		}else{
			if(matrix.length != subMatrix.length && matrix[0].length != subMatrix[0].length){
				return;
			}else{
				for(int i = 0;i < matrix.length;i++){
					for(int j = 0;j < matrix[i].length;j++){
						matrix[i][j] -= subMatrix[i][j];
					}
				}
			}
		}
	}
	
	/**
	 * 当前矩阵乘法
	 * @param addMatrix 相乘的矩阵
	 */
	public int[][] mul(int[][] mulMatrix){
		int resultMatrix[][] = new int[matrix.length][matrix.length];
		if(null == matrix || null == mulMatrix){
			return null;
		}else{
			//要求:matrix列数和mulMatrix的行数相等
			if(matrix[0].length != mulMatrix.length){
				return null;
			}else{
				for(int i = 0;i < matrix.length;i++){
					for(int k = 0;k < mulMatrix[0].length;k++){
						for(int j = 0;j < matrix[i].length;j++){
							resultMatrix[i][k] += matrix[i][j] * mulMatrix[j][k];
						}
					}
				}
			}
		}
		return resultMatrix;
	}
	
	/**
	 * 当前矩阵转置
	 * @return 转置后的矩阵
	 */
	public int[][] reverse(){
		int reverseMatrix[][] = new int[matrix[0].length][matrix.length];
		for(int i = 0;i < matrix.length;i++){
			for(int j = 0;j < matrix[i].length;j++){
				reverseMatrix[j][i] = matrix[i][j];
			}
		}
		return reverseMatrix;
	}
	
	 /**
     * 打印矩阵
     * @param matrix 矩阵
     */
    public  void printMatrix(){
    	int row = matrix.length;
    	for(int i = 0;i < row;i++){
    		for(int j = 0;j < matrix[i].length;j++){
    			System.out.print(matrix[i][j] + " ");
    		}
    		System.out.println("");
    	}
    }
    
    public static void printMatrix(int[][] matrix){
    	int row = matrix.length;
    	for(int i = 0;i < row;i++){
    		for(int j = 0;j < matrix[i].length;j++){
    			System.out.print(matrix[i][j] + " ");
    		}
    		System.out.println("");
    	}
    }
    
    public static int[][] randMatrix(int row,int col){
    	int[][] originalMatrix = new int[row][col];
    	Random rand = new Random();
    	for(int i = 0;i < originalMatrix.length;i++){
    		for(int j = 0;j < originalMatrix[i].length;j++){
    			originalMatrix[i][j] = rand.nextInt(10);
    		}
    	}
    	return originalMatrix;
    }
    
    public static void main(String args[]){
    	int originalMatrix[][] = randMatrix(4,3);
    	int mulMatrix[][] = randMatrix(3,4);
    	Matrix matrix = new Matrix(4,3,originalMatrix);
    	System.out.println("初始矩阵是:");
    	matrix.printMatrix();
    	matrix.add(originalMatrix);
    	System.out.println("相加后的矩阵是:");
    	matrix.printMatrix();
    	matrix.sub(originalMatrix);
    	System.out.println("相减后的矩阵是:");
    	matrix.printMatrix();
    	int resultMatrix[][] = matrix.reverse();
    	System.out.println("转置后的矩阵是:");
    	printMatrix(resultMatrix);
    	int result2Matrix[][] = matrix.mul(resultMatrix);
    	System.out.println("相乘后的矩阵是:");
    	printMatrix(result2Matrix);
    }
}


(3)实现结果:






版权声明:本文为u012734723原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。