


FIXDEC - Round towards zero with a specified number of decimals.
This routine rounds the elements of X to N decimals.
Usage:
y = fixdec(x, n)
Example:
fixdec(10*sqrt(2) + i*pi/10, 4) returns 14.1421 + 0.3141i
See also: FIX, FLOOR, CEIL, ROUND, FIXDIG, ROUNDDEC, ROUNDDIG.

0001 function y = fixdec(x, n) 0002 %FIXDEC - Round towards zero with a specified number of decimals. 0003 % 0004 % This routine rounds the elements of X to N decimals. 0005 % 0006 % Usage: 0007 % y = fixdec(x, n) 0008 % 0009 % Example: 0010 % fixdec(10*sqrt(2) + i*pi/10, 4) returns 14.1421 + 0.3141i 0011 % 0012 % See also: FIX, FLOOR, CEIL, ROUND, FIXDIG, ROUNDDEC, ROUNDDIG. 0013 0014 % Author: Peter J. Acklam 0015 % Time-stamp: 2004-09-22 20:08:10 +0200 0016 % E-mail: pjacklam@online.no 0017 % URL: http://home.online.no/~pjacklam 0018 0019 % Check number of input arguments. 0020 error(nargchk(2, 2, nargin)); 0021 0022 % Quick exit if either argument is empty. 0023 if (isempty(x) |isempty(n)) 0024 y = []; 0025 return 0026 end 0027 0028 % Get size of input arguments. 0029 size_x = size(x); 0030 size_n = size(n); 0031 scalar_x = all(size_x == 1); % True if x is a scalar. 0032 scalar_n = all(size_n == 1); % True if n is a scalar. 0033 0034 % Check size of input arguments. 0035 if ~scalar_x & ~scalar_n & ~isequal(size_x, size_n) 0036 error(['When both arguments are non-scalars they must have' ... 0037 ' the same size']); 0038 end 0039 0040 f = 10.^n; 0041 y = fix(x .* f) ./ f;