Find closest values from two different matrices
Show older comments
Hello there,
I deal with spatial datasets. I have two mat files, each of them contain a variable which vary in space. Let's go in to the detail. The first mat file, namely data_rough.mat; it contains parameter rough which is distributed in Lon and Lat space. The second mat file, namely data_dat.mat, it contains parameter ep which is distributed in x and y.
My intention is, I want to get the values of rough where the location (Lon, Lat) is close to the locations of values ep (x, y). So, the output I want is x, y, ep, and rough and if possible, the Lon and Lat of the closest locations to the location of of ep).

PS.
The provided datasets (data_dat.mat) I attached might be close each other and may result the same values of rough (and their positions). It's fine.
4 Comments
Adi Purwandana
on 12 Nov 2024
Edited: Adi Purwandana
on 12 Nov 2024
Stephen23
on 12 Nov 2024
"No. It's nothing to do with interpolation at all. It's more like finding indices close to certain values (here, the closest the locations)."
The NEAREST option is supported by INTERP1, INTERP2, INTERP3, etc.
Accepted Answer
More Answers (2)
Mr. Pavl M.
on 12 Nov 2024
Edited: Mr. Pavl M.
on 12 Nov 2024
home
clear all
close all
load('rough_dat.mat')
load('data_dat.mat')
x
y
ep
Lat
Lon
rough
thresh = eps*1000; %Any threshold as per custom objections
resclosestloc = [];
rescloseststr = [];
for i = 1:length(Lon)
for j = 1:length(Lat)
%Approach 1:
a2 = find(y==Lat(j))
a1 = find(x==Lon(i))
c = find(a1==a2);
if c~=0
str2.x = x(a1)
str2.y = y(a2)
str2.ep = ep(1) %a2 will also do, since x and y are in same length
str2.rough = rough(j,i)
rescloseststr = [rescloseststr,str2];
end
%Approach 2:
for k =1:length(y)
for l = 1:length(x)
if (sign(Lat(j)) == sign(y(k)))&&(abs(abs(Lat(j))-abs(y(k)))<thresh)&&(sign(Lon(i))==sign(x(l)))&&(abs(abs(Lon(i))-abs(x(l)))<thresh)
%found closest location
resclosestloc = [resclosestloc; x(l) y(k) ep(l) rough(j,i)];
end
end
end
end
end
%Constructed from needing help code by
%https://independent.academia.edu/PMazniker
%+380990535261
%https://diag.net/u/u6r3ondjie0w0l8138bafm095b
%https://github.com/goodengineer
%https://orcid.org/0000-0001-8184-8166
%https://willwork781147312.wordpress.com/portfolio/cp/
%https://www.youtube.com/channel/UCC__7jMOAHak0MVkUFtmO-w
%https://nanohub.org/members/130066
%https://pangian.com/user/hiretoserve/
%https://substack.com/profile/191772642-paul-m
5 Comments
Adi Purwandana
on 12 Nov 2024
Mr. Pavl M.
on 12 Nov 2024
Edited: Mr. Pavl M.
on 12 Nov 2024
To continue to do it marketable, wel l- commercializable , If to add constraint of additional coordinates to complete utile for Point Clouds processing for example( 0 plagiarism, completely novel approach code with structured arrays). It must needs to continue, to move forward.
Which features pooling? The my original code works completed fine (see also I added 2 loops instead of 4 loops before and structured array):
home
clear all
close all
load('rough_dat.mat')
load('data_dat.mat')
x
y
ep
Lat
Lon
rough
thresh = eps*10;
resclosestloc = [];
rescloseststr = [];
for i = 1:length(Lon)
for j = 1:length(Lat)
%Approach 1:
a1 = find(y==Lat(j))
a2 = find(x==Lon(i))
c = find(a1==a2);
if(c!=0)
str2.x = x(c)
str2.y = y(c)
str2.ep = ep(c)
str2.rough = rough(j,i)
rescloseststr = [rescloseststr,str2];
end
%Approach 2:
for k =1:length(y)
for l = 1:length(x)
if (sign(Lat(j)) == sign(y(k)))&&(abs(abs(Lat(j))-abs(y(k)))<thresh)&&(sign(Lon(i))==sign(x(l)))&&(abs(abs(Lon(i))-abs(x(l)))<thresh)
%found closest location
resclosestloc = [resclosestloc; x(l) y(k) ep(l) rough(j,i)]
end
end
end
end
end
%Constructed from needing help code by
%https://independent.academia.edu/PMazniker
%+380990535261
%https://diag.net/u/u6r3ondjie0w0l8138bafm095b
%https://github.com/goodengineer
%https://orcid.org/0000-0001-8184-8166
%https://willwork781147312.wordpress.com/portfolio/cp/
%https://www.youtube.com/channel/UCC__7jMOAHak0MVkUFtmO-w
%https://nanohub.org/members/130066
%https://pangian.com/user/hiretoserve/
%https://substack.com/profile/191772642-paul-m
Mr. Pavl M.
on 12 Nov 2024
Proof of Work:
My next code finds it (solves):
##
home
clear all
close all
load('rough_dat.mat')
load('data_dat.mat')
x
y
ep
Lat(1) = y(1) %for testing purposes:
Lon(1) = x(1) %for testing purposes:
rough
thresh = eps*10;
resclosestloc = [];
rescloseststr = [];
for i = 1:length(Lon)
for j = 1:length(Lat)
%Approach 1:
a1 = find(y==Lat(j))
a2 = find(x==Lon(i))
c = find(a1==a2);
if(c!=0)
str2.x = x(c)
str2.y = y(c)
str2.ep = ep(c)
str2.rough = rough(j,i)
rescloseststr = [rescloseststr,str2];
end
%Approach 2:
for k =1:length(y)
for l = 1:length(x)
if (sign(Lat(j)) == sign(y(k)))&&(abs(abs(Lat(j))-abs(y(k)))<thresh)&&(sign(Lon(i))==sign(x(l)))&&(abs(abs(Lon(i))-abs(x(l)))<thresh)
%found closest location
resclosestloc = [resclosestloc; x(l) y(k) ep(l) rough(j,i)]
end
end
end
end
end
%Constructed from needing help code by
%https://independent.academia.edu/PMazniker
%+380990535261
%https://diag.net/u/u6r3ondjie0w0l8138bafm095b
%https://github.com/goodengineer
%https://orcid.org/0000-0001-8184-8166
%https://willwork781147312.wordpress.com/portfolio/cp/
%https://www.youtube.com/channel/UCC__7jMOAHak0MVkUFtmO-w
%https://nanohub.org/members/130066
%https://pangian.com/user/hiretoserve/
%https://substack.com/profile/191772642-paul-m

