To remove the loop, you can use bsxfun
, keeping the rest of your script:
res = sum(bsxfun(@lt,a',k).*bsxfun(@lt,b',k),1).';
Basically, it applies the operator less-than to the pairs of vectors (a,k) and (b,k); the transposition guarantees that you compare a column vector to a row one, leading to a matrix result. The reduction of the dot product is made along a specified direction (row direction). I transpose the result to have the same size as yours, but it is not mandatory.