如何在MATLAB中查詢兩個矩陣之間的相似度百分比?


眾所周知,MATLAB 是一款功能強大的工具,可用於對矩陣執行各種操作。其中一項操作就是查詢兩個矩陣之間的相似度百分比。在數字影像處理、資料分析、機器學習等各種應用中,查詢兩個矩陣之間的相似度百分比至關重要。

本教程將幫助您瞭解使用 MATLAB 查詢兩個矩陣之間相似度百分比的分步過程。

什麼是兩個矩陣之間的相似度百分比?

衡量兩個矩陣在某些方面相關程度的指標稱為“兩個矩陣之間的相似度”。兩個矩陣之間的相似度百分比只不過是衡量它們在元素方面有多麼相似。

可以使用以下數學公式來計算兩個矩陣之間的相似度百分比。

$$\mathrm{\% \:of \:Similarity =\frac{相同元素的數量}{元素的總數}× 100}$$

也可以寫成:

$$\mathrm{\% \:of \:Similarity =\frac{相同元素的數量}{行數 × 列數}× 100}$$

相似度百分比用於量化兩個矩陣之間接近程度的度量。

現在讓我們瞭解查詢兩個矩陣之間相似度百分比的分步過程。

在MATLAB中查詢兩個矩陣之間的相似度百分比

此處解釋了使用 MATLAB 查詢兩個矩陣之間相似度百分比的分步過程。

  • 步驟 (1) − 建立兩個大小相同的矩陣,您要在其之間查詢相似度。

  • 步驟 (2) − 使用“==”運算子查詢兩個矩陣之間的相等性。將結果布林值儲存在一個變數中。

  • 步驟 (3) − 使用上面給出的公式計算兩個矩陣之間的相似度百分比。

MATLAB 提供了一個簡單的三步過程來查詢兩個矩陣之間的相似度百分比。

示例

讓我們來看一些示例,瞭解如何在 MATLAB 中計算兩個矩陣之間的相似度百分比。

% MATLAB program to find percentage of similarity between two square matrices
% Create two sample square matrices
A = [1 2 3 4; 1 4 2 3; 4 5 6 8; 2 3 7 6];
B = [1 4 3 2; 4 2 1 3; 1 5 7 8; 2 1 7 3];

% Specify the size of matrices
rows = 4;
cols = 4;

% Find the equality between matrices
same_elements = A == B;

% Count the number same elements
count_same_elements = sum(same_elements(:));

% Compute the percentage of similarity between two matrices
per_sim = count_same_elements / (rows * cols) * 100;

% Display the original matrices and percentage of similarity between them
disp('Matrix A is:');
disp(A);
disp('Matrix B is:');
disp(B);
disp('Percentage of Similarity between Matrices A and B is:');
disp(per_sim);

輸出

執行此程式碼時,它將產生以下輸出

Matrix A is:
     1     2     3     4
     1     4     2     3
     4     5     6     8
     2     3     7     6

Matrix B is:
     1     4     3     2
     4     2     1     3
     1     5     7     8
     2     1     7     3

Percentage of Similarity between Matrices A and B is:
   43.7500

程式碼解釋

在此示例中,我們首先宣告兩個矩陣,其中一些元素相同。接下來,我們指定兩個矩陣的大小。重要的是,為了查詢它們之間的相似度,兩個矩陣的大小必須相同。

然後,我們使用“==”運算子計算兩個矩陣中相同元素的數量,並將布林結果儲存在變數“same_elements”中,該變數是一個矩陣,當矩陣“A”和“B”的元素相同時,每個元素為“1”,不相同時為“0”。

之後,我們使用“sum”函式計算矩陣“same_elements”中“1”的數量。這基本上是矩陣 A 和 B 中相同元素的數量。

然後,我們使用公式查詢兩個矩陣之間的相似度百分比。

最後,我們使用“disp”函式顯示輸入矩陣和矩陣 A 和 B 之間的相似度百分比。

示例

讓我們考慮另一個示例來計算兩個矩形矩陣之間的相似度百分比。

% MATLAB program to find percentage of similarity between two rectangular matrices
% Create two sample rectangular matrices
A = [1 2 3 4; 1 4 2 3; 4 5 6 8];
B = [1 4 3 2; 4 2 1 3; 1 5 7 8];

% Specify the size of matrices
rows = 3;
cols = 4;

% Find the equality between matrices
same_elements = A == B;

% Count the number same elements
count_same_elements = sum(same_elements(:));

% Compute the percentage of similarity between two matrices
per_sim = count_same_elements / (rows * cols) * 100;

% Display the original matrices and percentage of similarity between them
disp('Matrix A is:');
disp(A);
disp('Matrix B is:');
disp(B);
disp('Percentage of Similarity between Matrices A and B is:');
disp(per_sim);

輸出

執行此程式碼時,它將產生以下輸出

Matrix A is:
     1     2     3     4
     1     4     2     3
     4     5     6     8

Matrix B is:
     1     4     3     2
     4     2     1     3
     1     5     7     8

Percentage of Similarity between Matrices A and B is:
   41.6667

程式碼解釋

此 MATLAB 程式碼的實現和執行與前一個相同。唯一的區別是,在此程式碼中,我們使用了兩個矩形矩陣而不是方陣。

結論

這就是使用 MATLAB 查詢兩個矩陣之間相似度百分比的全部內容。在本教程中,我解釋了分步過程和示例,以演示在 MATLAB 中計算兩個矩陣之間相似度百分比的過程。您可以使用您自己的矩陣嘗試上述程式碼。

更新於:2023年10月5日

瀏覽量:137

啟動您的職業生涯

完成課程獲得認證

開始學習
廣告
© . All rights reserved.