Originally Posted by

**CarmineCortez** I was given the MATLAB function

function y=dig(x,D)

ex = D - ceil(log10(abs(x)));

y = round(x.*(10.^ex)).*(10.^(-ex));

I need to know exactly what this does. I know it takes in a vector x and returns D significant decimals, but I don't know how it gets the sig digits...I do not have access to MATLAB.

Thanks

There are a number of issues here.

The first is the vectorisation, the above is a vector version of the scalar code:

Code:

function y=dig(x,D)
ex = D - ceil(log10(abs(x)));
y = round(x*(10^ex))*(10^(-ex));

where the function ceil(u) returns the smallest integer greater than u.

^ denotes raising to a power

round(u) rounds u to the nearest integer.

Lets take x=1234.56789, and D=2

log10(abs(x))=3.09151..

ceil(log10(abs(x))=4

ex=2-4=-2

y=round(x*10^-2)*10^2=round(12.3456789)*10^2=1200

It reduces x to 2-significant digits

If you want to try the code there is a free Matlab clone here

CB