首頁 > Java > java教程 > 主體

Java 中的矩陣乘法

WBOY
發布: 2024-08-30 16:27:31
原創
1091 人瀏覽過

Java中的矩陣儲存在陣列中。存在一維數組和二維數組,它們以矩陣形式儲存值,這些維度稱為數組。一維數組中只儲存一維的數字,而在二維數組中,數字以行和列的形式儲存。在 Java 程式語言中,矩陣可用於對數字進行加法、減法和乘法。矩陣乘法是 Java 程式設計方法中最複雜的任務之一。在本文中,我們必須在 Java 中執行矩陣乘法,並展示如何將兩個矩陣相乘並提供合理的輸出。

開始您的免費軟體開發課程

網頁開發、程式語言、軟體測試及其他

一般方法

Java 程式語言中的矩陣乘法以非常簡單的方式執行。首先,我們輸入第一個二維數組中的數字,然後輸入第二個二維數組中元素的數字。數字按行添加,這意味著創建第一行,然後創建第二行中的數字,依此類推。然後以類似的方式建立第二個矩陣,然後我們開始將矩陣中的數字相乘。

Java 中矩陣乘法的範例

下面是矩陣乘法的例子

範例#1

在編碼範例中,我們看到如何按行輸入兩個矩陣,然後進行矩陣乘法。兩個矩陣相乘的程式碼如下所示。聲明了三個數組。第一個和第二個矩陣的乘積顯示在第三個矩陣內。然後矩陣顯示為輸出,它是數組中兩個矩陣的乘積。

代碼:

import java.util.Scanner;
public class MatixMultiplication
{
public static void main(String args[])
{
int n;
Scanner input = new Scanner(System.in);
System.out.println("Enter the number of rows and columns of the matrices. They must be equal.");
n = input.nextInt();
int[][] a = new int[n][n];
int[][] b = new int[n][n];
int[][] c = new int[n][n];
System.out.println("Enter the numbers of the first matrix. Numbers will be added row wise \n");
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
a[i][j] = input.nextInt();
}
}
System.out.println("Enter the numbers of the 2nd matrix. Numbers will be added row wise.  \n");
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
b[i][j] = input.nextInt();
}
}
System.out.println("Generating the multiplication of matrices.....");
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
for (int k = 0; k < n; k++)
{
c[i][j] = c[i][j] + a[i][k] * b[k][j];
}
}
}
System.out.println("The product of the matrices is shown as below");
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
System.out.print(c[i][j] + " ");
}
System.out.println();
}
input.close();
}
}
登入後複製

顯示了 2*2 矩陣的輸出。第一個矩陣由 { 1,2

元素組成

3,4}

第二個矩陣也包含相同的元素。在樣本輸出中,我們注意到矩陣和樣本輸出的乘法。矩陣的元素以非常好的方式產生。產生的輸出

{1,2                          {1,2              { 7, 10

3,4}        *                3,4}            15, 22}

輸出:

Java 中的矩陣乘法

範例#2

在編碼範例 2 中,我們有相同的程序,但現在我們使用 3 維數組進行乘法。我們現在使用 3 * 3 矩陣乘法並在另一個 3 維數組中顯示輸出。

代碼:

import java.util.Scanner;
public class Matix
{
public static void main(String args[])
{
int n;
Scanner input = new Scanner(System.in);
System.out.println("Enter the number of rows and columns of the matrices. They must be equal.");
n = input.nextInt();
int[][] a = new int[n][n];
int[][] b = new int[n][n];
int[][] c = new int[n][n];
System.out.println("Enter the numbers of the first matrix. Numbers will be added row wise \n");
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
a[i][j] = input.nextInt();
}
}
System.out.println("Enter the numbers of the 2nd matrix. Numbers will be added row wise.  \n");
for (int z = 0; z < n; z++)
{
for (int k = 0; k < n; k++)
{
b[z][k] = input.nextInt();
}
}
System.out.println("Generating the multiplication of matrices.....");
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
for (int k = 0; k < n; k++)
{
c[i][j] = c[i][j] + a[i][k] * b[k][j];
}
}
}
System.out.println("The product of the matrices is shown as below");
for (int k = 0; k < n; k++)
{
for (int l = 0; l < n; l++)
{
System.out.print(c[k][l] + " ");
}
System.out.println();
}
input.close();
}
}
登入後複製

從第二個範例程式碼中,我們列印兩個 3 * 3 矩陣。第一個矩陣是 {1,1,1

1,1,1

1,1,1}

第二個矩陣也是一樣的。矩陣乘法透過以下方式產生

{1,1,1                          {1,1,1                         {3,3,3

1,1,1                *         1,1,1           =              3,3,3

1,1,1}                          1,1,1}                        3,3,3}

輸出:

Java 中的矩陣乘法

結論

在本文中,我們看到了 2 * 2 矩陣和 3 * 3 矩陣的乘法,並且輸出以非常漂亮的方式顯示。輸出已明確給出。使用矩陣乘法,我們也可以建立 4*4 矩陣乘法。在程序的第一步中會詢問基礎。我們也可以建立 5 * 5、6 * 6 矩陣。越是基礎越是程式的複雜度。

但是,簡單的矩陣乘法在計算以 X 軸、Y 軸或 Z 軸為反射軸的點的反射時非常有用。這些簡單的概念用於座標幾何,並用於幾何應用的數學建模。

以上是Java 中的矩陣乘法的詳細內容。更多資訊請關注PHP中文網其他相關文章!

相關標籤:
來源:php
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!