I come from a Matlab world where working on arrays in extremely fast and I want to do the same with a C++ function. Basically I have an array of values and I want to average each row of this array (while omitting the zeros). In Matlab that would look something like:
a = ones(rowSize, ColSize)
%* fill values *%
mean(a)
Which would give me a vector that has as many columns as a
and would contains the average value of each column in a matter of milliseconds regardless of the matrix size.
To remove the zeros I would create a vector to tell me where they are and apply the mean function only when appropriate
To do the same in C++, my first instinct would be a loop that would roughly look like this:
int rowSize; // random value
int colSize; // random value
float num;
float denum;
vector<vector<float>> vec(rowSize, vector<float>(colSize));
vector<float> mean;
// * fill values *//
for (int i = 0; i < vec.size(); i++) {
num = 0;
denum = 0;
for (int j = 0; j < vec[i].size(); j++)
{
if (vec[i][j] != 0)
{
num += vec[i][j];
denum++;
}
}
mean[i] = num / denum;
}
But that would take forever with big arrays. So considering each loop iteration is independent from the other one is there a way to do that in a faster way? And if possible in a transparent way like in Matlab... I am struggling to find the right resources for that.
Thank you in advance.