Matching closest values to each other

4 vues (au cours des 30 derniers jours)
JamJan
JamJan le 15 Oct 2019
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

Réponse acceptée

Jos (10584)
Jos (10584) le 15 Oct 2019
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')

Plus de réponses (1)

Stephen23
Stephen23 le 15 Oct 2019
>> [~,idx] = min(abs(bsxfun(@minus,B,A.')),[],2);
>> C = [B,A(idx)]
C =
39.167 39.342
39.892 39.942
40.592 40.792
41.300 41.342
42.008 42.192
42.725 42.792
43.492 43.692
44.225 44.292
44.958 45.142
45.692 45.742
46.483 46.642
47.292 47.342
48.125 48.292
56.983 56.992
57.750 57.892
58.508 58.492
59.175 59.342
59.942 59.942
60.667 60.792
61.400 61.392
62.125 62.242
62.900 62.892
63.650 63.742
64.383 64.392
65.117 65.242
65.875 65.842
66.633 66.742
75.017 73.442

Catégories

En savoir plus sur Matrix Indexing dans Help Center et File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by