Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Rounding decimals off in multiple columns

Subject: Rounding decimals off in multiple columns

From: Ryan Neilson

Date: 29 Dec, 2008 15:52:01

Message: 1 of 7

Hello:

I'm trying to round-off data in columns to three decimals and was hoping for some useful assistance!

In the array, I need to round 5 columns (columns #4-8) to three decimals places. There are 1,099 rows in these columns if that's helpful/useful whatsoever.

Thanks in advace!

Subject: Rounding decimals off in multiple columns

From: someone

Date: 29 Dec, 2008 16:18:02

Message: 2 of 7

"Ryan Neilson" <rneilson3@cogeco.ca> wrote in message <gjarn1$87n$1@fred.mathworks.com>...
> Hello:
>
> I'm trying to round-off data in columns to three decimals and was hoping for some useful assistance!
>
> In the array, I need to round 5 columns (columns #4-8) to three decimals places. There are 1,099 rows in these columns if that's helpful/useful whatsoever.
>
> Thanks in advace!

% something like:

A(:,4:8) = round(1000*A(:,4:8))/1000

Subject: Rounding decimals off in multiple columns

From: Walter Roberson

Date: 2 Jan, 2009 10:25:09

Message: 3 of 7

Ryan Neilson wrote:
> I'm trying to round-off data in columns to three decimals

That isn't possible in Matlab in any released implementation. There is no
exact representation for 0.1 in any finite precision binary numbering system.

Try for example

sprintf('%.75g', 0.001)

and you will see that what appears to already be a number rounded to three decimals
is in fact nothing of the sort internally.

There is no solution other than to not store your numbers as floating point numbers.

--
.signature note: I am now avoiding replying to unclear or ambiguous postings.
Please review questions before posting them. Be specific. Use examples of what you mean,
of what you don't mean. Specify boundary conditions, and data classes and value
relationships -- what if we scrambled your data or used -Inf, NaN, or complex(rand,rand)?

Subject: Rounding decimals off in multiple columns

From: Roger Stafford

Date: 2 Jan, 2009 20:18:01

Message: 4 of 7

"Ryan Neilson" <rneilson3@cogeco.ca> wrote in message <gjarn1$87n$1@fred.mathworks.com>...
> Hello:
>
> I'm trying to round-off data in columns to three decimals and was hoping for some useful assistance!
>
> In the array, I need to round 5 columns (columns #4-8) to three decimals places. There are 1,099 rows in these columns if that's helpful/useful whatsoever.
>
> Thanks in advace!

  Ryan, I suspect 'Someone' has given you the answer you are actually seeking, in spite of Walter's rather pessimistic statement. Walter is technically correct only in the following sense. If for example you start with the quantity x = 2.0267 and apply 'Someone's (vectorized) operation, you first get

 round(x*1000) = 2027

which will be an exact integer and then

 xr = round(x*1000)/1000 = 2.0270

if you are displaying in format short or even format long. However, if you look at xr more closely with, say,

 sprintf('%20.18f\n',xr)

you will get 2.027000000000000135.

  What this means is that matlab is inherently incapable of actually realizing the fraction 2.027 exactly and the xr above is the closest it can possibly get to that as a number in binary floating point (double) format. However, I am guessing 'Someone's method would probably satisfy your needs in rounding your data.

  In discussing the rounding of numbers to various numbers of decimal places, there should always be a careful distinction made between doing so with the internal form of stored numbers and that of how they are displayed. The above actually alters the internal quantity. If it is only a matter of displaying numbers rounded to three decimals, that should be done using the 'fprintf', 'sprintf', or 'format' forms of accuracy in displaying numbers without actually making changes to the numbers themselves.

Roger Stafford

Subject: Rounding decimals off in multiple columns

From: David Smith

Date: 14 Jan, 2009 21:44:02

Message: 5 of 7

"Roger Stafford" <ellieandrogerxyzzy@mindspring.com.invalid> wrote in message <gjlspp$9pc$1@fred.mathworks.com>...
> "Ryan Neilson" <rneilson3@cogeco.ca> wrote in message <gjarn1$87n$1@fred.mathworks.com>...
> > Hello:
> >
> > I'm trying to round-off data in columns to three decimals and was hoping for some useful assistance!
> >
> > In the array, I need to round 5 columns (columns #4-8) to three decimals places. There are 1,099 rows in these columns if that's helpful/useful whatsoever.
> >
> > Thanks in advace!
>
> Ryan, I suspect 'Someone' has given you the answer you are actually seeking, in spite of Walter's rather pessimistic statement. Walter is technically correct only in the following sense. If for example you start with the quantity x = 2.0267 and apply 'Someone's (vectorized) operation, you first get
>
> round(x*1000) = 2027
>
> which will be an exact integer and then
>
> xr = round(x*1000)/1000 = 2.0270
>
> if you are displaying in format short or even format long. However, if you look at xr more closely with, say,
>
> sprintf('%20.18f\n',xr)
>
> you will get 2.027000000000000135.
>
> What this means is that matlab is inherently incapable of actually realizing the fraction 2.027 exactly and the xr above is the closest it can possibly get to that as a number in binary floating point (double) format. However, I am guessing 'Someone's method would probably satisfy your needs in rounding your data.
>
> In discussing the rounding of numbers to various numbers of decimal places, there should always be a careful distinction made between doing so with the internal form of stored numbers and that of how they are displayed. The above actually alters the internal quantity. If it is only a matter of displaying numbers rounded to three decimals, that should be done using the 'fprintf', 'sprintf', or 'format' forms of accuracy in displaying numbers without actually making changes to the numbers themselves.
>
> Roger Stafford

Roger,
How exactly can the display of numbers within a table be changed using the 'format' option? I have a table within a GUI with popup menus and sliders that change the values in the table. I cannot seem to get the data in the table formatted to two digits, even using the rounding trick above. I have tried 'format bank', but the table still displays 4 or 5 digits to the right of the decimal. Any suggestions?
thanks,
Dave Smith

Subject: Rounding decimals off in multiple columns

From: Walter Roberson

Date: 15 Jan, 2009 22:09:31

Message: 6 of 7

David Smith wrote:

> How exactly can the display of numbers within a table be changed using the 'format' option?

Matlab uses "table" in different contexts, and it is not clear which of those contexts you
mean. None the less, in any version that I am aware of, in all of the places that -Matlab- would
refer to as a "table", the 'format' command has no effect. But perhaps you are referring
to the output of disp() of a matrix as a "table" (though Matlab would not call it
that): if so, then the 'format' command would have an effect on disp().

> I have a table within a GUI with popup menus and sliders that change the values in the table.
> I cannot seem to get the data in the table formatted to two digits, even using the rounding
> trick above. I have tried 'format bank', but the table still displays 4 or 5 digits to
> the right of the decimal. Any suggestions?

The way you write about having a "table" within a GUI, I -suspect- that you are referring
to a uitable(). The 'format' command does not affect uitable() constructs. If you
are trying to affect the format of a uitable, see the ColumnFormat uitable property.


--
.signature note: I am now avoiding replying to unclear or ambiguous postings.
Please review questions before posting them. Be specific. Use examples of what you mean,
of what you don't mean. Specify boundary conditions, and data classes and value
relationships -- what if we scrambled your data or used -Inf, NaN, or complex(rand,rand)?

Subject: Rounding decimals off in multiple columns

From: Cagri Gizli

Date: 3 Jan, 2013 21:37:08

Message: 7 of 7

"Ryan Neilson" <rneilson3@cogeco.ca> wrote in message <gjarn1$87n$1@fred.mathworks.com>...
> Hello:
>
> I'm trying to round-off data in columns to three decimals and was hoping for some useful assistance!
>
> In the array, I need to round 5 columns (columns #4-8) to three decimals places. There are 1,099 rows in these columns if that's helpful/useful whatsoever.
>
> Thanks in advance!

Thanks to everyone who is sharing lots of useful information in here. Just did lots of logging issue to be able to share this simple "excel trick": Want to cut of all unnecessary part of 0.1111111111111 just take mod in decimal precision I need 0.1 ,0.01 or 0.001.

Then subtract it from the number it self... Here you are

>> mod(0.111111111,0.01)

ans =

    0.0011

>> mod(0.111111111,0.001)

ans =

  1.1111e-004

>> 0.111111111-ans

ans =

    0.1110

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us