view toolbox/chop.m @ 4:0aae72032c25 draft

Added files described in changeset d24a00dabdc2.
author Antonio Pino Robles <data.script93@gmail.com>
date Fri, 15 May 2015 20:30:21 +0200
parents 8f23314345f4
children
line wrap: on
line source

function c = chop(x, t)
%CHOP    Round matrix elements.
%        CHOP(X, t) is the matrix obtained by rounding the elements of X
%        to t significant binary places.
%        Default is t = 24, corresponding to IEEE single precision.

if nargin < 2, t = 24; end
[m, n] = size(x);

%  Use the representation:
%  x(i,j) = 2^e(i,j) * .d(1)d(2)...d(s) * sign(x(i,j))

%  On the next line `+(x==0)' avoids passing a zero argument to LOG, which
%  would cause a warning message to be generated.

y = abs(x) + (x==0);
e = floor(log2(y) + 1);
c = pow2(round( pow2(x, t-e) ), e-t);