# Math Help - MATLAB translation

1. ## MATLAB translation

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

2. Hi!

I'm sorry but I can't give you the answer - because I don't understand it.

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,
That sounds good and makes sense.

Originally Posted by CarmineCortez
but I don't know how it gets the sig digits...I do not have access to MATLAB.
So theoretically you are able to understand the algorithm for that problem, I don't..., but you have problems with the syntax? Is that it?

function y=dig(x,D)

This is like you said. x is a vector, D is the significant decimals and y is also a vector.

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

ex is a vector, too. D is an integer like 10. And x is a vector [5,3,-1]

abs(x) leads to the vector [|5|,|3|,|-1| ] = [5,3,1]

log10(x) -> you get the vector [log(5),log(3),log(1)]

So what does ceil do? For this syntax you should use google or go to the official website of matlab, because there is a good documentation.

You find this:
MATLAB

In my opinion it explains it very good what ceil does, because there is a nice example.

It is D = 10 for example and you solve ceil(...).
For example you have 10 - [3,5,7] then matlab returns the vector [10-3, 10-5, 10-7]

I don't know what happens in the next line if the vector has a negative number in it.

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

Use the Matlab documentation I showed you to find out how round() works.

The critical thing here is the point ' . ' You need it if you calculate * or / or ^ or... then you get this: You have two vectors [x_1,...,x_n] and ex_1,...ex_n

round(x.*(10.^ex)).*(10.^(-ex));
= round([x_1*10^ex_1)*10^(-ex_1),...,[x_n*10^ex_n)*10^(-ex_n) ])

But this could be wrong. I don't have matlab either. I just think this is what Matlab does. If not, I'm sorry, but no one hasn't responded yet, so I thought it might help.

Kind regards
Rapha

3. 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