a = [1 3 5 7 8 9 10 15 16];b = [2 4 14]; c = bsxfun(@minus,b',a) c(c<0)=nan;[~,ci] = min(c,[],2)d = a(ci)
Standard input is empty
c = 1 -1 -3 -5 -6 -7 -8 -13 -14 3 1 -1 -3 -4 -5 -6 -11 -12 13 11 9 7 6 5 4 -1 -2 ci = 1 2 7 d = 1 3 10
The brand new service which powers Ideone!
Widget for compiling and running the source code in a web browser!