Clear Filters
Clear Filters

Eucliedan Distances In two Arrays

4 views (last 30 days)
Osita Onyejekwe
Osita Onyejekwe on 18 Nov 2016
Answered: Greg Dionne on 18 Nov 2016
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.

Accepted Answer

Jan
Jan on 18 Nov 2016
Edited: Jan on 18 Nov 2016
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.
  2 Comments
Osita Onyejekwe
Osita Onyejekwe on 18 Nov 2016
thank you so much. That works. Now can you help me index the coordinates in the second array associated with the minimized distance
dpb
dpb on 18 Nov 2016
That's what the Result above is for each of the values in X.
Or see alternate solution...which also returns them as the second optional output.

Sign in to comment.

More Answers (2)

dpb
dpb on 18 Nov 2016
Given first/second sets are X,Y, respectively,
[D,I]=pdist2(Y,X,'euclid','smallest',1); % doc pdist2 for details

Greg Dionne
Greg Dionne on 18 Nov 2016
You can also use findsignal if you have a recent copy of the Signal Processing Toolbox, which has some additional normalization and scaling options. (See also example using findsignal)
Even so, I think you'll want to massage your data a little bit to get a good result. The first column of both your observed and denoised move fairly linearly from 0 to 1; the second column looks like it is centered at 0.03 in your observed data, and centered at the origin in your denoised.
Was this an attempt at normalization?

Categories

Find more on Interpolation in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!