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 件のコメント

darova
darova 2019 年 5 月 28 日
Do you have a question?
Mehmet Volkan Ozdogan
Mehmet Volkan Ozdogan 2019 年 5 月 28 日
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

サインインしてコメントする。

 採用された回答

darova
darova 2019 年 5 月 28 日

0 投票

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 件のコメント

Mehmet Volkan Ozdogan
Mehmet Volkan Ozdogan 2019 年 5 月 28 日
編集済み: Mehmet Volkan Ozdogan 2019 年 5 月 28 日
Thank you for your answer and clear explanation
But I think i could not tell my self clearly,sorry about that,
In my current code i did not use xyz and abc, i used them to find the "newVar" and "newVar2"
"newVar" = X and "newVar2" = *, and also "newVar5"= o
Also i need the distance but "d1 = XYZ0 - xyz" gives coordinate.
I changed the variables and also changed the distance calculation but i have still a problem with the code . Because results are not sensible.
On the other hand i do not understand why you duplicated the rows
Do you have any other suggestion, thank you...
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)];
newVar52 = repmat(newVar5,size(newVar,1),1); % duplicate rows
[d1,idx5] = pdist2(newVar52, newVar, 'euclidean', 'Smallest',1); % Distance(s) between O and X (d1)
% find the nearest * to X
D = pdist2(newVar,newVar2); % 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,idx6] = pdist2(newVar5, newVar2(j,:), 'euclidean', 'Smallest',1); % difference between O point and * (nearest X)
D2 = repmat(d2,size(d2,1),1); % duplicate rows
d = D2 - d1;
darova
darova 2019 年 5 月 29 日
Sorry. forgot about that. Those are not coordinates, when you subract coordinates you get coordinates difference
p1(x1,y1) - p2(x2,y2) = v(dx,dy)
xyz0 = (mean(xyz)+mean(abc))/2; % O point (ONE point)
XYZ0 = repmat(xyz0,size(xyz,1),1); % duplicate rows
d1 = XYZ0 - xyz; % difference(s) between O and X (dy,dy,dz)
d1 = sqrt(sum(d1.^2,2)); % absolute distance(s) (euclidean)
% the same because xyz0 - one point
d1 = pdist(xyz0,xyz);
% or
d1 = pdist2(xyz0,xyz,'euclidean','smallest',1)
I duplicated rows, because forgot how pdist2() works
  • Distance between O and X (d1)
  • Than i need to find the nearest * to X, - i misunderstood you here
  • Than calculate the distance between O and * (which is nearest X) (d2)
  • And lastly i need to calculate the difference between (d1) and (d2)
Try this:
d1 = pdist2(varO,varX); % distance(s) (many) between O and X
% varS == var*
[~,idx] = pdist2(varS,varX,'euclidean','smallest',1); % indices of nearest * for EACH X
d2 = pdist2(varO,varS(idx)); % distance(s) between O and * (nearest to X)
d2 - d1
darova
darova 2019 年 5 月 29 日
Sorry, didn't test it (missed ':' for column). Try:
d2 = pdist2(newVar5,newVar2(idx,:));
Mehmet Volkan Ozdogan
Mehmet Volkan Ozdogan 2019 年 5 月 29 日
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 2019 年 5 月 29 日
編集済み: Mehmet Volkan Ozdogan 2019 年 5 月 29 日
works great thank you again...

サインインしてコメントする。

その他の回答 (1 件)

e_oksum
e_oksum 2019 年 5 月 29 日

1 投票

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 件のコメント

Mehmet Volkan Ozdogan
Mehmet Volkan Ozdogan 2019 年 5 月 29 日
Thank you for your helps and advice, i solve my problem with previous answer.

サインインしてコメントする。

カテゴリ

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by