MATLAB: Eucliedan Distances In two Arrays

euclidean distance

I have an array of (X-Y) Coordinates,
Observed_Signal_Positive_Inflection_Points_Coordinates =
0.1040 -0.0432
0.2090 -0.0264
0.3140 -0.0096
0.4180 -0.0527
0.5230 -0.0359
0.6280 -0.0191
0.7330 -0.0023
0.8370 -0.0455
0.9420 -0.0287
Using each of the 9 coordinates I want to find its distances from a second array of (X-Y) coordinates (D = sqrt(X^2+Y^2))
Positive_Inflection_Points_Coordinates_denoised =
0.0020 0.8093
0.0040 0.7637
0.0070 0.7494
0.0130 0.4747
0.0250 0.6108
0.0260 0.6134
0.0980 -0.1331
0.1000 0.0740
0.1030 0.1959
0.1880 -0.5077
0.1980 -0.2024
0.2020 0.1651
0.2060 0.2103
0.2090 0.3228
0.2120 0.4626
0.2970 -0.5625
0.3050 -0.3444
0.3130 -0.0907
0.3150 0.0769
0.3200 0.2399
0.3950 -0.7348
0.4000 -0.6530
0.4130 -0.2682
0.4150 -0.1705
0.4170 -0.0756
0.4190 0.0999
0.4200 0.1384
0.4220 0.2145
0.4260 0.4140
0.5010 -0.7668
0.5150 -0.4427
0.5190 -0.2756
0.5240 -0.0631
0.5260 0.0475
0.5290 0.1839
0.6030 -0.5451
0.6080 -0.5282
0.6260 -0.0955
0.6280 0.0680
0.6320 0.2191
0.6530 0.7563
0.7240 -0.4235
0.7300 -0.1596
0.7330 -0.0320
0.7350 0.0883
0.7380 0.2280
0.8310 -0.2144
0.8320 -0.1546
0.8340 -0.0583
0.8600 0.6336
0.8620 0.6169
0.9320 -0.5955
0.9330 -0.5314
0.9340 -0.4676
0.9370 -0.2955
0.9410 -0.1334
0.9430 0.1233
0.9460 0.1775
Using each coordinate from the first, I want to find the minimal Euclidean Distance from the second set. How do I do this given that both arrays are of different length? Basically, I will have a final set of X-Y Coordinates (9 in total) that minimize the euclidean distance based on testing each of the first coordinates against every single set in the second.

Best Answer

There are more sophisticated solutions, but what about a simple loop?
X = Observed_Signal_Positive_Inflection_Points_Coordinates;
Y = Positive_Inflection_Points_Coordinates_denoised;
nX = size(X, 1);
Result = zeros(1, nX)
for k = 1:nX
tmp = (X(k, 1) - Y(:, 1)) .^ 2 + (X(k, 2) - Y(:, 2)) .^ 2;
[dummy, Result(k)] = min(tmp, [], 1);
end
Or in R2016b:
tmp = sum((X(k, :) - Y) .^ 2, 2);
Note: You can omit the expensive sqrt(), because it does not change the property of beeing the minimum.
Related Question