Distance difference from center
显示 更早的评论

Hi,
I created a cross section by using kmeans function from two different data (indicated by X and * in image),
My aim is to determine the distance difference of two data from center (o) of cross section,
Briefly i try to find the;
Distance between O and X (d1)
Than i need to find the nearest * to X,
Than calculate the distance between O and * (which is nearest X) (d2)
And lastly i need to calculate the difference between (d1) and (d2)
And i want to do this calculations for all X to * in cross-section.
Thank you...
My current code is given below: my points are represented by m,n and o in code...
clc;clear;
x=xlsread('king1.xlsx', 'A:A');
y=xlsread('king1.xlsx', 'B:B');
z=xlsread('king1.xlsx', 'C:C');
a=xlsread('king2.xlsx', 'A:A');
b=xlsread('king2.xlsx', 'B:B');
c=xlsread('king2.xlsx', 'C:C');
xyz=[x y z];
abc=[a b c];
rng(1);
[idx1,C1] = kmeans(xyz,100,'distance','sqEuclidean','MaxIter',500, 'Replicates', 10);
[idx2,C2] = kmeans(abc,100,'distance','sqEuclidean','MaxIter',500, 'Replicates', 10);
[dist,idx3] = pdist2(xyz, C1, 'euclidean', 'Smallest',1);
newVar = xyz(idx3 ,:);
plot3(newVar(:,1), newVar(:,2), newVar(:,3), 'bx');
hold on;
xlabel ('x - axis', 'fontsize', 12);
ylabel ('y - axis', 'fontsize', 12);
zlabel ('z - axis', 'fontsize', 12);
grid
[dist2,idx4] = pdist2(abc, C2, 'euclidean', 'Smallest',1);
newVar2 = abc(idx4 ,:);
plot3(newVar2(:,1), newVar2(:,2), newVar2(:,3), 'r*')
newVar3 = mean (newVar)
newVar4 = mean (newVar2)
newVar5 = (newVar3 + newVar4)/ 2
plot3(newVar5(:,1), newVar5(:,2), newVar5(:,3), 'go');
m=[newVar(:,1) newVar(:,2) newVar(:,3)];
n=[newVar2(:,1) newVar2(:,2) newVar2(:,3)];
o=[newVar5(:,1) newVar5(:,2) newVar5(:,3)];
采纳的回答
更多回答(1 个)
e_oksum
2019-5-29
hi mehmet, here is an example code performing what you explained,
example uses random positions, you can adopt by yours..and also simplify it for more compact without plotting etc..
X=rand(1,10)*10 ;% your x position of X
Y=rand(1,10)*10 ;% your y position of X
xs=rand(1,10)*10 ;% your x position of *
ys=rand(1,10)*10 ;% your y position of *
xo=5 ;% center x
yo=5 ;% center y
plot(X,Y,'ro','markerfacecolor','r');
hold on
plot(xs,ys,'k+');
plot(xo,yo,'go','markerfacecolor','g');
for i=1:numel(X)
d1(i)=sqrt((X(i)-xo).^2+(Y(i)-yo).^2);% distance d1 of X(i) Y(i) to center
%find position of nearest xs,ys to X,Y
L=sqrt((xs-X(i)).^2 + (ys-Y(i)).^2);
idx=find(L==min(L));
xp(i)=xs(idx); %(xp yp are the nearest nearest X)
yp(i)=ys(idx);
d2(i)=sqrt((xp(i)-xo).^2 + (yp(i)-yo).^2); % distance d2 of nearest xp yp to X(i),Y(i)
diffd1d2(i)=(d1(i)-d2(i)); % diffrence between d1 d2
% check by plot
l1=plot([X(i) xo],[Y(i) yo],'-r'); % line d1
l2=plot([xp(i) xo],[yp(i) yo],'-k'); % line d2
pause(1)
delete(l1)
delete(l2)
end
list=[X' Y' xp' yp' d1' d2' diffd1d2']
类别
在 帮助中心 和 File Exchange 中查找有关 Logical 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!