Skip to content

Instantly share code, notes, and snippets.

@Umair444
Created January 16, 2022 08:18
Show Gist options
  • Save Umair444/55d73a67001965bcc2a21ecbda561cde to your computer and use it in GitHub Desktop.
Save Umair444/55d73a67001965bcc2a21ecbda561cde to your computer and use it in GitHub Desktop.
Spatial Joining MATLAB - Shift Attributes using spatial data
% FORMAT:
% DATA1 = [ID1 Lat Lon]
% DATA2 = [ID2 data Lat Lon]
%% Remove nan Before
DATA1(isnan(DATA1.Lat), :) = [];
DATA2(isnan(DATA2.Lat), :) = [];
%% Calculate Dist
dist = zeros(length(DATA2.ID2), length(DATA1.ID1));
for i = 1:length(DATA1.Lat)
for j = 1:length(DATA2.Lat)
dist(j, i) = distance(DATA1.Lat(i), DATA1.Lon(i), DATA2.Lat(j), DATA2.Lon(j)); % Need mapping toolbox
end
end
% dist = dist';
%% Remove NAN Rows Column
% dist = dist(any(~isnan(dist),2),:);
% dist = dist(:,any(~isnan(dist)));
%% Calculate Min
m = zeros(1,size(dist,2));
idx = zeros(1,size(dist,2));
for k = 1:size(dist,2)
[m(k), idx(k)] = min(dist((dist(:, k) > 0), k), [], 'omitnan');
end
%% Display
result = [DATA2.ID2(idx) DATA1.ID1 DATA2.data(idx) m'];
histfit(m);
disp(mean(m));
%% Histo
figure,
[a,b] = groupcounts(result(:,2)+"/"+result(:,3));
bar(a);
xticks(1:68);
xticklabels(b);
@Umair444
Copy link
Author

  1. Join two datasets - and shift attributes based on spatial charaterstics of data.
  2. Visualize the distance distribution (normal)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment