Y = [0 0 0 0.5 0.5];
X = [0 0 0 1 1];
absdiff = abs(X-Y);
pos = find(max(absdiff) == absdiff); % or [trash,pos] = ismember(max(absdiff),absdiff);
pos(randi(numel(pos),1))
Try to execute several times to see the random selection between position 4 and 5.