eliminate zeros in vector of vectors [modified]
-
Hi everyone, I'm using a vector of vectors to mimic a matrix using the following:
vector < vector <long double> > data_matrix;
I then add "rows" using the following:
for (int z=0;z<13;z++)
data_matrix.push_back(vector <long double>());Next, I load the appropriate values (from an input file) into the vector of vectors (some the values will be zeroes). What I'd like to do, is to search a particular "column" for zeroes and eliminate them in that column as well as the corresponding entries in the adjacent columns (so in effect, eliminate an entire row). I've figured out how to delete zeros from any given vector in the vector of vectors using:
bool IsEven (double d)
{
return (d) == 0;
}data_matrix[0].erase(std::remove_if(data_matrix[0].begin(),data_matrix[0].end(),IsEven), data_matrix[0].end());
Does anyone know how to do this :confused: The number of entries in each of the columns are the same. Thanks!
modified on Friday, May 21, 2010 12:43 PM
-
Hi everyone, I'm using a vector of vectors to mimic a matrix using the following:
vector < vector <long double> > data_matrix;
I then add "rows" using the following:
for (int z=0;z<13;z++)
data_matrix.push_back(vector <long double>());Next, I load the appropriate values (from an input file) into the vector of vectors (some the values will be zeroes). What I'd like to do, is to search a particular "column" for zeroes and eliminate them in that column as well as the corresponding entries in the adjacent columns (so in effect, eliminate an entire row). I've figured out how to delete zeros from any given vector in the vector of vectors using:
bool IsEven (double d)
{
return (d) == 0;
}data_matrix[0].erase(std::remove_if(data_matrix[0].begin(),data_matrix[0].end(),IsEven), data_matrix[0].end());
Does anyone know how to do this :confused: The number of entries in each of the columns are the same. Thanks!
modified on Friday, May 21, 2010 12:43 PM
Can you give us an example of what happens when you, for example, reduce a 3 by 3 matrix like: 1 1 1 0 2 2 3 3 3 Would you expect to end up with the following matrix? 1 1 1 3 3 3 If so you can do it with remove_if on the vector of rows - you remove any row that has a zero element in it. The predicate will have the form:
bool contains_zero( const std::vector &row )
{
....
}Cheers, Ash PS: If you're using a compiler that supports them you can write the predicate as a lambda rather than as a function. PPS: Just out of interest you can get rid of the loop:
for (int z=0;<13;z++) data_matrix.push_back(vector ());
Have a look for a constructor for vector that takes a size or consider using std::generate_n to do it all in one fell swoop. -
Can you give us an example of what happens when you, for example, reduce a 3 by 3 matrix like: 1 1 1 0 2 2 3 3 3 Would you expect to end up with the following matrix? 1 1 1 3 3 3 If so you can do it with remove_if on the vector of rows - you remove any row that has a zero element in it. The predicate will have the form:
bool contains_zero( const std::vector &row )
{
....
}Cheers, Ash PS: If you're using a compiler that supports them you can write the predicate as a lambda rather than as a function. PPS: Just out of interest you can get rid of the loop:
for (int z=0;<13;z++) data_matrix.push_back(vector ());
Have a look for a constructor for vector that takes a size or consider using std::generate_n to do it all in one fell swoop.Hi Ash, Thanks for replying. You're example matrix is what I'd expect. The way my vector of vectors is set up is that each of the vectors actually contains the data from the particular column (instead of row). This is why I seem to be having trouble. For example (when referring to your sample matrix): vector[0] would contain 1 0 3 vector[1] would contain 0 2 2 vector[2] would contain 3 3 3 So for example, the code and predicate that I previously posted which used "remove_if" would make vector[0] = 1 3 which would then give:
1 1 1
3 2 2
3 3but not:
1 1 1
3 3 3I should have mentioned that key piece of information (that the vectors hold columnar data instead of row date), sorry about that. Any ideas for this scenario :confused: Thanks,
-
Hi Ash, Thanks for replying. You're example matrix is what I'd expect. The way my vector of vectors is set up is that each of the vectors actually contains the data from the particular column (instead of row). This is why I seem to be having trouble. For example (when referring to your sample matrix): vector[0] would contain 1 0 3 vector[1] would contain 0 2 2 vector[2] would contain 3 3 3 So for example, the code and predicate that I previously posted which used "remove_if" would make vector[0] = 1 3 which would then give:
1 1 1
3 2 2
3 3but not:
1 1 1
3 3 3I should have mentioned that key piece of information (that the vectors hold columnar data instead of row date), sorry about that. Any ideas for this scenario :confused: Thanks,
Ouch, it's a bit messier but... - for each column use std::find to locate any zeros. Record the indices of the rows the zeros are in somewhere, maybe another vector or a perhaps a set. (std::find returns an iterator, you'll have to subtract column.begin() from that to get the index from the iterator). - for every row there's a zero in, zero every column element in that row. - for each column use the combination of std::remove_if and std::erase you presented in the question to eliminate the zeros. Hope that's clear enough - if not feel free to ask what I'm wittering on about, Ash
-
Ouch, it's a bit messier but... - for each column use std::find to locate any zeros. Record the indices of the rows the zeros are in somewhere, maybe another vector or a perhaps a set. (std::find returns an iterator, you'll have to subtract column.begin() from that to get the index from the iterator). - for every row there's a zero in, zero every column element in that row. - for each column use the combination of std::remove_if and std::erase you presented in the question to eliminate the zeros. Hope that's clear enough - if not feel free to ask what I'm wittering on about, Ash