I noticed a bug in the histogram computation. Gradient vectors whose angle is equal to pi are being omitted from the histogram because you check for angle < angle_lim instead of angle <= angle_lim.

I also re-wrote the histogram computation to optimize it in a way similar to what was suggested in an earlier comment.

% Compute the bin index for every angle.
binIndeces = ceil((v_angles + pi) / binSize);

% For each bin
for (bin = 1:B)
% Get the magnitudes for all the pixels that belong in 'bin'.
magnitudes = v_magnit(binIndeces == bin);

% Add all of the magnitudes to 'bin'.
H2(bin) = H2(bin) + sum(magnitudes);
end