在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 - 初始化嵌套for循环,将标量值与给定矩阵元素相乘,并将这些乘积值存储到空矩阵中。
步骤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编程语言查找矩阵标量乘法的不同方法。
广告
数据结构
网络
关系数据库管理系统(RDBMS)
操作系统
Java
iOS
HTML
CSS
Android
Python
C语言编程
C++
C#
MongoDB
MySQL
Javascript
PHP