Windy Day
Windy Day

Reputation: 173

truncate decimal numbers in matlab?

Is there a quick and easy way to truncate a decimal number, say beyond 4 digits, in MATLAB?

round() isn't helping, it's still rounding off. I have to use it in for loop, so the quickest way is appreciated.

Thanks for your inputs.

Upvotes: 7

Views: 6530

Answers (5)

Luis Mendo
Luis Mendo

Reputation: 112659

Truncating is like rounding if you subtract 5 from the decimal after the last you want to keep.

So, to truncate x to n decimal figures use

round(x - sign(x)*.5/10^n, n)

(Thanks to @gnovice for noticing the need of sign(x) in order to deal with negative numbers.)

For example,

format long
x = 3.141592653589793;
for n = 2:5
    result = round(x - sign(x)*.5/10^n, n);
    disp(result)
end

gives

   3.140000000000000
   3.141000000000000
   3.141500000000000
   3.141590000000000

Upvotes: 6

informaton
informaton

Reputation: 1482

Yet another option:

x = -3.141592653;
x_trun = x - rem(x,0.0001)

x_trun =

    -3.1415

Kudos to gnovice for the update.

In general, for n decimal places:

x_trun = x - rem(x,10^-n)

Upvotes: 10

Wolfie
Wolfie

Reputation: 30047

As you asked for the fastest method, I've put together a quick benchmark of the top 3 truncation methods currently answered here. Please see the code below. I increased the size of the x vector to be rounded, using the timeit function for timing.

function benchie()
    % Set up iteration variables
    K = 17;  n = 4;  T = zeros(K,3);
    for k = 1:K
        x = rand(2^k,1);
        % Define the three truncation functions
        LuisRound = @() round(x - 0.5/10^n, n);
        JodagFix = @() fix(x*10^n)/10^n;
        InfoRem = @() x - rem(x,10^-n);
        % Time each function
        T(k,1) = timeit(LuisRound);
        T(k,2) = timeit(JodagFix);
        T(k,3) = timeit(InfoRem);
    end
    % Plot results
    figure
    plot(2.^(1:K), T); legend('LuisRound', 'JodagFix', 'InfoRem');
    grid on; xlabel('number of elements in x'); ylabel('time taken');
end

The resulting plot can be seen here:

plot

According to this test, the fix method suggested by jodag is significantly quicker, so you should use something like this for a custom truncation function to n decimal places:

function y = trunc(x, n)
%% Truncate matrix/scalar x to n decimal places
    if nargin < 2; n = 0; end; % default to standard fix behaviour if no n given
    y = fix(x*10^n)/10^n;      % return value truncated to n decimal places
end

tests:

>> trunc([pi, 10.45, 1.9], 4)
>> ans = [3.1415   10.4500    1.9000]
>> trunc([pi, 10.45, 1.9], 1)
>> ans = [3.1      10.4       1.9] 

Upvotes: 5

Mendi Barel
Mendi Barel

Reputation: 3677

Use my round2 function:

function result = round2(x,accuracy)
    if nargin<2, accuracy=1; end %default accuracy 1 same as 'round'
    if accuracy<=0, accuracy=1e-6; end
    result=round(x/accuracy)*accuracy;
end

Usual use: round2(3.14159,0.01) But you can also use it to round to every other multipler, for example: round2(number,2) will round to even number, or round2(number,10) etc..

Upvotes: -1

jodag
jodag

Reputation: 22184

Here's one method to truncate d digits after the decimal.

val = 1.234567;
d = 4;
val_trunc = fix(val*10^d)/10^d

Result

val_trunc =

   1.2345

If you know that val is positive then floor() will work in place of fix().

Upvotes: 11

Related Questions