0

I have a CSV file which contains a matrix of correlation coefficients between a set of objects (in the example below it is four objects u101, u102, u103, u104). The files uses the object names as row names and column names like so:

        u101,   u102,   u103,   u104
u101,   1.0,    0.2,    0.1,    0.4
u102,   0.2,    1.0,    0.5,    0.8
u103,   0.1,    0.5,    1.0,    0.9
u104,   0.4,    0.8,    0.9,    1.0

What I now need to do is: read the CSV file into some matrix format in Java such that I can access the correlation coefficients by name. Basically, I need to implement a function:

double getValue(String arg0, String arg1) {
    […]
}

When invoked with

getValue("u101", "u104")

the function should then return 0.4.

How can I do that?

Chris
  • 1,479
  • 2
  • 15
  • 19
  • [Read 2D array from file](http://stackoverflow.com/questions/4769976/reading-2-d-array-from-a-file) (you'll probably want to use `next` for row/col names and `nextDouble` for cells). Convert String to row/col index - `Map`. If the naming for row/col `i` is always `u(101+i)`, then you can simply `substring` and `parseInt`. – Bernhard Barker Apr 03 '13 at 14:29

2 Answers2

1

You would need 2 maps and a 2 dimensional array (or arraylist).

  • 1st map should contain the col name as the key and the col position as value i.e. ('u101', 0), ('u102', 1) etc
  • 2nd map should contain the row name as the key and the row postion as value
  • The 2d array should have the data, i.e. the numbers themselves

So when you get a call to your method, you do like

return myArray[rowmap.get('rowname')][colmap.get('colname')];
Pradeep Pati
  • 5,779
  • 3
  • 29
  • 43
0

To read doubles from a file: Reading double values from a file

Then store the values you read in a HashMap of doubles. Construct the hashmap keys using both the row and column name. To access the element at u102, u103 do:

hashmap.put("u102-u103", 0.5);
x = hashmap.get("u102-u103");
Community
  • 1
  • 1
David
  • 943
  • 1
  • 10
  • 26