function [ nextx ] = pf_resample( currx, w )
%PF_RESAMPLE Basic resampling

% number of samples:
M = size(currx, 3);

% Redraw particles:
nextx = zeros(3, 1, M);

% Cumulative sum of weights:
cum = cumsum(w);

for p = 1:M
    r = rand();
    % Find first index for which the cumulative sum is >= r. If this ever
    % fails due to unmerical inaccuracies, we should take the last element.
    ind = min(find (cum >= r, 1, 'first'), M);
    nextx(:,:,p) = currx(:,:,ind);
end

end

