round_ice

PURPOSE ^

ROUND_ICE - rounds up x so that it has only numnonzeros non zero digits

SYNOPSIS ^

function new_x=round_ice(x,numnonzeros)

DESCRIPTION ^

ROUND_ICE - rounds up x so that it has only numnonzeros non zero digits

   numnonzeros must be an integer larger or equal to 1

   Usage:
      new_x=round_ice(x,numnonzeros)

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 function new_x=round_ice(x,numnonzeros)
0002 %ROUND_ICE - rounds up x so that it has only numnonzeros non zero digits
0003 %
0004 %   numnonzeros must be an integer larger or equal to 1
0005 %
0006 %   Usage:
0007 %      new_x=round_ice(x,numnonzeros)
0008 
0009 %some checks
0010 if (nargin ~=2 | nargout>1),
0011     error('round_ice usage: new_x=round_ice(x,numonzeros)');
0012 end
0013 if ~isnumeric(x)
0014     error('round_ice error message: x must be a number and numzeros an integer');
0015 end
0016 if round(numnonzeros)~=numnonzeros
0017     error('round_ice error message: numnonzeros must be an integer larger or equal to 1')
0018 end
0019 if any(numnonzeros<1)
0020     error('round_ice error message: numnonzeros must be an integer larger or equal to 1')
0021 end
0022 if (length(numnonzeros)~=1 & size(numnonzeros)~=size(x))
0023     error('round_ice error message: numnonzeros must be an integer larger or equal to 1 or a list of integers of length length(x)')
0024 end
0025 
0026 %figure out how long x is
0027 lengthx=ceil(log10(abs(x)));
0028 
0029 %if x contains 0, lengthx=-Inf
0030 lengthx(isinf(lengthx))=1;
0031 
0032 %get its sign
0033 si=sign(x);
0034 
0035 %rule out zeros
0036 new_x=si.*round(abs(x).*10.^(-lengthx+numnonzeros)).*10.^(lengthx-numnonzeros);
0037 
0038

Generated on Sun 29-Mar-2009 20:22:55 by m2html © 2003