如何在MATLAB中反转向量?
在本文中,我们将讨论 MATLAB 中的“向量求逆”,它可以通过以下三种不同的方法来完成:
方法一:使用flipud()函数
flipud()函数用于翻转指定向量的元素。
Syntax:
flipud(A)
Here, flipud(A) function is used to return a vector with reversed elements of the same length of the specified vector “A”.
示例 1:
Matlab
% MATLAB code for inverse a
% vector using flippud()
% Initializing a vector of some elements
v = [10;9;8;7;6;5]
% Calling the flipud() function
% over the above vector "v" to
% reverse its elements
flipud(v)
Matlab
% MATLAB code for inverse vector using flip()
% Initializing a 2*2 cell array of characters
A = {'a' 'b'; 'c' 'd'}
% Calling the flipud() function
% over the above cell array to
% reverse its elements
flipud(A)
Matlab
% MATLAB code for fliplr()
% Initializing a row vector
v = 10:20
% Calling the fliplr() function
% over the above vector "v" to
% reverse its elements
fliplr(v)
Matlab
% MATLAB code for fliplr()
% Initializing a 2*2 cell
% array of characters
A = {'a' 'b'; 'c' 'd'}
% Calling the fliplr() function
% over the above cell array "A" to
% reverse its elements
fliplr(A)
Matlab
% MATLAB code for find inverse
% of vector using end keyword
% Initializing a vector of some elements
v = [10;9;8;7;6;5]
% Reversing the above vector's elements
v(end:-1:1)
Matlab
% MATLAB code for find inverse
% of vector using end keyword
% Initializing a row vector
v = 10:20
% Reversing the above vector's elements
v(end:-1:1)
输出:
示例 2:
MATLAB
% MATLAB code for inverse vector using flip()
% Initializing a 2*2 cell array of characters
A = {'a' 'b'; 'c' 'd'}
% Calling the flipud() function
% over the above cell array to
% reverse its elements
flipud(A)
输出:
方法二:使用fliplr()函数
fliplr()函数用于翻转指定行向量的元素。
Syntax:’
fliplr(A)
Here, fliplr(A) function is used to return a vector with reversed elements of the same length of the specified row vector “A”.
示例 1:
MATLAB
% MATLAB code for fliplr()
% Initializing a row vector
v = 10:20
% Calling the fliplr() function
% over the above vector "v" to
% reverse its elements
fliplr(v)
输出:
示例 2:
MATLAB
% MATLAB code for fliplr()
% Initializing a 2*2 cell
% array of characters
A = {'a' 'b'; 'c' 'd'}
% Calling the fliplr() function
% over the above cell array "A" to
% reverse its elements
fliplr(A)
输出:
方法三:使用“vector(end:-1:1)”操作
MATLAB 中的 end 关键字用于查找行或列索引以引用最后一个元素,
示例 1:
MATLAB
% MATLAB code for find inverse
% of vector using end keyword
% Initializing a vector of some elements
v = [10;9;8;7;6;5]
% Reversing the above vector's elements
v(end:-1:1)
输出:
示例 2:
MATLAB
% MATLAB code for find inverse
% of vector using end keyword
% Initializing a row vector
v = 10:20
% Reversing the above vector's elements
v(end:-1:1)
输出: