
I need to remove rows from a matrix if it fails a comparison. I have two possible options to remove the matrix rows. Option 1 adds a row to the new matrix for each row from the old matrix that passes the comparison. Option 2 keeps track of the rows from the old matrix that passed the comparison and then creates a new matrix all at once. I'm wondering which option is more efficient or is there a simpler way to achieve the same results? Ryan Option 1 void( matrix<double> const& H, matrix<double> const& P, matrix<double> const& R, vector<double> const& v) { double const rejection_criterion = 2; matrix<double> M = prod(H, matrix<double>(prod(P, trans(H))) + R; matrix<double> result; int matrix_size = 1; for (unsigned i = 0; i < v.size(); ++i) { if ( v[i] * v[i] < rejection_criterion * M(i,i)) { result.resize(matrix_size, H.size2(), true); matrix_row<matrix<double> > oldH (H, i); matrix_row<matrix<double> > newH (result, matrix_size - 1); newH = oldH; matrix_size++; } } } Option 2 void( matrix<double> const& H, matrix<double> const& P, matrix<double> const& R, vector<double> const& v) { double const rejection_criterion = 2; matrix<double> M = prod(H, matrix<double>(prod(P, trans(H))) + R; list<unsigned> goodrows; for (unsigned i = 0; i < v.size(); ++i) { if ( v[i] * v[i] < rejection_criterion * M(i,i)) { goodrows.push_back(i); } } matrix<double> result(goodrows.size(), H.size2()); for ( unsigned i = 0, list<unsigned>::iterator iter = goodrows.begin(); i < result.size1(); ++i, ++iter) { matrix_row<matrix<double> > oldH (H, *iter); matrix_row<matrix<double> > newH (result, i); newH = oldH; } }