0

I have a double valued diagonal matrix stored in a text file.

size(file)~410 Mo

I would like to reduce the size by rounding my double values.

If its a good idea, how to do it in java

0.1706524958886193=>0.17

I need to use this file later in matlab

when i try

dlmread(file) i get out of memory error

nawara
  • 1,157
  • 3
  • 24
  • 49

3 Answers3

5

If you round the values you are throwing away precision. That may change the results you will get.

A better approach is to store only the diagonal items. There is no point in storing the n^2-n off-diagonal zeroes. Use the diag function to convert a vector into a diagonal matrix. http://www.mathworks.es/es/help/matlab/ref/diag.html

Even more efficient: store the numbers in a binary format instead of text.

Joni
  • 108,737
  • 14
  • 143
  • 193
  • +1 because I agree that reducing to 2 decimals might alter the results. – Schorsch Jun 06 '13 at 18:22
  • great idea but ....the same problem with function diag =out of memory – nawara Jun 06 '13 at 19:48
  • Then the matrix is just too big to process with the memory currently assigned to Matlab. There's a pagewith information on how to give Matlab more memory and tips on how to reduce memory cconsumption here: http://www.mathworks.es/es/help/matlab/matlab_prog/resolving-out-of-memory-errors.html – Joni Jun 06 '13 at 20:29
  • yes thanks , i asked about this problem [here](http://stackoverflow.com/questions/16971111/out-of-memory-error-when-using-diag-function-in-matlab/16974023?noredirect=1#16974023): – nawara Jun 07 '13 at 08:43
2

Use DecimalFormat class to format the double value to your needs. For example, if you want to keep only 2 digits after decimal point use, "#0.00 and so on. DecimalFormat#format class returns String output which you can use to instantiate Double value. from

import java.text.DecimalFormat;
public class Tester
{
  public static void main(String arg[]) throws Throwable
  {
    double bigDouble=0.1706524958886193;
    DecimalFormat df=new DecimalFormat("#0.00");
    String numberString=df.format(bigDouble);
    double smallDouble=new Double(numberString);
    System.out.println(smallDouble);
  }
}

Hope this helps...

pinkpanther
  • 4,770
  • 2
  • 38
  • 62
  • +1 As it is the best solution to round numbers. – eternay Jun 06 '13 at 17:54
  • +1 and please to add implemented methods in [NumberFormat](http://stackoverflow.com/a/8582656/714968), otherwise you have to use BigDecimal – mKorbel Jun 06 '13 at 17:54
  • [true is](http://stackoverflow.com/a/7553827/714968) by @EJP – mKorbel Jun 06 '13 at 17:56
  • @mKorbel please to add? I didn't get you...you want' me to edit my answer or? – pinkpanther Jun 06 '13 at 18:04
  • not there isn't something from my linked answer, nor to edit something, do you have any issue with my comments or – mKorbel Jun 06 '13 at 18:41
  • @mKorbel `please to add implemented methods in NumberFormat, otherwise you have to use BigDecimal`... I didn't get that sentence...I don't have an issue :) – pinkpanther Jun 06 '13 at 18:48
  • @pinkpanther ignore that, doesn't matter, empty answer – mKorbel Jun 06 '13 at 18:51
  • exception `Exception in thread "main" java.lang.NumberFormatException: For input string: "0,17"` – nawara Jun 06 '13 at 19:48
  • @nawara use the methods from the NumberFormat class for convertion....note that DecimalFormat is a subclass of number format.. see this question for details http://stackoverflow.com/questions/888088/how-do-i-convert-a-string-to-double-in-java-using-a-specific-locale – pinkpanther Jun 07 '13 at 05:41
0

Unless I'm misunderstanding the question you can do it like this:

double value = 0.1706524958886193;
value = (double)((int)(value*100))/100; // returns 0.17
Josh Maggard
  • 419
  • 3
  • 9