Question

I observe errors like this pretty often when I grade papers (and definitely make them myself on occasion):

% any vectorized language, such as MATLAB/Octave/R that supports logical indexing
Y = rand(1,10); % random numbers drawn uniformly from the interval [0,1] in a 1x10 vector
Y(Y<=0.6) = 1;
Y(Y>0.6) = 0; % These two lines try to convert the distribution from uniform to Bernoulli

This code results in a vector of zeros, not the desired random binary vector whose distribution approximates a Bernoulli random variable.

The problem, from a programming perspective, is in the in-place modification; the coder skips a mental step. (There are other ways to solve this, based on the mathematics the code is supposed to accomplish, or by switching the conditionals, etc., but those are all application specific.) A unit test would lay the error bare, but I've been unable to convince any students to try them (it's not a programming class, specifically). Code that first allocates an output vector and fills it based on the input vector would prevent this from happening:

X = rand(1,10); % random numbers in a 1x10 vector
Y = zeros(size(X));
Y(X<=0.6) = 1;
Y(X>0.6) = 0;

However, this is guaranteed to take twice the space, although I would say it better fits the mental model of the mistaken coder here, and in general is easier to figure out than a really intricate algorithm that wastes no space at all (see the CLRS illustration of quicksort).

Are there any compilers/interpreters that optimize code like the second snippet to reduce the required space to that of an in-place operation?

Was it helpful?

Solution

Yes, there are. There are a few different optimizations that are in the area you discuss:

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top