0

I have two matrices of Longitude values. I would like to identify points that are close together within the two lists. My problem is the lists are of different lengths

One of them is named Lon_pair and the other is named Lon_epa.

Lon_pair is of size

size(Lon_pair) 
12635       1

Lon_epa is of size

size(Lon_epa)
20560       1

How do I find points within Lon_pair that are close to those within Lon_epa? Or find the difference between them?

Edit: Adding Code

clear all
close all

filename_pair = 'C:\Users\tadams15\Desktop\ALL_PAIR.csv';
data = xlsread(filename_pair);

Lon_pair = data(:,2);
Lat_pair = data(:,3);
Label_pair = data(:,4);

filename_epa = 'C:\Users\tadams15\Desktop\aqs.csv'
data2 = xlsread(filename_epa);
Lon_epa = data2(:,4);
Lat_epa = data2(:,5);

%% Find Lat/Lon Combos
for j = 1:20560
for i = 2:12636
        d_lon(j) = Lon_epa(:) - Lon_pair(i);
        d_lon_m(j) = d_lon/(9e-06);
        if d_lon(:) < 300
           [row,col] = find(d_lon)
        end
end
end
3
  • But what's a difference about the length? You anyway have to iterate through all two lists. You don't compare like List1(1) to List2(1), List1(2) to List2(2).
    – Karls
    Mar 21, 2019 at 13:41
  • I believe this problem was defined incorrectly or did not provide sufficient detail for the characteristics of lists. You may use a for loop and cite the longer one. You may also want to provide more detail for what you meant by "close together".
    – Y. Chang
    Mar 21, 2019 at 13:46
  • @Y.Chang Hello! Sorry, I'm fairly new to this. I have a two list of longitude points. The lists are the lengths specified. I'm converting their values to meters and then I would like to find the index of each longitude that is less than 500 meters away from another longitude value.
    – Taylor
    Mar 21, 2019 at 13:50

1 Answer 1

1

This isn't the most efficient for very large datasets, but it may work for you (or least be a starting point). Below double-loops through both datasets, finds distances between points, keeps the minimum and row number of companion dataset corresponding to minimum distance.

% set up dummy data to mimic UTM x,y (northing,easting) coordinates
x1 = randi([0,50000],20,1);
y1 = randi([-230000,420000],20,1);

x2 = randi([0,50000],12,1);
y2 = randi([-230000,420000],12,1);
% end set up


mydist = NaN(length(x1), 2); % pre-allocate

for ii = 1:length(mydist)
    xa = x1(ii);
    ya = y1(ii);
    temp_dist = NaN(length(x2), 2);
    for jj = 1:length(x2)
        xb = x2(jj);
        yb = y2(jj);
        temp_dist(jj, 1) = sqrt((xa-xb)^2 + (ya-yb)^2); % if you have the Statistics Toolbox, you can just use pdist
        temp_dist(jj, 2) = jj; % this is the row number in set2
    [value, index] = min(temp_dist(:,1));
    mydist(ii, 1) = value;
    mydist(ii, 2) = temp_dist(index, 2);
    end
end

mydist_table = array2table(mydist);
mydist_table.Properties.VariableNames = {'Clostest_Distance', 'Set2_RowNumber'};

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.