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?

1
  • Read 2D array from file (you'll probably want to use next for row/col names and nextDouble for cells). Convert String to row/col index - Map<String, Integer>. If the naming for row/col i is always u(101+i), then you can simply substring and parseInt. Commented Apr 3, 2013 at 14:29

2 Answers 2

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')];
Sign up to request clarification or add additional context in comments.

Comments

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");

1 Comment

It may help to say a HashMap of what exactly you're suggesting.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.