<blockquote>
<p>This one I am not sure how good the optimization of armadillo can do, it would do things like</p>

<p>arma::mat randomMat;// build a random matrix<br>
mask = randomMat; //copy the data of randomMat to mask</p>

<p>or overwrite the data of mask directly since the mask already allocate the buffer?</p>
</blockquote>

<p>I'm not sure what you mean... in that code snippet, if <code>mask</code> is already initialized, then it will overwrite the data.</p>

<p>In your example, yes, Armadillo is smart enough to avoid allocating memory because it is always the same size.  But if you were to choose the size randomly inside your <code>while</code> loop, then memory would be reallocated for every iteration.</p>

<p>Basically what I am getting at is that the <code>mask</code> matrix is unnecessary: it's only being used to store random values, which are being accessed sequentially and only once.  So why not just generate the random values as they are needed?  i.e.</p>

<pre><code>for (size_t i = 0; i &lt; input.n_elem; ++i)
{
  const double rand = math::Random();
  if (rand &gt; ratio)
    output = input[i];
  else
    output = 0.0;
}
</code></pre>

<p style="font-size:small;-webkit-text-size-adjust:none;color:#666;">&mdash;<br>Reply to this email directly or <a href="https://github.com/mlpack/mlpack/pull/463#issuecomment-151500400">view it on GitHub</a>.<img alt="" height="1" src="https://github.com/notifications/beacon/AJ4bFEBMEizXFosHgiudzPPuQfnrGZ2vks5o_3bJgaJpZM4GSg7s.gif" width="1" /></p>
<div itemscope itemtype="http://schema.org/EmailMessage">
<div itemprop="action" itemscope itemtype="http://schema.org/ViewAction">
  <link itemprop="url" href="https://github.com/mlpack/mlpack/pull/463#issuecomment-151500400"></link>
  <meta itemprop="name" content="View Pull Request"></meta>
</div>
<meta itemprop="description" content="View this Pull Request on GitHub"></meta>
</div>