I have a nested list like
matrix = {{1.2345664, 2.38673956, 3.567846}, {0.00034554, 124.4554654,
6.34543}, {0.0000233, 244.34454, 2}}
and I want to apply MatrixForm
, but also NumberForm[,{5,4}]
, because I want that in every element of the matrix the numbers are properly padded.
Is this possible in some way?
Interestingly perhaps, I want eventually write the resulting output list in a file.
P.S.: Now I write with Export["my_file.dat",Flatten[matrix]]
, and after I obtain the proper padding using a C program. But there must be a clever way...
Answer
You can do this a couple of different ways. This is easiest:
NumberForm[MatrixForm @ matrix, {5, 4}]
This works because NumberForm
affects sub-expressions including ones wrapped in MatrixForm
. You can do it the other way but it's more complicated:
Map[NumberForm[#, {5, 4}] &, matrix, {2}] // MatrixForm
You mention that you eventually want to export this. You should not need (or use) MatrixForm
for export. The question of export of NumberForm
formatted numbers may be answered here: How can I control the number format of exported data? In version 7 however the Accepted answer does not work. You might use something like:
Export[
"myfile.dat"
Map[ToString @ NumberForm[#, {5, 4}] &, matrix, {2}],
"Table"
]
Comments
Post a Comment