Java 找到矩阵的标量乘法
在Java中,数组是一个对象。它是一种非基本数据类型,可以存储相似数据类型的值。在Java中,矩阵实际上就是一个多维数组,它表示多行和列。
标量乘法实际上就是矩阵与一个实数的乘法。这种乘法的结果是一个矩阵。
根据问题描述,我们需要找到矩阵的标量乘法。
让我们深入探讨一下这篇文章,了解如何使用Java编程语言来实现。
展示一些实例
实例-1
Suppose the original matrix is {
{10, 20, 30},
{40, 50, 60},
{70, 80, 90}
}
标量值:5
Resultant Matrix −
50 100 150
200 250 300
350 400 450
实例-2
Suppose the original matrix is {
{10, 20, 30, 40},
{40, 50, 60, 20},
{70, 80, 90, 90}
}
标量值:12
Resultant Matrix −
120 240 360
480 600 720
840 960 1080
实例-3
Suppose the original matrix is {
{12, 123, 12131},
{11, 67, 896},
{233, 234, 678}
}
标量值:3
Resultant Matrix −
36 369 36393
33 201 2688
699 702 2034
步骤
步骤 1 - 声明并初始化一个整型多维数组。
步骤 2 - 声明另一个空的整型多维数组来存储结果矩阵的元素。
步骤 3 - 初始化一个嵌套的循环来将标量值与给定的矩阵元素相乘,并将这些乘积值存储在空矩阵中。
步骤 4 - 将结果矩阵打印输出。
语法
要获取数组的长度(即数组中元素的数量),可以使用数组的内置属性,即 length 。
下面是其语法的参考:
array.length;
其中,“array”是指数组引用。
多种方法
我们提供了不同的方法来解决问题。
- 通过使用矩阵的静态初始化
-
通过使用用户自定义方法
让我们逐个查看程序及其输出。
方法1:通过使用矩阵的静态初始化
在这种方法中,数组元素将在程序中进行初始化。然后根据算法计算标量乘法。
示例
public class Main {
public static void main(String[] args) {
//declare and initialize an integer type multi- dimensional array
int inputMatrix[][] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
//declare a variable to store scalar value
int s = 2;
//declare a variable to store the given matrix length value
int len = inputMatrix.length;
System.out.println("Given Matrix: ");
for (int i = 0; i < len; i++){
for (int j = 0; j < len; j++){
System.out.print(inputMatrix[i][j] + " ");
}
System.out.println();
}
System.out.println("The scalar value is: " + s);
//declare an empty integer type multi- dimensional array
int temp[][] = new int[len][len];
//initiate the loop to calculate the scalar Multiplication
for (int i = 0; i < len; i++) {
for (int j = 0; j < len; j++){
temp[i][j] = inputMatrix[i][j] * s;
}
}
System.out.println("The scalar multiplication of given Matrix is: ");
//initiate the loop to print the resultant matrix
for (int i = 0; i < len; i++){
for (int j = 0; j < len; j++){
System.out.print(temp[i][j] + " ");
}
System.out.println();
}
}
}
输出
Given Matrix:
1 2 3
4 5 6
7 8 9
The scalar value is: 2
The scalar multiplication of given Matrix is:
2 4 6
8 10 12
14 16 18
方法2:使用矩阵的动态初始化
在这个方法中,数组元素将在程序中进行初始化。然后通过将该数组作为参数调用一个用户定义的方法。在方法中,根据算法计算矩阵的标量乘法。
示例
public class Main {
public static void main(String[] args) {
//declare an integer type multi- dimensional array and add some random values
int matrix[][] = {{11, 22, 34}, {42, 25, 61}, {72, 83, 94}};
//declare a variable to store scalar value
int scalar = 10;
//call the user-defined method
scalarMultiplication(matrix,scalar);
}
//user-defined method to calculate the scalar Multiplication
public static void scalarMultiplication(int[][] inputMatrix, int s) {
//declare a variable to store the given matrix length value
int len = inputMatrix.length;
System.out.println("Given Matrix: ");
for (int i = 0; i < len; i++){
for (int j = 0; j < len; j++){
System.out.print(inputMatrix[i][j] + " ");
}
System.out.println();
}
System.out.println("The scalar value is: " + s);
//declare an empty integer type multi- dimensional array
int temp[][] = new int[len][len];
//initiate the loop to calculate the scalar Multiplication
for (int i = 0; i < len; i++) {
for (int j = 0; j < len; j++){
temp[i][j] = inputMatrix[i][j] * s;
}
}
//output screen
System.out.println("The scalar multiplication of given Matrix is: ");
//initiate the loop to print the resultant matrix
for (int i = 0; i < len; i++){
for (int j = 0; j <len; j++){
System.out.print(temp[i][j] + " ");
}
System.out.println();
}
}
}
输出
Given Matrix:
11 22 34
42 25 61
72 83 94
The scalar value is: 10
The scalar multiplication of given Matrix is:
110 220 340
420 250 610
720 830 940
在本文中,我们通过使用Java编程语言探索了不同的方法来找到矩阵的标量乘法。