chop.m

来自「经典通信系统仿真书籍《通信系统与 MATLAB (Proakis)》源代码」· M 代码 · 共 29 行

M
29
字号
function X=chop(Xin,n,unit)
% CHOP  CHOP(X,n) rounds the elements of X to n significant 
%       figures.
%       
%       e.g. chop(3.141592,5) returns 3.141600000..
%
%       CHOP(X,n,unit) rounds the elements of X to n significant
% 	figures whose digits (mantissa) are exactly divisible
%	by unit. 
%
%       e.g. chop(3.141592,3,5) returns 3.150000000..
%            chop(3.141592,3,3) returns 3.150000000..
%            chop(3.141592,3,2) returns 3.140000000..
%	   

%	Copyright (c) 1986-93 by the MathWorks, Inc.

% Set last sig. fig. rounding to 1 if only two input arguments.
if nargin<3, unit=1; end

% Cater for -ve numbers  and numbers = 0.
X=abs(Xin) +(Xin==0);
[nx,mx] = size(X);
exponent=unit.*((10*ones(nx,mx)).^(floor(log10(X))-n+1));
X=round(X./exponent).*exponent;

% Put back sign and zeros
X=sign(Xin).*X.*(Xin~=0);

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?