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

4 次查看(过去 30 天)
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 个评论
Praveen GB
Praveen GB 2019-12-4
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.

请先登录,再进行评论。

采纳的回答

JESUS DAVID ARIZA ROYETH
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

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Graph and Network Algorithms 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by