跳轉至內容

MATLAB 程式設計/向量和矩陣/矩陣運算

來自華夏公益教科書,開放的書籍,為開放的世界

基本矩陣運算

[編輯 | 編輯原始碼]

加法和減法

[編輯 | 編輯原始碼]

如果兩個矩陣的行數和列數相同,我們可以對它們進行加法和減法運算。

以下是一些示例

  >> a = [7,9,4;6,2,5]
  a =
     7     9     4
     6     2     5

  >> b=[2,0,1;4,7,3]
  b =
     2     0     1
     4     7     3

  >> % Addition of a and b matrices
  a+b
  ans =
     9     9     5
    10     9     8

  >> % Subtraction of a and b matrices
  a-b
  ans =
     5     9     3
     2    -5     2

矩陣乘法

[編輯 | 編輯原始碼]

對於矩陣乘法,有兩種方法可以進行運算。

矩陣乘法

(i) 矩陣乘法(使用符號*mtimes

要求是第一個矩陣的列數必須等於第二個矩陣的行數。

如右側示例所示,矩陣 A 有 3 X 2,矩陣 B 有 2 X 3

因此,2 X 3 <-> 3 X 2,因此滿足上述要求。

此外,請注意,結果矩陣的大小取決於第一個矩陣的行數第二個矩陣的列數。

矩陣乘法步驟
>> A=[4,2,4;8,3,1]

A =
     4     2     4
     8     3     1

>> B=[3,5;2,8;7,9]

B =
     3     5
     2     8
     7     9

>> mtimes(A,B)

ans =
    44    72
    37    73

>> A*B

ans =

    44    72
    37    73

以下示例顯示瞭如果矩陣維度不匹配會發生什麼。

如所示,矩陣 C 有 5 X 4,矩陣 D 有 3 X 2

5 X 4 <-> 3 X 2,因此無法滿足條件,也無法求解。

>> %Demonstrations what if matrices dimensions are incorrect 
>> C=randi(10,5,4)

C =

     2    10    10     3
     2    10     4     5
     3     5     2     1
     5     5     8     2
     1     4     4    10

>> D=randi(10,3,2)

D =

    10     3
     6     4
     1     9

>> mtimes(C,D)
Error using  * 
Incorrect dimensions for matrix multiplication. Check that the number of columns in the first
matrix matches the number of rows in the second matrix. To perform elementwise
multiplication, use '.*'.

(ii) 點積(注意:只有當兩個矩陣大小相同時才能使用)以上面的示例為例,它不能求解方程,因為上面的矩陣 A 和 B 大小不同

>> A.*B % want to show dot product unable to solve this multiplication issues
Matrix dimensions must agree.

建立隨機整數矩陣

[編輯 | 編輯原始碼]

要生成隨機整數矩陣,可以鍵入以下內容 randi(IMAX,M,N)
注意:IMAX 是最大整數(從 1 開始),M * N 是矩陣

以下是一些示例

>> randi(50,3,4)

ans =
     9    14    42    48
    36     3    35     2
     2     5    16    22

使用上面的矩陣,我們可以對矩陣進行轉置。轉置矩陣通常只是將行轉換為列,將列轉換為行。圖示只是演示了轉置操作。轉置矩陣的應用之一是密碼學。

矩陣轉置


有兩種方法可以實現它。一種是在要轉置的矩陣末尾新增 ',或者使用函式 transpose

回到上面的幻方示例,我們將對其進行轉置。我們可以看到,它沿著對角線轉置看起來像這樣 : \

  >> % transpose matrix c to d
  >> d = c'
  d =
    17    23     4    10    11
    24     5     6    12    18
     1     7    13    19    25
     8    14    20    21     2
    15    16    22     3     9

行列式

[編輯 | 編輯原始碼]

矩陣的行列式是一個特殊的數字,它只針對方陣定義。
行列式用於求解線性方程組並確定矩陣的逆矩陣。

2X2 矩陣的行列式 :

3X3 矩陣的行列式 :

>> A=[6,1,1;4,-2,5;2,8,7]

A =
     6     1     1
     4    -2     5
     2     8     7

>> det(A)
ans =
 -306.0000

逆矩陣

[編輯 | 編輯原始碼]

矩陣的逆矩陣是倒數矩陣,其公式表示為
,其中 adj 代表矩陣的伴隨矩陣。

注意:並非所有矩陣都有逆矩陣,如果它們的行列式等於零。

>>%matrix inversion using manual method

>> M=[2,-1,3;-5,3,1;-3,2,3]

M =

     2    -1     3
    -5     3     1
    -3     2     3

>> %First we find the matrix determinant
>> DM = det(M)

DM =
   -1.0000

>>%Since determinant IS NOT equal to 0, we can find the matrix inverses

>> AM = adjoint(M)
AM =
    7.0000    9.0000  -10.0000
   12.0000   15.0000  -17.0000
   -1.0000   -1.0000    1.0000

>> (1/DM)*AM

ans =
   -7.0000   -9.0000   10.0000
  -12.0000  -15.0000   17.0000
    1.0000    1.0000   -1.0000

%shortcut using function inv which should be same as manual calculation above
>> inv(M)

ans =
   -7.0000   -9.0000   10.0000
  -12.0000  -15.0000   17.0000
    1.0000    1.0000   -1.0000

現實生活中的應用

[編輯 | 編輯原始碼]

矩陣有許多應用,例如


密碼學 

Public key encryption keys
公鑰加密金鑰

矩陣用於加密訊息程式碼。程式設計師使用矩陣來對字母進行編碼或加密。訊息由一系列二進位制數字組成,這些數字使用編碼理論來解決通訊問題。因此,矩陣的概念用於求解此類方程。

要執行此矩陣運算,訊息首先被分解成固定大小的塊,每個塊都表示為一個數字向量。公鑰包含一個矩陣,稱為加密矩陣或公鑰矩陣,用於轉換每個數字向量。然後將得到的轉換後的向量乘以模數模冪,以獲得密文。

% Define the message to encrypt
message = 'I LOVE MATLAB';

% Convert the message to a vector of numbers
message_vec = double(message);

% Define the public key parameters (modulus and exponent)
% Next, we define the public key parameters, which in this case are a modulus of 104729 and an exponent of 65537.
modulus = 104729;
exponent = 65537;

% Define the encryption matrix
% We also define an encryption matrix of size 2x2.
encryption_matrix = [3 5; 7 11];

% Break the message vector into blocks of length 2
block_size = 2;
num_blocks = ceil(length(message_vec) / block_size);
message_blocks = zeros(num_blocks, block_size);
message_blocks(1:length(message_vec)) = reshape(message_vec, [], block_size);

% Apply the encryption matrix to each message block
encrypted_blocks = mod(message_blocks * encryption_matrix, modulus);

% Raise each encrypted block to the exponent
exponentiated_blocks = mod(encrypted_blocks .^ exponent, modulus);

% Convert the encrypted blocks to a single vector of numbers
encrypted_vec = reshape(exponentiated_blocks, [], 1);

% Print out the encrypted message
fprintf('Encrypted message: %s\n', char(encrypted_vec'));


要解密訊息,可以使用私鑰,私鑰包含兩個部分:模數和模某個值的指數的逆。指數的逆用於將密文乘以一個冪,該冪將反轉矩陣變換並恢復原始訊息塊。

% Define the encrypted message
encrypted_message = [16124 4546 84313 99848 16124 4546 84313 40458 16124 4546 84313 40458 32274 40458];

% Define the private key parameters (modulus and inverse exponent)
modulus = 104729;
inverse_exponent = 47027;

% Define the decryption matrix
decryption_matrix = [17 23; 21 29];

% Break the encrypted message vector into blocks of length 2
block_size = 2;
num_blocks = length(encrypted_message) / block_size;
encrypted_blocks = reshape(encrypted_message, block_size, num_blocks)';

% Raise each encrypted block to the inverse of the exponent
decrypted_blocks = mod(encrypted_blocks .^ inverse_exponent, modulus);

% Apply the decryption matrix to each decrypted block
decrypted_blocks = mod(decrypted_blocks * decryption_matrix, modulus);

% Convert the decrypted blocks to a single vector of numbers
decrypted_vec = reshape(decrypted_blocks, [], 1);

% Convert the decrypted vector to a string and print it out
fprintf('Decrypted message: %s\n', char(decrypted_vec'));

影像處理 

矩陣運算在現實生活中的一種應用是影像處理。我們將研究一個比較簡單的例子,例如影像模糊。

例如,我們希望對一張假設為黑白的影像進行模糊處理,這裡展示了一個矩陣的示例,其中每個畫素的值代表一個灰度值,從 1 到 255。

我們將使用矩陣卷積來計算影像中每個畫素周圍 3x3 鄰域的畫素值的平均值。

要應用此濾波器,我們將矩陣在影像的每個畫素上滑動,並執行矩陣乘法。例如,要模糊影像中心的畫素,您將獲取其周圍的 3x3 鄰域,將每個畫素值乘以模糊濾波器矩陣中的對應值,然後將結果相加。最終的值是模糊影像的新畫素值。

我們對影像中的每個畫素重複此過程,最終得到一張看起來更模糊的新影像。

% Define the input matrix
input_matrix = [1 2 3; 8 9 4; 7 6 5];

% Define the blur filter matrix
blur_filter = 1/9 * [1 1 1; 1 1 1; 1 1 1];

% Apply the filter using convolution
blurred_matrix = conv2(input_matrix, blur_filter, 'same');

% Display the original and blurred matrices side by side
disp('Original Matrix:');
disp(input_matrix);
disp('Blurred Matrix:');
disp(blurred_matrix);

電路分析

Multiloop-circuit-example marked

在電氣工程中,基爾霍夫電壓定律 (KVL) 指出,電路中閉合迴路上的所有電壓之和必須等於零。該定律可用於為具有 m 個迴路的電路編寫一組線性方程。這些方程可以排列在一個矩陣中,稱為迴路分支關聯矩陣。

根據這些示例

迴路 M1 : V1 - R1*I1 - R3*I2 = 0

迴路 M2 : V2 - R2*I2 - R3*I1 = 0

[ R1 -R3 0 ] [ I1 ] = [ V1 ]

[-R3 R2+R3 -R2 ] [ I2 ] = [ -V2 ]

[ 0 -R2 1 ] [ V2 ] = [ 0 ]

使用符號數學工具箱,我們可以求解這些方程

% Define the symbolic variables
syms R1 R2 R3 V1 V2 I1 I2

% Define the Kirchhoff Voltage Law equations for each loop
eq1 = V1 - R1*I1 - R3*(I1-I2) == 0;
eq2 = V2 - R2*I2 - R3*(I2-I1) == 0;

% Define the Kirchhoff Current Law equation at node K1 and K2
eq3 = I1 - I2 == 0;

% Solve the system of equations for the currents and voltages
sol = solve(eq1, eq2, eq3, I1, I2, V2);

% Display the results
I1 = double(sol.I1);
I2 = double(sol.I2);
V2 = double(sol.V2);
fprintf('I1 = %.2f A\n', I1);
fprintf('I2 = %.2f A\n', I2);
fprintf('V2 = %.2f V\n', V2);

參考文獻

[編輯 | 編輯原始碼]

[1] [2] [3] [4]

  1. https://web.archive.org/web/20220712153202/https://collegedunia.com/exams/applications-of-determinants-and-matrices-and-solved-examples-mathematics-articleid-2195
  2. https://web.archive.org/web/20220719154910/https://www.embibe.com/exams/inverse-matrix/
  3. https://web.archive.org/web/20220814062118/https://www.theclickreader.com/dot-products-and-matrix-multiplication/
  4. https://web.archive.org/web/20220814062138/https://www.theclickreader.com/matrix-transpose-determinants-and-inverse/
華夏公益教科書