MATLAB Answers

Distance difference from center

7 views (last 30 days)
ML2.jpg
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)];

  2 Comments

darova
darova on 28 May 2019
Do you have a question?
Mehmet Volkan Ozdogan
Mehmet Volkan Ozdogan on 28 May 2019
I could not make the calculation..
I need to find the distance between d2-d1
X in figure are represented by variable m in code,
* in figure are represented by variable n in code,
and the center of cross section is represented by variable o in code
thank you

Sign in to comment.

Accepted Answer

darova
darova on 28 May 2019
I did this
xyz0 = (mean(xyz)+mean(abc))/2; % O point
XYZ0 = repmat(xyz0,size(xyz,1),1); % duplicate rows
d1 = XYZ0 - xyz; % Distance(s) between O and X (d1)
% find the nearest * to X
D = pdist2(xyz,abc); % every possible combinations
D(D==0) = max(D(:)); % fill zeros with max ( (:) - convert matrix to column vector )
[~,ind] = min(D(:)); % find index of min element
% Found index of min element in vector. Find correspoding indices of points
[i,j] = ind2sub(size(D),ind); % extract row and column (i - index of xyz, j - index of abc)
% calculate the distance between O and * (which is nearest X) (d2)
d2 = xyz0 - abc(j,:); % difference between O point and * (nearest X)
D2 = repmat(d2,size(d2,1),1); % duplicate rows
d = D2 - d1; % distance(s) between d2 and d1

  5 Comments

Show 2 older comments
darova
darova on 29 May 2019
Sorry, didn't test it (missed ':' for column). Try:
d2 = pdist2(newVar5,newVar2(idx,:));
Mehmet Volkan Ozdogan
Mehmet Volkan Ozdogan on 29 May 2019
Thank you for your helps,
I made a little uptade on your code
d2 = pdist2(varO,varS(idx)); have to be ;
d2 = pdist2(varO,varS(idx, :))
thank you again
Mehmet Volkan Ozdogan
Mehmet Volkan Ozdogan on 29 May 2019
works great thank you again...

Sign in to comment.

More Answers (1)

e_oksum
e_oksum on 29 May 2019
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']

  1 Comment

Mehmet Volkan Ozdogan
Mehmet Volkan Ozdogan on 29 May 2019
Thank you for your helps and advice, i solve my problem with previous answer.

Sign in to comment.

Tags