📄 sd_round.m
字号:
% flag == 2 rounds up
% flag == 3 rounds down
% flag == 4 rounds toward zero
% flag == 5 rounds away from zero
% otherwise round to the nearest integer
buf=dec./mult;
switch flag
case 1
A2=1./buf.*round(buf.*A);
case 2
A2=1./buf.*ceil(buf.*A);
case 3
A2=1./buf.*floor(buf.*A);
case 4
A2=1./buf.*fix(buf.*A);
case 5
A2=sign(A)./buf.*ceil(buf.*abs(A));
otherwise
A2=1./buf.*round(buf.*A);
end
A2(A==0)=0;
% After rounding recalculate the number of significant digits.
% The number of digits to the left and right of the decimal
% place can be effected by rounding.
% Digit furthest to the left of the decimal point
D1=ceil(log10(abs(A2)));
buf1=D1( abs(A2)-10.^D1 == 0)+1;
D1( abs(A2)-10.^D1 == 0)=buf1;
% Number of digits to the left of the decimal place
real_digitsL=max(D1, 0);
real_digitsL(A2==0)=0;
% rounding factor
dec=10.^(N-D1);
% Number of digits to the right of the decimal place
real_digitsR=max(N-D1, 0);
real_digitsR(A2==0)=N;
else
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Round the real part
Ar=real(A);
% Digit furthest to the left of the decimal point
D1=ceil(log10(abs(Ar)));
buf1=D1( abs(Ar)-10.^D1 == 0)+1;
D1( abs(Ar)-10.^D1 == 0)=buf1;
% Number of digits to the left of the decimal place
real_digitsL=max(D1, 0);
real_digitsL(Ar==0)=0;
% rounding factor
dec=10.^(N-D1);
% Number of digits to the right of the decimal place
real_digitsR=max(N-D1, 0);
real_digitsR(Ar==0)=N;
% Rounding Computation
% This program supports five different styles of rounding.
% flag == 1 rounds to the nearest integer
% flag == 2 rounds up
% flag == 3 rounds down
% flag == 4 rounds toward zero
% flag == 5 rounds away from zero
% otherwise round to the nearest integer
buf=dec./mult;
switch flag
case 1
A2r=1./buf.*round(buf.*Ar);
case 2
A2r=1./buf.*ceil(buf.*Ar);
case 3
A2r=1./buf.*floor(buf.*Ar);
case 4
A2r=1./buf.*fix(buf.*Ar);
case 5
A2r=sign(Ar)./buf.*ceil(buf.*abs(Ar));
otherwise
A2r=1./buf.*round(buf.*Ar);
end
A2r(Ar==0)=0;
% After rounding recalculate the number of significant digits.
% The number of digits to the left and right of the decimal
% place can be effected by rounding.
% Digit furthest to the left of the decimal point
D1=ceil(log10(abs(A2r)));
buf1=D1( abs(A2r)-10.^D1 == 0)+1;
D1( abs(A2r)-10.^D1 == 0)=buf1;
% Number of digits to the left of the decimal place
real_digitsL=max(D1, 0);
real_digitsL(A2r==0)=0;
% rounding factor
dec=10.^(N-D1);
% Number of digits to the right of the decimal place
real_digitsR=max(N-D1, 0);
real_digitsR(A2r==0)=N;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Round the imaginary part
Ai=imag(A);
% Digit furthest to the left of the decimal point
D1=ceil(log10(abs(Ai)));
buf1=D1( abs(Ai)-10.^D1 == 0)+1;
D1( abs(Ai)-10.^D1 == 0)=buf1;
% Number of digits to the left of the decimal place
imag_digitsL=max(D1, 0);
imag_digitsL(Ai==0)=0;
% rounding factor
dec=10.^(N-D1);
% Number of digits to the right of the decimal place
imag_digitsR=max(N-D1, 0);
imag_digitsR(Ai==0)=N;
% Rounding Computation
% This program supports five different styles of rounding.
% flag == 1 rounds to the nearest integer
% flag == 2 rounds up
% flag == 3 rounds down
% flag == 4 rounds toward zero
% flag == 5 rounds away from zero
% otherwise round to the nearest integer
buf=dec./mult;
switch flag
case 1
A2i=1./buf.*round(buf.*Ai);
case 2
A2i=1./buf.*ceil(buf.*Ai);
case 3
A2i=1./buf.*floor(buf.*Ai);
case 4
A2i=1./buf.*fix(buf.*Ai);
case 5
A2i=sign(Ai)./buf.*ceil(buf.*abs(Ai));
otherwise
A2i=1./buf.*round(buf.*Ai);
end
A2i(Ai==0)=0;
% After rounding recalculate the number of significant digits.
% The number of digits to the left and right of the decimal
% place can be effected by rounding.
% Digit furthest to the left of the decimal point
D1=ceil(log10(abs(A2i)));
buf1=D1( abs(A2i)-10.^D1 == 0)+1;
D1( abs(A2i)-10.^D1 == 0)=buf1;
% Number of digits to the left of the decimal place
imag_digitsL=max(D1, 0);
imag_digitsL(A2i==0)=0;
% rounding factor
dec=10.^(N-D1);
% Number of digits to the right of the decimal place
imag_digitsR=max(N-D1, 0);
imag_digitsR(A2i==0)=N;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Add the real and imaginary parts together
A2=A2r+i*A2i;
end
else
warningdlg('Error in p_round Input Matrix A is not numeric');
A2=A;
end
end
% % Convert the rounded array to string format with specified
% % number of significant digits.
[m1, n1]=size(A);
A_str=cell(size(A));
rtd=round(real_digitsL+real_digitsR);
itd=round(imag_digitsL+imag_digitsR);
% Output the cell array of strings if it is in the output argument list
if nargout > 1
% This code formats the rounded numbers into a cell array
% of strings.
if isreal(A)
for e1=1:m1
for e2=1:n1;
aa2=num2str(A2(e1, e2), ['%', int2str(rtd(e1, e2)), '.', int2str(real_digitsR(e1, e2)), 'f' ]);
if length(aa2) < N
aa2=num2str(A2(e1, e2),['%', int2str(N), '.', int2str(N), 'f']);
end
A_str{e1, e2}=aa2;
end
end
else
for e1=1:m1
for e2=1:n1;
if imag(A2(e1, e2)) >= 0
aa=' + ';
else
aa=' - ';
end
aa1=num2str(real(A2(e1, e2)), ['%', int2str(rtd(e1, e2)),'.', int2str(real_digitsR(e1, e2)), 'f' ]);
if length(aa1) < N
aa1=num2str(real(A2(e1, e2)),['%', int2str(N), '.', int2str(N), 'f']);
end
aa2=num2str(abs(imag(A2(e1, e2))), ['%', int2str(itd(e1, e2)),'.', int2str(imag_digitsR(e1, e2)), 'f' ]);
if length(aa2) < N
aa2=num2str(abs(imag(A2(e1, e2))),['%', int2str(N), '.', int2str(N), 'f']);
end
A_str{e1, e2}= [aa1, aa, aa2, 'i'];
end
end
end
else
A_str={};
end
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -