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
std::bitset
fordropoutmask
if it's just a flagstd::bitset
would trade speed for space. As always, measure performance using a profiler. If heap usage is a hogstd::bitset
might be a better choice. If nothing else, usechar
notint
as the mask member. Usingint
for a 1/0 flag is quite wasteful.