lundi 5 décembre 2016

Creating a random std::vector with ones and zeros C++

I want to create a vector of random 1's and 0's in a proportion set by me (in the program I called it dropout) The vector is the same size of a previously created vector CSUM.

in MATLAB it would be

dropout=0.9;
n_elements=size(CSUM)
drpoutmask = (rand(n_elements) > dropout); 

in C++ I have

size_t elements = Csum.size();
std::vector<float> y(elements);
std::uniform_real_distribution<float> distribution(0.0f, 1.0f); 
std::mt19937 engine; // Mersenne twister MT19937
auto generator = std::bind(distribution, engine);
std::generate_n(y.begin(), elements, generator);
std::vector<int> dropoutmask(elements,0);
float dropout=0.9;

for(int i=0; i<elements; i++)
  {
  if(y.at(i)>dropout)
    {
    dropoutmask.at(i)=1;
    }
  }
}

which works but for huge vectors is very very slow, is there a faster way to do this? I am very new at C++.

Any help will be much appreciated




Aucun commentaire:

Enregistrer un commentaire