from
roundd.m
by Carlos Adrian Vargas Aguilera
Rounds an array up to N digits from the left.
|
| roundd(X,N)
|
function Y = roundd(X,N)
% ROUNDD Rounds to some digits from the left.
% Y = ROUNDD(X,N) rounds the array X up to N digits from the left, taking
% the element with more digits to the left as the reference, even if it
% is a decimal.
%
% Example:
% If X = [ 1234.56 25.5 0.045 856 52.87],
% then roundd(X,2) = [1200 0 0 900 100].
%
% See also ROUND, ROUNDN
% Written by
% M.S. Carlos Adrin Vargas Aguilera
% Physical Oceanography PhD candidate
% CICESE
% Mexico, november 2006
%
% nubeobscura@hotmail.com
% Make sure input is an integer:
if any([~isnatural(N(:)); (numel(N)~=1); (N(:)<1)])
error('Argument must be an integer.')
end
N = N-1;
Xmax = max(abs(X(:)));
if Xmax ~= 0
M = floor(log10(Xmax));
Y = X*10^(N-M);
Y = round(Y);
Y = Y*10^(M-N);
else
Y = X;
end
function yes = isnatural(n)
%ISNATURAL Checks if an array has natural numbers.
% Y = ARENATURAL(X) returns an array of the same size as X with ones in
% the elements of X that are natural numbers (...-2,-1,0,1,2,...), and
% zeros where not.
% Written by
% M.S. Carlos Adrin Vargas Aguilera
% Physical Oceanography PhD candidate
% CICESE
% Mexico, november 2006
%
% nubeobscura@hotmail.com
yes = (n==floor(n)).*(isreal(n));
% Carlos Adrin. nubeobscura@hotmail.com
|
|
Contact us at files@mathworks.com