在 MATLAB 中將陣列轉換為列向量
在 MATLAB 中,陣列是一種用於將元素儲存在連續記憶體塊中的資料結構。列向量是元素的一維陣列。在列向量中,元素按列排列。
同樣重要的是要注意,MATLAB 預設以列方式儲存陣列,這意味著陣列的元素作為列向量儲存在記憶體中。
在本文中,我們將探討使用 MATLAB 程式設計將陣列轉換為列向量的不同方法。
方法 (1):使用轉置函式
我們可以使用“轉置”函式將陣列轉換為列向量,該函式是 MATLAB 的內建函式。
以下 MATLAB 程式演示瞭如何使用“轉置”函式將陣列轉換為列向量。
示例
% MATLAB code for converting an array into a column vector using transpose function
% Define an array
a = [10, 20, 30, 40, 50, 60, 70];
% Turn the array into a column vector
c = transpose(a);
% Display the column vector
disp('Column vector is: ');
disp(c);
輸出
Column vector is:
10
20
30
40
50
60
70
解釋
在這個 MATLAB 程式中,我們演示瞭如何使用“轉置”函式將一維陣列轉換為列向量。
在此程式碼中,我們首先建立一個包含 7 個元素的一維陣列。接下來,我們使用“轉置”函式將其轉換為列向量。最後,我們使用“disp”函式顯示結果。
關於“轉置”函式需要注意的重要一點是,它只能將一維陣列轉換為列向量。
方法 (2):使用轉置運算子
我們還可以使用轉置運算子“’”將陣列轉換為列向量。與轉置函式類似,轉置運算子也用於將一維陣列轉換為列向量。
以下 MATLAB 程式演示瞭如何使用轉置運算子將陣列轉換為列向量。
示例
% MATLAB code for converting an array into a column vector using transpose operator
% Define an array
a = [10, 20, 30, 40, 50, 60, 70];
% Turn the array into a column vector
c = a.';
% Display the column vector
disp('Column vector is: ');
disp(c);
輸出
Column vector is:
10
20
30
40
50
60
70
解釋
此 MATLAB 程式碼的執行與 MATLAB 程式 (1) 類似。唯一的區別在於,在此程式碼中,我們使用了轉置運算子“’”而不是轉置函式。
方法 (3):使用冒號運算子
MATLAB 還允許我們使用冒號運算子“:”將陣列轉換為列向量。關於冒號運算子需要注意的重要一點是,它可以將多維陣列轉換為列向量,而轉置運算子無法做到這一點。
以下兩個 MATLAB 程式分別演示瞭如何使用冒號運算子將一維陣列和二維陣列轉換為列向量。
示例
% MATLAB code for converting an array into a column vector using colon operator
% Define a one-dimensional array
a = [10, 20, 30, 40, 50, 60, 70];
% Turn the array into a column vector
c = a(:);
% Display the column vector
disp('Column vector is: ');
disp(c);
輸出
Column vector is:
10
20
30
40
50
60
70
解釋
在此 MATLAB 程式碼中,我們使用了冒號運算子“:”將一維陣列轉換為列向量。上述程式碼中的表示式“c = a(:);”將陣列“a”轉換為列向量並將結果儲存在變數“c”中。
示例
% MATLAB code for converting an array into a column vector using colon operator
% Define a two-dimensional array
a = [10 20 30; 40 50 60; 70 80 90];
% Turn the array into a column vector
c = a(:);
% Display the column vector
disp('Column vector is: ');
disp(c);
輸出
Column vector is:
10
40
70
20
50
80
30
60
90
解釋
此 MATLAB 程式碼使用冒號運算子“:”將二維陣列轉換為列向量。
方法 (4):使用重塑函式
我們還可以使用“reshape()”函式將陣列轉換為列向量,該函式是 MATLAB 的內建函式。與冒號運算子類似,reshape 函式也可用於將多維陣列轉換為列向量。
以下 MATLAB 程式分別演示瞭如何使用“reshape”函式將一維陣列和二維陣列轉換為列向量。
示例
% MATLAB code for converting an array into a column vector using the reshape function
% Define a one-dimensional array
a = [10 20 30 40 50 60 70];
% Determine the number of elements in the array
n = numel(a);
% Turn the array into a column vector
c = reshape(a, n, 1);
% Display the column vector
disp('Column vector is: ');
disp(c);
輸出
Column vector is:
10
20
30
40
50
60
70
解釋
在此 MATLAB 程式碼中,我們首先建立一個一維陣列並將其儲存在變數“a”中。然後,我們使用“numel”函式獲取陣列中的元素數量並將結果儲存在變數“n”中。
接下來,我們使用“reshape”函式將陣列轉換為列向量。最後,我們使用“disp”函式顯示生成的列向量“c”。
示例
% MATLAB code for converting an array into a column vector using reshape function
% Define a two-dimensional array
a = [10 20 30; 40 50 60; 70 80 90];
% Determine the number of elements in the array
n = numel(a);
% Turn the array into a column vector
c = reshape(a, n, 1);
% Display the column vector
disp('Column vector is: ');
disp(c);
輸出
Column vector is:
10
40
70
20
50
80
30
60
90
解釋
此 MATLAB 程式碼演示瞭如何使用“reshape”函式將二維陣列轉換為列向量。此程式碼的執行與 MATLAB 程式 (5) 相同。
結論
因此,這就是關於將陣列轉換為列向量的所有內容。MATLAB 提供了各種運算子和內建函式來將陣列轉換為列向量。我們在本文的上述部分藉助 MATLAB 程式介紹了所有這些方法。
資料結構
網路
關係型資料庫管理系統
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP