机器学习(07天,矩阵元素相加)

机器学习(07天,矩阵元素相加),第1张

机器学习(07天,矩阵元素相加) 代码方面         矩阵元素相加:
package First;
import java.util.Arrays;

public class first {
	public static void main(String[] args)
	{
		matrixElementSumTest();

		matrixAdditionTest();
	}
	public static int matrixElementSum(int[][] paraMatrix) {
		int resultSum = 0;
		for (int i = 0; i < paraMatrix.length; i++) {
			for (int j = 0; j < paraMatrix[0].length; j++) {
				resultSum += paraMatrix[i][j];
			}
		}

		return resultSum;
	}
	
	public static void matrixElementSumTest() 
	{
		int[][] tempMatrix = new int[3][4];
		for (int i = 0; i < tempMatrix.length; i++) 
		{
			for (int j = 0; j < tempMatrix[0].length; j++) 
			{
				tempMatrix[i][j] = i * 10 + j;
			}
		}

		System.out.println("The matrix is: rn" + Arrays.deepToString(tempMatrix));
		System.out.println("The matrix element sum is: " + matrixElementSum(tempMatrix) + "rn");
	}

	public static int[][] matrixAddition(int[][] paraMatrix1, int[][] paraMatrix2) 
	{
		int[][] resultMatrix = new int[paraMatrix1.length][paraMatrix1[0].length];

		for (int i = 0; i < paraMatrix1.length; i++)
		{
			for (int j = 0; j < paraMatrix1[0].length; j++) 
			{
				resultMatrix[i][j] = paraMatrix1[i][j] + paraMatrix2[i][j];
			}
		}

		return resultMatrix;
	}
	
	public static void matrixAdditionTest()
	{
		int[][] tempMatrix = new int[3][4];
		for (int i = 0; i < tempMatrix.length; i++)
		{
			for (int j = 0; j < tempMatrix[0].length; j++) 
			{
				tempMatrix[i][j] = i * 10 + j;
			}
		}

		System.out.println("The matrix is: rn" + Arrays.deepToString(tempMatrix));
		int[][] tempNewMatrix = matrixAddition(tempMatrix, tempMatrix);
		System.out.println("The new matrix is: rn" + Arrays.deepToString(tempNewMatrix));
	}

}
单词方面

 

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5660077.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-16
下一篇 2022-12-16

发表评论

登录后才能评论

评论列表(0条)

保存