function L = recursiveBinarySearch(x, v)
% Find position after which to insert x (or find position of x in v).
% x is the scalar search target.
% v is a vector of numbers sorted in ascending order.
% L is the index such that v(L) <= x < v(L+1);
% L=0 if x<v(1). If x>v(end), L=length(v) but x~=v(L).

if isempty(v)
L = 0;
else
m= floor((length(v) + 1)/2); % middle of search window
if v(m) <= x
% Remember what page we were on, then rip out the first half
L= m + recursiveBinarySearch(x, v(m+1:length(v)));
else
% Rip out the second half
L= recursiveBinarySearch(x, v(1:m-1));
end
end