Find the perfect overlay of 2 maps of points
12 views (last 30 days)
Show older comments
I would like to have a perfect overlay of these 2 maps of points. Most of the points have their pair in the other map, some don't.
I already tried to minimize the distance between the points but it doesn't work as the min distance is not the one that overlay perfectly the points.
Do you have a method to do that ?
More Answers (2)
Thorsten on 8 Nov 2019
Edited: Thorsten on 8 Nov 2019
You can maximize the number of perfect matches, that is the number of red points with zero distance to a blue point.
Edited: Adam Danz on 30 Dec 2020
You can compute the difference between the x values from each set and the y values from each set which will create a matrix of offsets for the x values and the y values. If (and only if) coordinates (x1,y1) are a linear offset from coordinates (x0,y0) plus some extra non-paired coordinates from both sets, the most frequent offset value for x and y will be the amount you need to shift the dataset to match. This can be computed using mode().
% Create a fake dataset (x0,y0)
x0 = randi(100, 1, 50);
y0 = randi(100, 1, 50);
% Create a fake dataset (x1,y1)
x1 = randi(100, 1, 60);
y1 = randi(100, 1, 60);
% Match several of the values between the two sets and offset them a bit.
randSelect = unique(randi(50,1,40));
x1(randSelect) = x0(randSelect) + 0.5; % Offset some of the x values
y1(randSelect) = y0(randSelect) - 3.5; % Offset some of the y values
% Show inputs
plot(x0,y0,'bo', 'DisplayName', 'xy0')
plot(x1,y1,'ro', 'DisplayName', 'xy1')
% SOLUTION :
% Get most common x-offset and y-offset
% This approach uses implicit expansion requires matlab r2016b or later.
% It also uses the 'all' flag in mode() which requires r2018b or later.
xDiff = mode(x1(:) - x0(:).','all'); % implicit expansion requires matlab r2016b or later
yDiff = mode(y1(:) - y0(:).','all'); % implicit expansion requires matlab r2016b or later
% Shift (x1,y1) to overlay (x0,y0)
x1shift = x1 - xDiff;
y1shift = y1 - yDiff;
(x1shift, y1shift) are the new coordinate values for (x1,y1) that overlay (x0,y0) when paired.
If x1shift & y1shift does not result in overlap between the two sets, that indiciates that the difference between the two sets may not be linear or that you do not have enough pairs of coordinates that should overlap between the two datasets.
Update plot; recall that some xy1 values do not have a paired xy0 value in this demo.
plot(x1shift, y1shift, 'rx', 'DisplayName', 'xy1 shifted')
Adam Danz on 11 Nov 2019
I didn't realize you had an earlier version. The two lines that require later versions can easily be changed to work with r2007b.
Mind sharing your solution?
Find more on Resizing and Reshaping Matrices 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!