Wes
Wes

Reputation: 41

How do you iterate through a 2D array from a specific point?

Sample Data Table

The following image was created rather hastily, but the premise of my question is outlined within. What I am looking to do is iterate through to a specific part in my CSV file I have loaded into Eclipse and once there, collect the data in that column and average the numerical values and print to the console. At first I had thought about manually entering ([1][1] + [1][2] + [1][3]) / 3, but this for obvious reasons would be too time consuming. Could I create a loop which moves over 1 column and down 1 row and then start computing from there?

Upvotes: 0

Views: 167

Answers (2)

Nathan
Nathan

Reputation: 41

I believe you must just specify the initial values of loop variables to be 2, unless I misunderstand the question.

Upvotes: 0

weston
weston

Reputation: 54781

As the data is given row by row in CSV, it would be best to process it in that way.

Psuedocode:

int columnCount = loadHeaders().length;
float[] totals = new float[columnCount];
int rowCount = 0;
for each row {
  rowCount++;
  for each column //skipping first
     totals[rowIndex] += row[columnIndex].data; //add cell to totals
}
float[] averages = totals / rowCount;

Upvotes: 1

Related Questions