如何在 MATLAB 中反轉向量?
眾所周知,MATLAB 是一個強大的工具,可以對矩陣和陣列執行各種操作。向量是一種僅有一行或一列的矩陣。我們可以使用 MATLAB 確定向量的反轉版本。為此,MATLAB 提供了不同的方法和函式。因此,讓我們討論使用 MATLAB 中的不同方法反轉向量的方法。
使用 MATLAB 中的索引方法反轉向量
在 MATLAB 中,我們可以利用索引方法來查詢給定向量的逆。在這種方法中,向量的元素使用它們的索引按相反的順序排列。
示例
以下是一個示例,說明如何使用索引方法查詢向量的逆。
% MATLAB code to inverse a vector using indexing method
% Create a sample vector
V = [12, 20, 31, 14, 50, 14, 30, 10, 11, 9];
% Find the inverse of the vector
I = V(end : -1 : 1);
% Display the input and inverted vectors
disp('The input vector is:');
disp(V);
disp('The inverted vector is:');
disp(I);
輸出
執行此程式碼時,它將產生以下輸出 -
The input vector is:
12 20 31 14 50 14 30 10 11 9
The inverted vector is:
9 11 10 30 14 50 14 31 20 12
解釋
在此示例中,我們首先建立一個示例向量“V”。然後,我們透過反轉向量元素的索引來查詢向量的逆,並將反轉結果儲存在變數“I”中。最後,我們使用“disp”函式顯示輸入和反轉向量。
使用 MATLAB 中的“flip”函式反轉向量
在 MATLAB 中,有一個名為“flip”的內建函式可用於查詢向量的反轉。此函式按相反的順序排列輸入向量的元素。
但是,“flip”函式在 MATLAB R2019b 或更高版本的 MATLAB 中可用。
語法
I = flip(V);
這裡,“V”是輸入向量,“I”是反轉向量。
示例
讓我們看一個示例來了解如何實現這種查詢向量逆的方法。
% MATLAB code to inverse a vector using flip function
% Create a sample vector
V = [12, 20, 31, 14, 50, 14, 30, 10, 11, 9];
% Fin inverse of the vector using the flip function
I = flip(V);
% Display the input and inverted vectors
disp('The input vector is:');
disp(V);
disp('The inverted vector is:');
disp(I);
輸出
執行此程式碼時,它將產生以下輸出 -
The input vector is:
12 20 31 14 50 14 30 10 11 9
The inverted vector is:
9 11 10 30 14 50 14 31 20 12
解釋
在此 MATLAB 示例中,我們首先建立一個示例向量。然後,我們呼叫“flip”函式,其中“V”作為輸入向量,並將輸出反轉向量儲存在變數“I”中。最後,我們使用“disp”函式顯示輸入和反轉向量。
使用 MATLAB 中的“fliplr”函式反轉向量
在 MATLAB 中,“fliplr”函式用於查詢行向量的逆。換句話說,“fliplr”函式沿行反轉向量元素的順序。
語法
I = fliplr(V);
示例
以下 MATLAB 示例演示瞭如何使用“fliplr”函式查詢行向量的逆。
% MATLAB code to inverse a vector using the fliplr function
% Create a sample vector
V = [12, 20, 31, 14, 50, 14, 30, 10, 11, 9];
% Find the inverse of the vector
I = fliplr(V);
% Display the input and inverted vectors
disp('The input vector is:');
disp(V);
disp('The inverted vector is:');
disp(I);
輸出
執行此程式碼時,它將產生以下輸出 -
The input vector is:
12 20 31 14 50 14 30 10 11 9
The inverted vector is:
9 11 10 30 14 50 14 31 20 12
解釋
此 MATLAB 程式的程式碼實現和執行與上述程式碼相同。在此程式碼中,我們使用了“fliplr”函式來沿行執行向量的反轉。
上面解釋的這些方法用於查詢行向量的逆。現在讓我們瞭解如何查詢列向量的逆。
使用 MATLAB 中的“flip”函式反轉列向量
我們還可以使用“flip”函式在 MATLAB 中查詢列向量的逆。程式碼的實現與行向量類似。
示例
以下示例程式顯示瞭如何使用“flip”函式查詢列向量的逆。
% MATLAB code to inverse a column vector using flip function
% Create a sample column vector
V = [12; 20; 31; 14; 50];
% Fin inverse of the column vector using the flip function
I = flip(V);
% Display the input and inverted vectors
disp('The input vector is:');
disp(V);
disp('The inverted vector is:');
disp(I);
輸出
執行此程式碼時,它將產生以下輸出 -
The input vector is:
12
20
31
14
50
The inverted vector is:
50
14
31
20
12
使用 MATLAB 中的索引方法反轉列向量
讓我們看看如何使用索引方法來使用 MATLAB 查詢列向量的逆。
示例
以下示例說明了在 MATLAB 中使用索引方法查詢列向量的逆。
% MATLAB code to inverse a column vector using indexing method
% Create a sample column vector
V = [12; 20; 31; 14; 50];
% Find the inverse of the vector
I = V(end : -1 : 1);
% Display the input and inverted vectors
disp('The input vector is:');
disp(V);
disp('The inverted vector is:');
disp(I);
輸出
執行此程式碼時,它將產生以下輸出 -
The input vector is:
12
20
31
14
50
The inverted vector is:
50
14
31
20
12
使用 MATLAB 中的“flipud”函式反轉列向量
在 MATLAB 中,有一個內建函式“flipud”用於查詢列向量的逆。
語法
I = flipud(V);
示例
以下示例程式顯示瞭如何使用“flipud”函式查詢列向量的逆。
% MATLAB code to inverse a column vector using flipud function
% Create a sample column vector
V = [12; 20; 31; 14; 50];
% Fin inverse of the column vector using the flipud function
I = flipud(V);
% Display the input and inverted vectors
disp('The input vector is:');
disp(V);
disp('The inverted vector is:');
disp(I);
輸出
執行此程式碼時,它將產生以下輸出 -
The input vector is:
12
20
31
14
50
The inverted vector is:
50
14
31
20
12
結論
這就是關於使用 MATLAB 查詢向量逆的所有內容。在 MATLAB 中,有兩種方法,即索引方法和內建函式方法,可以查詢向量的逆。在本教程中,我透過示例解釋瞭如何使用 MATLAB 查詢行或列向量的逆。
資料結構
網路
關係資料庫管理系統
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP