Hello,
I have a matrix of int (vector of vector); the problem is that I need to compute the mean of each column (the matrix can be 500 x200); so to do this I did:
foreach (iter_column) {
float sum = 0;
iter_line.setFirstLine;
foreach(iter_line) {
sum += iter_line[iter_coulmn]. value;
}
mean = sum / nOfLine;
}
foreach (iter_column) {
float sum = 0;
iter_line.setFirstLine;
foreach(iter_line) {
sum += iter_line[iter_coulmn]. value;
}
mean = sum / nOfLine;
}
To copy to clipboard, switch view to plain text mode
The problem is that I have to do "two for" one inside other; I thought If is better to do something like to tie a vector<coulum> and do:
class Column {
public:
vector<double> _value;
};
vector<Column> col; //to fill col properly
foreach (col) {
int sum = std::accumulate(col.begin(), col.end(), 0);
mean = sum / col.size();
}
class Column {
public:
vector<double> _value;
};
vector<Column> col; //to fill col properly
foreach (col) {
int sum = std::accumulate(col.begin(), col.end(), 0);
mean = sum / col.size();
}
To copy to clipboard, switch view to plain text mode
Will be the second way better/faster? Any other ways to improve?
Bookmarks