A compact way to find single digit numbers (i.e. numbers between 0 and 9) and replace them with two digit numbers
14 views (last 30 days)
Show older comments
Commented: Sim on 11 Aug 2022
A compact way to find single digit numbers (i.e. numbers between 0 and 9) and replace them with two digit numbers ?
For example, from:
a = [ 8
I would like to add a zero in front of the single digit numbers:
b = [08
John D'Errico on 9 Aug 2022
Edited: John D'Errico on 9 Aug 2022
In MATLAB, you CANNOT store a number in a numeric format as one that has a leading zero.
If you want to convert the numbers to character form, then it is trivial. One line is sufficient.
a = [ 8
d = dec2base(a,10)
The result is no longer usable as a number though. It is effectively only a picture of a number at that point, or perhaps I should say a caricature, if you can stand the pun.
More Answers (1)
dpb on 9 Aug 2022
For what purpose and in what context? You will only be able to show the leading zeros if you convert the numeric values to some form of text in which case it's trivial
7×1 cell array
Well, there is one other way --
7×1 categorical array
but if you want a straight, ordinary double array to appear that way at the command line, just not possible.
Find more on Logical in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!Start Hunting!