(Validation runs accomplished on Desktop).
Completion is a function of input (initial, boundary conditions).
Adi Purwandana
on 12 Nov 2024
Mr. Pavl M.
on 12 Nov 2024
Edited: Mr. Pavl M.
on 12 Nov 2024
corrected, so far:
Finds only corresponding equal entries on same places
#in
#Lat - y
#Lon - x
I am preparing more precise with each even on same places finding with intersect(...) function.
see Proof of Work, it really traverses and extracts the cells in multidimensional arrays as per custom objections.
It depends on TCE NCE MPPL version. The logic is correct, which check that c array of indices is not 0 you can substitute?
I can make more on it, for example to reduce the number of loops from 2 to 1, to fill resultant points to cell, table or multidim array, to add custom criterias for the 2 data lakes processing (for instance norms or sophisticated distances (see rationale: sophisticated doesn't mean complicated, while simple as not too simple means compex). I have more novel codes as for novel distances (in addition to existing Euclidean, Fro, Inf distances I have running codes in Python and Octave for Mahalanobis and more scientific metrics, tell me should there be a customer willing to buy I may deliver as per order/request sufficiently well here or via Skype/e-mail).
In Latest versions of TCE Matlab they replaced C style != to ~=.
So use ~= in TCE Matlab instead of common to C/C++ != .
In Octave beauty it works also for if(c!=0), the 2nd approach works on both, right?
As let us continue benign commited to utile high quality products and services and complete customers' satisfaction.
You may need interpolation / extrapolation for this kind of questions as well, to handle more valuably space-time multidimensional data by regressing missing values.
Please let me know which functionality to gently append.
What more to add to to the clean solution, which more features who will be requiring?
Can you accept my the answer?
Mr. Pavl M.
on 12 Nov 2024
Edited: Mr. Pavl M.
on 12 Nov 2024
%Version with zero loops.
%Runs both on TCE MPPL Octave and Matlab. Finds not only corresponding equal entries on same places
%in
%Lat - y
%Lon - x
%But also contains improvement, runs for intersection of values
home
clear all
close all
load('rough_dat.mat')
load('data_dat.mat')
x
y
ep
%Lat(3) = y(3) %For testing purposes
%Lon(3) = x(3)
%Lat(4) = y(4)
%Lon(4) = x(4)
rough
Lon
Lat
thresh = eps*1000; %Any threshold as per custom objections
resclosestloc = [];
str2 = [];
xl = x;
yl = y;
l1 = length(Lon);
l2 = length(x);
l3 = length(Lat);
l4 = length(y);
%l5 = length(Az);
%l6 = length(z);
%dimensions fitting:
Lon = Lon';
col = true;
%for x,y column vectors
if col
if l1 > l2
xl = [x; ones(l1-l2,1)*NaN]
elseif l1 < l2
xl = [Lon; ones(l2-l1,1)*NaN]
end
if l3 > l4
yl = [y; ones(l3-l4,1)*NaN]
elseif l3 < l4
yl = [Lat; ones(l4-l3,1)*NaN]
end
else
%ToDo: doable for row vectors as well:
if l1 > l2
xl = [x ones(1,l1-l2)*NaN]
elseif l1 < l2
xl = [Lon ones(1,l2-l1)*NaN]
end
if l3 > l4
yl = [y ones(1,l3-l4)*NaN]
elseif l3 < l4
yl = [Lat ones(1,l4-l3)*NaN]
end
end
if l3 > l4
if l1 > l2
%Approach 1:
a1 = find(yl==Lat);
a2 = find(xl==Lon);
c = find(a1==a2);
if c~=0
[a1,b1,c1] = intersect(yl,Lat);
[a2,b2,c2] = intersect(xl,Lon);
str2.x = x(c2);
str2.y = y(c1);
str2.ep = ep(c2);
str2.rough = diag(rough(c2,c1));
end
else
%Approach 1:
a1 = find(yl==Lat);
a2 = find(xl==x);
c = find(a1==a2);
if c~=0
[a1,b1,c1] = intersect(yl,Lat);
[a2,b2,c2] = intersect(xl,x);
str2.x = x(c2);
str2.y = y(c1);
str2.ep = ep(c2);
str2.rough = diag(rough(c2,c1));
end
endif
else
if l1 > l2
%Approach 1:
a1 = find(yl==y);
a2 = find(xl==Lon);
c = find(a1==a2);
if c~=0
[a1,b1,c1] = intersect(yl,y);
[a2,b2,c2] = intersect(xl,Lon);
str2.x = x(c2);
str2.y = y(c1);
str2.ep = ep(c2);
str2.rough = diag(rough(c2,c1));
end
else
%Approach 1:
a1 = find(yl==y);
a2 = find(xl==x);
c = find(a1==a2);
if c~=0
[a1,b1,c1] = intersect(yl,y);
[a2,b2,c2] = intersect(xl,x);
str2.x = x(c2);
str2.y = y(c1);
str2.ep = ep(c2);
str2.rough = diag(rough(c2,c1));
end
endif
endif
%res = struct2cell(str2)
%ToDo: doable for additional z and azimuth dimensions, just twice the if checks and additional pre-filling
%Contact/order me more to do and I will do it.
%Constructed from needing help code by
%https://independent.academia.edu/PMazniker
%+380990535261
%https://diag.net/u/u6r3ondjie0w0l8138bafm095b
%https://github.com/goodengineer
%https://orcid.org/0000-0001-8184-8166
%https://willwork781147312.wordpress.com/portfolio/cp/
%https://www.youtube.com/channel/UCC__7jMOAHak0MVkUFtmO-w
%https://nanohub.org/members/130066
%https://pangian.com/user/hiretoserve/
%https://substack.com/profile/191772642-paul-m
Proof of Commitment (Work) that it runs in TCE:

Categories
Find more on Geographic Plots 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!