Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. eliminate zeros in vector of vectors [modified]

eliminate zeros in vector of vectors [modified]

Scheduled Pinned Locked Moved C / C++ / MFC
graphicstutorial
5 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • B Offline
    B Offline
    b rad311
    wrote on last edited by
    #1

    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

    A 1 Reply Last reply
    0
    • B b rad311

      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

      A Offline
      A Offline
      Aescleal
      wrote on last edited by
      #2

      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.

      B 1 Reply Last reply
      0
      • A Aescleal

        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.

        B Offline
        B Offline
        b rad311
        wrote on last edited by
        #3

        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 3

        but not:

        1 1 1
        3 3 3

        I 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,

        A 1 Reply Last reply
        0
        • B b rad311

          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 3

          but not:

          1 1 1
          3 3 3

          I 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,

          A Offline
          A Offline
          Aescleal
          wrote on last edited by
          #4

          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

          B 1 Reply Last reply
          0
          • A Aescleal

            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

            B Offline
            B Offline
            b rad311
            wrote on last edited by
            #5

            Thanks Ash, Sounds good, I'll give this a try.

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups