如何在MATLAB中反转数字?
在本文中,我们将讨论在 MATLAB 中可以使用多种方法完成的“数字反转”,如下所示。
使用 str2num()
str2num()函数用于将指定的字符数组或字符串转换为数值数组。
Syntax: str2num(chr)
Parameters: This function accepts a parameter .
- char: This is the specified character array or string.
例子:
Matlab
% MATLAB code for str2num()
% Calling the str2num() functions
% over the character 10
str2num('10')
Matlab
% MATLAB code for fliplr()
% Calling the fliplr() function
% over a number string '123'
fliplr('123')
Matlab
% MATLAB code for reversing a specified number
% using the combination of str2num(), fliplr()
% and num2str()
% Specifying a number to reverse
Number = 2468
% Calling the combination of three functions
% str2num(), fliplr(), and num2str()
% over the above number to reverse
Reversed_Number = str2num(fliplr(num2str(Number)))
Matlab
% MATLAB code for polyval() for reverse a number
% Specifying a number to reverse
Number = 3579
% Calling the num2str() over the
% above number
s = num2str(Number) - '0';
% Calling the polyval() function
Reversed_Number = polyval(s(end:-1:1),10)
Matlab
% MATLAB code for reverse a number
% using flip() and sprintf()
% Calling the flip() function over the
% sprintf() function with number
% 5678 as its parameter to reverse
Reversed_Number = flip(sprintf('%d', 5678), 2)
输出:
ans = 10
使用fliplr()
fliplr()函数用于从左到右翻转指定的数字。
Syntax: fliplr(num)
Parameters: This function accepts a parameter.
- num: This is the specified number.
例子:
MATLAB
% MATLAB code for fliplr()
% Calling the fliplr() function
% over a number string '123'
fliplr('123')
输出:
ans = 321
使用str2num() 、 fliplr()和num2str()
num2str()函数用于将指定的数字转换为字符数组。
Syntax: num2str(num)
Parameters: This function accepts a parameter.
- num: This is the specified number.
例子:
MATLAB
% MATLAB code for reversing a specified number
% using the combination of str2num(), fliplr()
% and num2str()
% Specifying a number to reverse
Number = 2468
% Calling the combination of three functions
% str2num(), fliplr(), and num2str()
% over the above number to reverse
Reversed_Number = str2num(fliplr(num2str(Number)))
输出:
Number = 2468
Reversed_Number = 8642
使用 polyval() 和 num2str()
polyval(p, x)函数用于返回在 x 处评估的 n 次多项式的值。输入参数 p 是一个长度为 n+1 的向量,其元素是要评估的多项式的降幂系数。
Syntax: polyval(p, x)
Parameters: This function accepts two parameters,
- p: This is the specified input vector of length n+1.
- x: This is the specified matrix or vector or array.
MATLAB
% MATLAB code for polyval() for reverse a number
% Specifying a number to reverse
Number = 3579
% Calling the num2str() over the
% above number
s = num2str(Number) - '0';
% Calling the polyval() function
Reversed_Number = polyval(s(end:-1:1),10)
输出:
Number = 3579
Reversed_Number = 9753
使用flip()和sprintf()
sprintf()函数用于将数据格式化为字符串或字符向量。
Syntax: sprintf(formatSpec, A1, …, An)
Here, sprintf(formatSpec, A1, …, An) is used to format the data in arrays A1,…, An uses the formatting operators specified by formatSpec and returns the resulting text in str.
例子:
MATLAB
% MATLAB code for reverse a number
% using flip() and sprintf()
% Calling the flip() function over the
% sprintf() function with number
% 5678 as its parameter to reverse
Reversed_Number = flip(sprintf('%d', 5678), 2)
输出:
Reversed_Number = 8765