How to reverse a number in MATLAB?
Last Updated :
05 Aug, 2021
In this article, we will discuss the "Reversing of a number" in MATLAB that can be done using the multiple methods which are illustrated below.
Using str2num()
The str2num() function is used to convert the specified character array or string to a numeric array.
Syntax: str2num(chr)
Parameters: This function accepts a parameter .
- char: This is the specified character array or string.
Example:
Matlab
% MATLAB code for str2num()
% Calling the str2num() functions
% over the character 10
str2num('10')
Output:
ans = 10
Using fliplr()
The fliplr() function is used to flip the specified number from left to right.
Syntax: fliplr(num)
Parameters: This function accepts a parameter.
- num: This is the specified number.
Example:
Matlab
% MATLAB code for fliplr()
% Calling the fliplr() function
% over a number string '123'
fliplr('123')
Output:
ans = 321
Using str2num(), fliplr(), and num2str()
The num2str() function is used to convert the specified numbers to a character array.
Syntax: num2str(num)
Parameters: This function accepts a parameter.
- num: This is the specified number.
Example:
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)))
Output:
Number = 2468
Reversed_Number = 8642
Using polyval( ) and num2str( )
The polyval(p, x) function is used to return the value of a polynomial of degree n evaluated at x. The input argument p is a vector of length n+1 whose elements are the coefficients in descending powers of the polynomial to be evaluated.
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)
Output:
Number = 3579
Reversed_Number = 9753
Using flip( ) and sprintf()
The sprintf() function is used to format the data into a string or character vector.
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.
Example:
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)
Output:
Reversed_Number = 8765
Similar Reads
How to reverse a string in MATLAB? In this article, we are going to discuss the "Reversing of a string" in MATLAB which can be done in multiple approaches that are illustrated below: Method 1: Using Built-in Functions By using MATLAB built-in functions like reverse(), flip() and fliplr(). Built-in functions can be defined as - " The
3 min read
How to remove decimal in MATLAB? In this article, we are going to discuss the "Removal of decimal point" in MATLAB which can be done using sprintf(), fix(), floor(), round() and num2str() functions which are illustrated below. Using sprintf() The sprintf() function is used to write formatted data to a string. Syntax: sprintf(format
3 min read
MATLAB - Read Words in a File in Reverse Order MATLAB stands for Matrix Laboratory. It is a high-performance language that is used for technical computing. It allows matrix manipulations, plotting of functions, implementation of algorithms, and creation of user interfaces Suppose we are given a text file containing the following words "I STUDY F
2 min read
How to inverse a vector in MATLAB? In this article, we are going to discuss the "Inversion of a vector" in MATLAB which can be done in three different approaches that are illustrated below: Method 1: By using flipud() function The flipud() function is used for flipping the specified vector's elements. Syntax: flipud(A) Here, flipud(A
2 min read
Negative of an image in MATLAB The negative of an image is achieved by replacing the intensity 'i' in the original image by 'i-1', i.e. the darkest pixels will become the brightest and the brightest pixels will become the darkest. Image negative is produced by subtracting each pixel from the maximum intensity value. For example i
2 min read
How to Round toward negative infinity in MATLAB Rounding a number towards infinity means rounding the number X to the nearest integer less than or equal to X. In this article, we will discuss how to round toward negative infinity in MATLAB. Example: Suppose X = 3.5, then result is 3And if X = -3.5, then result is -4Â The floor() function in MATLA
2 min read