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일
distance is calculated as
for i=1:size(P,1)
distance(i)=sqrt((P(i,1)-R(i,1)).^2 + (P(i,2)-R(i,2)).^2)';
end
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
JESUS DAVID ARIZA ROYETH 2019년 12월 4일
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