Services & Resources / Wolfram Forums / MathGroup Archive
-----

MathGroup Archive 2012

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: silly question on formatting tables

  • To: mathgroup at smc.vnet.net
  • Subject: [mg129099] Re: silly question on formatting tables
  • From: Bill Rowe <readnews at sbcglobal.net>
  • Date: Sat, 15 Dec 2012 05:49:18 -0500 (EST)
  • Delivered-to: l-mathgroup@mail-archive0.wolfram.com
  • Delivered-to: l-mathgroup@wolfram.com
  • Delivered-to: mathgroup-newout@smc.vnet.net
  • Delivered-to: mathgroup-newsend@smc.vnet.net

On 12/14/12 at 3:01 AM, alba.maydeu at gmail.com wrote:

>Consider the matrix

>{{0, 0, 1.19092}, {0.883038, 0, 0.366804}, {0, 0, 0}}

>How can I display it in matrixform/tableform so that the integers
>are displayed as integers and the reals are displayed only with 2
>decimals?

>If I use MatrixForm, the reals are displayed with 6 decimals, and if
>I use TableForm all numbers, including integers are displayed with 2
>decimals.

Here are two ways to achieve what you want. First:

In[1]:= m = {{0, 0, 1.19092}, {0.883038, 0, 0.366804}, {0, 0, 0}};
m /. a_Real :> NumberForm[a, {3, 2}] // MatrixForm

But notice

In[3]:= Union@Flatten@Map[Head, %, {2}]

Out[3]= {Integer,NumberForm}

That is, NumberForm is a wrapper for the reals which means you
cannot simply use the end result in further computation. So,

({{0, 0, 1.19092}, {0.883038, 0, 0.366804}, {0, 0, 0}} /.
    a_Real :> NumberForm[a, {3, 2}]) + {{0, 0, 1}, {1, 0, 2},
{0, 0,
    0}}

likely has an output you wouldn't want. You can "fix" this by doing:

{{0, 0, 1.19092}, {0.883038, 0, 0.366804}, {0, 0, 0}} /.
  a_Real :> Round[a, .01]

But this changes the values of the real elements which may not
be something you want. Of course by assigning the matrix to a
variable then applying NumberForm etc. you can control the
display and still have the matrix unchanged for further computation.




  • Prev by Date: how to define step size in FindMinimum
  • Next by Date: Re: Mathematica 9 does not read mathematica 8 ".mx" files
  • Previous by thread: Re: silly question on formatting tables
  • Next by thread: Re: silly question on formatting tables