MATLAB: Matching closest values to each other

data analysismatchingmatrix comparison

I have two vectors:
A = [36.1920000000000;36.8920000000000;37.8420000000000;38.4920000000000;39.3420000000000;39.9420000000000;40.7920000000000;41.3420000000000;42.1920000000000;42.7920000000000;43.6920000000000;44.2920000000000;45.1420000000000;45.7420000000000;46.6420000000000;47.3420000000000;48.2920000000000;48.9420000000000;49.8420000000000;50.5420000000000;51.5420000000000;52.1420000000000;53.0920000000000;53.6920000000000;54.6920000000000;55.3920000000000;56.3420000000000;56.9920000000000;57.8920000000000;58.4920000000000;59.3420000000000;59.9420000000000;60.7920000000000;61.3920000000000;62.2420000000000;62.8920000000000;63.7420000000000;64.3920000000000;65.2420000000000;65.8420000000000;66.7420000000000;67.3420000000000;68.1920000000000;68.7920000000000;69.7420000000000;70.3420000000000;71.1920000000000;71.8420000000000;72.7920000000000;73.4420000000000]
B = [39.1670000000000;39.8920000000000;40.5920000000000;41.3000000000000;42.0080000000000;42.7250000000000;43.4920000000000;44.2250000000000;44.9580000000000;45.6920000000000;46.4830000000000;47.2920000000000;48.1250000000000;56.9830000000000;57.7500000000000;58.5080000000000;59.1750000000000;59.9420000000000;60.6670000000000;61.4000000000000;62.1250000000000;62.9000000000000;63.6500000000000;64.3830000000000;65.1170000000000;65.8750000000000;66.6330000000000;75.0170000000000]
I want to match every value of B to the closest value in A, next to it in the second column. How to do this?
Thanks

Best Answer

For larger vectors, where BSXFUN will require a lot of memory, function NEARESTPOINT might be useful
A = rand(1000000,1) ; B = rand(1000000,1) ; % bsxfun will fail here!
i = nearestpoint(B, A) ;
% check
r = randperm(numel(B), 10) ; % 10 random numbers in B
[~, j] = min(abs(bsxfun(@minus, B(r), A')),[],2) % might be tricky already ...
[i(r) j] % equal :-)
See it's speed using
nearestpoint('test')