Rounding to a power of 10

Alex Nichols picture Alex Nichols · Jul 21, 2011 · Viewed 18.1k times · Source

I have a variable, tauMax, that I want to round up to the nearest power of ten(1, 10, 100, 1000...). I am using the below expression to find the closest integer to the max value in the tau array. I am finding the max value because I am trying to calculate the power of ten that should be the x axis cutoff. In this cause, tauMax is equal to 756, so I want to have an expression that outputs either 1000, or 3(for 10^3).

tauMax = round(max(tau));

I'd really appreciate any help!

Answer

b3. picture b3. · Jul 21, 2011

Since you're talking base 10, you could just use log10 to get the number of digits.

How about:

>> ceil(log10(756))

ans =

     3