Appending trailing zeros to integers in a vector

Let's assume I have the following vector:
x = [2, 13, 901, 5, 18, 7, 112, 5931];
And I created a function that first counts the digits using numel function, determines the maximum number of digits (4 digits in this example), then tried to append zeros to the right of each number corresponding to the relation (difference) between the max and the current number of digits, resulting in the following:
x = [2000, 1300, 9010, 5000, 1800, 7000, 1120, 5931];
I couldn't find out a straightforward way to do it.

2 Comments

Why would you want to do such a thing?
Hi Matt,
This is just a part of an algorithm, it relies on counting digits and so. You know sometimes you get stuck with tiny programming details that may prevent you from progressing if not solved.

Sign in to comment.

 Accepted Answer

Hi Maamoun, try
x = [2, 13, 901, 5, 18, 7, 112, 5931];
y = floor(log10(x))
xnew = x.*10.^(max(y)-y)

More Answers (0)

Categories

Find more on Mathematics in Help Center and File Exchange

Products

Release

R2015a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!