在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