⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 numdig.m

📁 很多matlab的源代码
💻 M
字号:
function y = numdig(x,n,ty)
% NUMDIG Rounding/Trunctation to n significant digits.
%
%	Y = NUMDIG(X,N)      truncates X to N significant digits.
%	Y = NUMDIG(X,N,'r')  rounds X to N significant digits.
%
%	NOTE: Real and imaginary parts are rounded (or truncated) separately.
%
%	NUMDIG (with no input arguments) invokes the following example:
%
%	% Select some numbers, truncate and round to 6 significant digits 
%	% and display the results using a long scientific display
%        >>format long e
%        >>x1 = log([0.1;5;1e5])
%        >>x2 = numdig(x1,6)
%        >>x3 = numdig(x1,6,'r')
%        >>format        	% return to default format


% ADSP Toolbox: Version 2.0 
% For use with "Analog and Digital Signal Processing", 2nd Ed.
% Published by PWS Publishing Co.
%
% Ashok Ambardar, EE Dept. MTU, Houghton, MI 49931, USA
% http://www.ee.mtu/faculty/akambard.html
% e-mail: akambard@mtu.edu
% Copyright (c) 1998


if nargin==0,help numdig,disp('Strike a key to see results of the example')
pause,format long e,
x1=log([0.1;5;1e5]),x2=numdig(x1,6),x3=numdig(x1,6,'r'),format,return,end

if nargin<3,ty='t';end,
if nargin<2,n=4;end
flag=0;xr=real(x);xi=imag(x);
i=find(xr==0);
if ~isempty(i),flag=1;[mm,nn]=size(i);xr(i)=ones(mm,nn);end
m=ceil(log10(abs(xr)));
n0=(10).^(n-m);
if ty=='t',yr=fix(xr.*n0)./n0;else,yr=round(xr.*n0)./n0;end
if flag==1;yr(i)=zeros(mm,nn);end
flag=0;i=find(xi==0);
if ~isempty(i),flag=1;[mm,nn]=size(i);xi(i)=ones(mm,nn);end
m=ceil(log10(abs(xi)));
n0=(10).^(n-m);
if ty=='t',yi=fix(xi.*n0)./n0;else,yi=round(xi.*n0)./n0;end
if flag==1;yi(i)=zeros(mm,nn);end
y=yr+sqrt(-1)*yi;

⌨️ 快捷键说明

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