Clear Filters
Clear Filters

matlab dataset

1 view (last 30 days)
Sami
Sami on 11 Jun 2011
Hallo,
I have a huge dataset, for example matrix A, and the numbers are with 15 significat digits after the point. I want to have a new matrix B where the data is with 4 significatn digits after the point of course with the correct approximation
example: if one value of the A matrix is 23.861999511718750 I want that the value in the B matrix is 23.8620 if a value is 39.934001922607420 i want it to be 39.9340 in the B matrix. Thanks in advance
Sam

Answers (2)

Walter Roberson
Walter Roberson on 11 Jun 2011
You can fool yourself with
B = round(A*10000)/10000;
but there simply isn't any real way of doing what you want in a binary floating point system. For example,
>> sprintf('%.99g\n',23.8620)
ans =
23.861999999999998323119143606163561344146728515625
This will display as the 23.8620 that you want, but what is actually stored is a binary value whose decimal equivalent is the one I show.

the cyclist
the cyclist on 11 Jun 2011
By default, MATLAB numerical arrays are stored as double precision, with about 15 digits of precision (not necessary after the decimal point).
If you are primarily worried about the memory requirements of storing your large matrix, then you can convert that double-precision value to single precision, using the single() function. That will result in about 7 digits of precision, and about half the memory requirement.
Regardless of how the variable is stored, there are options to display it with however many digits you want. You might start with the format() command, but there are others.
If the answer is not helpful, you should provide more details about exactly what you are trying to accomplish. If the answer was helpful, please consider accepting it.

Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!