2

I have a correlation matrix, which I want to display in the following format:

  • 2 decimal places (e.g., -0.56)
  • number signs "+" and "-" (e.g., +0.76 or -0.34)

I've tried many different things, but I had no success so far... The code I'm using to generate the data is:

randomWalk[x_] := Accumulate[Prepend[RandomVariate[NormalDistribution[0, 1], x], 0]]
exchange = Table[Subscript[asset, i] = randomWalk[500], {i, 1, 5}];

Now the code I'm using to generate the correlation matrix:

N[Correlation[Transpose[exchange][[1 ;; 500, All]]], 2]//MatrixForm

However, the numbers generated are displayed with $MachinePrecision, and not 2 decimal places (although I've used N[#,2]). I've tried another code:

MatrixForm[Round[Correlation[Transpose[exchange][[1 ;; 500, All]]], 0.01]]

In this case I get the correlation matrix with 2 decimal places, but it's still missing the "+" signal before the number. So I've tried this last code:

NumberForm[Round[Correlation[Transpose[exchange][[1 ;; 500, All]]], 0.01],NumberSigns -> {"-", "+"}]//MatrixForm

In this last case I get the numbers correctly formatted, but I'm not able to put them in MatrixForm. Can someone give me a hint to solve this?

Rod
  • 3,318
  • 2
  • 24
  • 40

2 Answers2

5

Of course there are many ways of doing this. Here's one:

mat = RandomReal[{-1, 1}, {4, 4}]
Table[NumberForm[mat[[i, j]], {3, 2}, NumberSigns -> {"-", "+"}], 
     {i, 1, 4}, {j, 1, 4}] // MatrixForm

You get:

enter image description here

bill s
  • 68,936
  • 4
  • 101
  • 191
  • Imagine you have mat defined... try to use: NumberForm[mat, NumberSigns -> {"-", "+"}] // TableForm. It doesn't work... – Rod Apr 09 '13 at 14:47
  • what's wrong with the output of the above, either Table or Map? – bill s Apr 09 '13 at 14:50
  • You've generated numbers with 2 decimal places... try something with $MachinePrecision... – Rod Apr 09 '13 at 14:52
  • mat has full real precision, it's the argument {3,2} that does the truncating to 3 significant digits plus two to the right of the decimal place. – bill s Apr 09 '13 at 14:54
  • Ok, but the problem is: I'm already using mat[[1 ;; 500, All]] inside a DynamicModule environment... so, I can't use Table[mat[[i,j]],{i,1,4},{j,1,4}]... – Rod Apr 09 '13 at 15:06
  • 1
    @RodLm What would be the problem there? You understand bill's matrix is just an example, right? We wouldn't like to display a 500x5 matrix here just to be closer to your matrix – Sjoerd C. de Vries Apr 09 '13 at 15:37
  • Yes, I understand that perfectly... the problem, as said, is: I'm running exchange[[1 ;; 500, All]] inside a DynamicModule environment, but instead of 500 I have n++ (this means I compute my correlation matrix dinamically). So, how could I use Table together with DynamicModule, once I use mat[[1;; n++,All]]? As far as I have tried, its not possible to use Table[NumberForm[mat[[i, j]][[1;;n++, All]], {3, 3},NumberSigns -> {"-", "+"}], {i,1, 4}, {j, 1, 4}] // MatrixForm... – Rod Apr 09 '13 at 15:46
  • @andre Thanks for the solution... somehow MatrixForm outside NumberForm doesn't work... We just need to put it inside NumberForm, as he suggested: NumberForm[MatrixForm[Round[...,NumberSigns->{"-","+"}]... – Rod Apr 09 '13 at 16:07
4

It is perfectly possible to wrap NumberForm around MatrixForm :

NumberForm[MatrixForm[Round[...,NumbersSigns->{"-","+"}] does the job

andre314
  • 18,474
  • 1
  • 36
  • 69