Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

I have a list of matrices

a = {{{1, 0.0027439,  0.145732, 0.231707,31.8}},
     {{1, 0.00275229, 0.146177, 0.232416, 31.7}}, 
     {{1, 0.00276074, 0.146626, 0.233129, 31.6}}, 
     {{1, 0.00276923, 0.147385, 0.233846, 31.5}}, 
     {{1, 0.00277778, 0.14784,  0.234568, 31.4}}, 
     {{1, 0.00278638, 0.148297, 0.235294, 31.3}}}

For the 2nd, 3rd and 4th element of each row, I want to only keep 2 places after the decimal, so the list looks like

anew={{{1, 0.00, 0.15, 0.23, 31.8}}, 
      {{1, 0.00, 0.15, 0.23, 31.7}}, 
      {{1, 0.00, 0.15, 0.23, 31.6}}, 
      {{1, 0.00, 0.15, 0.23, 31.5}},
      {{1, 0.00, 0.15, 0.23, 31.4}},
      {{1, 0.00, 0.15, 0.24, 31.3}}}

I have tried applying pa = PaddedForm[a[[All, All, {2, 3, 4}]], {2, 2}] but the output is simply the list of 2nd, 3rd and 4th elements. How do I retain the 1st and 5th element in the output as shown above?

Thanks

EDIT 1 Since my approach with PaddedForm is sort of the right result, but is missing the 1st and 5th elements, I was wondering a naive way might be to join a[[All, All, 1] and a[[All, All, 5]] to pa. Can anyone help me with this?

EDIT 2 I am only trying to plot these data points, therefore the exact form of the numbers shown is not necessary.

share|improve this question
2  
See my comments on the answer below - is this for display only, or do you want to later operate on the results? Treating numbers massaged for display as just numbers can open a whole can of worms... –  ciao Jul 17 at 22:52
    
I understand that you want to plot these values. I wonder, are you sure that you want to reduce the precision of your data? You are losing some information that way. –  MarcoB Jul 17 at 23:04
    
The idea is, these are particles that move very slightly as time progresses. I am not interested in their movements, so I am reducing the precision of the data to "hide" these movements. –  HuShu Jul 17 at 23:12

2 Answers 2

up vote 1 down vote accepted

If you want to round the 2nd through 4th row, try:

MapAt[Round[#, .01] &, a, {{2}, {3}, {4}}]

or

MapAt[Round[#, .01] &, a, {#} & /@ Range[2, 4]]

or

MapAt[Round[#, .01] &, a, List /@ Range[2, 4]]

Output:

{{{1, 0.0027439, 0.145732, 0.231707, 31.8}}, 
 {{1., 0., 0.15, 0.23, 31.7}}, 
 {{1., 0., 0.15, 0.23, 31.6}}, 
 {{1., 0., 0.15, 0.23, 31.5}}, 
 {{1, 0.00277778, 0.14784, 0.234568, 31.4}}, 
 {{1, 0.00278638, 0.148297, 0.235294, 31.3}}}

If you want to round the 2nd through 4th element in each row:

Map[Round[#, .01] &, a]

{{{1., 0., 0.15, 0.23, 31.8}}, 
 {{1., 0., 0.15, 0.23, 31.7}}, 
 {{1., 0., 0.15, 0.23, 31.6}}, 
 {{1., 0., 0.15, 0.23, 31.5}}, 
 {{1., 0., 0.15, 0.23, 31.4}}, 
 {{1., 0., 0.15, 0.24, 31.3}}}
share|improve this answer
2  
Neither of these seems to produce the desired result, am I missing something? –  ciao Jul 17 at 22:42
    
Look at the OP anew... "For the 2nd, 3rd and 4th element of each row". In addition, 0. is not 0.00, nor is 1. 1, though I think the OP needs to clarify if this is for display purposes only, or they want to do calculations against it - the latter form can be had via numberform, etc, but that may cause machinations later... –  ciao Jul 17 at 22:46
    
The last one works! Thanks!:) –  HuShu Jul 17 at 22:51
1  
No need to map, just Round[a,.01]. Poor example in OP, much back-and-forth could have been saved by making it clear an exact first element (as shown in the example) was not needed. Ugh. –  ciao Jul 17 at 22:55
1  
@HuShu: No, since this is actually rounding (and see my comment above - no need to map). But your example shows exact first elements (not rounded), and 0.00, not 0., so a reader would be lead to believe you needed it that way. could have saved time and effort stating that was not the case, then Round[a,.01] suffices.... –  ciao Jul 17 at 22:58

As an alternative to David's approach with Map, you could also extract those parts of the matrices whose precision you want to change, and then use the rounded values to update the values in the original matrices:

a[[All, All, 2 ;;]] = Round[a[[All, All, 2 ;;]], 0.01]

(* Out:
{{{1, 0., 0.15, 0.23, 31.8}},
 {{1, 0., 0.15, 0.23, 31.7}},
 {{1, 0., 0.15, 0.23, 31.6}},
 {{1, 0., 0.15, 0.23, 31.5}},
 {{1, 0., 0.15, 0.23, 31.4}},
 {{1, 0., 0.15, 0.24, 31.3}}}
*)

If, as you say in comments, you are just going to plot these values, then you don't care about the precision of the first value. In that case, the much simpler Round[a, 0.01] will do just fine!

share|improve this answer
    
Yes, my bad. I did that manually, and missed that. –  HuShu Jul 17 at 23:01

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.