Find the unique pair of coordinates in matrix and depending on the distance matrix, retain the minimum distance matrix and replace the other coordinate pair by NaN

2 views (last 30 days)
I have two input matrices ( P and R) and a Distance matrix (distance)
P=[1315.2,832.2
1317.2,808.8
1510.8,514.4
1354.7,743
1519.1,873
1382.7,736.6]
R=[1318,816
1318,816
1515,515
1346,740
1515,867
1346,740]
distance= [16.43
7.24
4.2
9.19
7.2
36]
I want to find out unique values of R and from the distance matrices, which ever distance is lesser, retain the R value as per minimum distance between two pairs of coordinates and remaining replace it with NaN
The output should look like this:
P_new=[1315.2,832.2
1317.2,808.8
1510.8,514.4
1354.7,743
1519.1,873
1382.7,736.6]
R_new = [NaN,NaN
1318,816
1515,515
1346,740
1515,867
NaN,NaN]
distance_new = [NaN
7.24
4.2
9.19
7.2
NaN]
% P, R and distance are input matrices
P=[1315.2,832.2;1317.2,808.8;1510.8,514.4;1354.7,743;1519.1,873;1382.7,736.6]
R=[1318,816;1318,816;1515,515;1346,740;1515,867;1346,740]
distance= [16.43;7.24;4.2;9.19;7.2;36]
% P_new, R_new and distance_new are the required output matrices
P_new=[1315.2,832.2;1317.2,808.8;1510.8,514.4;1354.7,743;1519.1,873;1382.7,736.6]
R_new = [NaN,NaN;1318,816;1515,515;1346,740;1515,867;NaN,NaN]
distance_new = [NaN;7.24;4.2;9.19;7.2;NaN]
  4 Comments
Praveen GB
Praveen GB on 4 Dec 2019
for example: 1st and 2nd rows of P have common value of R (1318,816), but distance matrix (16.43 and 7.24).
I want to retain value of R based on minimum distance. Since 7.24 is smallest between 16.43 and 7.24. I want to retain the 2nd row of R as 1318,816 and the other value (1st row of R) i just want to edit as NaN.

Sign in to comment.

Accepted Answer

JESUS DAVID ARIZA ROYETH
JESUS DAVID ARIZA ROYETH on 4 Dec 2019
a solution:
[uv,~,idx] = unique(R,'rows');
v = accumarray(idx,distance,[],@min);
eliminate=~ismember([R distance],[uv v],'rows');
P_new=P
R_new=R;
R_new(eliminate,:)=nan
distance_new=distance;
distance_new(eliminate)=nan

More Answers (0)

Community Treasure Hunt

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

Start Hunting!