euclidean distance calculation for values from excel sheet
Show older comments
sir, I have values in an excel sheet, which contains 60x3 values, they are x,y,z cordinates for all the 60 points.Now I need to find out the distance : |d(i)|=sqrt((x(k)-x(j))^2+(y(k)-y(j))^2+(z(k)-z(j)^2)), where i=1:60 , j,k are end points of the line segment under consideration, i.e., between these 60 points line segments are considered, for which this distance we are calculating. So j,k can be j=1:59 and k=2:60..
Can you please help me with the code...
22 Comments
Jan
on 26 Mar 2015
Please ask more precise: Why do you want to do this in Matlab and not in Excel directly? Are you able to import the file already or is this a part of the problem. Did you try to solve this by simple loops already? What did you try at all? Which problems occurred?
krishnasri
on 27 Mar 2015
dpb
on 27 Mar 2015
Use pdist and it does the assumption for you automagically. If you don't have the Stat Toolbox still just use x(:,1), x(:,2), x(:,3) to refer to the three columns.
See the "Getting Started" section in the documentation and work thru the examples given on how Matlab works with arrays and the colon (:) operator...
krishnasri
on 28 Mar 2015
krishnasri
on 28 Mar 2015
krishnasri
on 30 Mar 2015
Image Analyst
on 30 Mar 2015
What does that mean? What does it mean "to angle" a line segment with 2 endpoints in 3D space? Do you want the Euler angles? To you want to change the orientation (Euler angles)? Why are there two line segments? Do you somehow want some kind of angles that describe the location and orientation differences between the two? If so, why? What's the use case?
dpb
on 30 Mar 2015
pdist returns a 1D vector per the doc. See
doc squareform
for converting that into a symmetric square array.
I don't understand the second query, sorry...
krishnasri
on 31 Mar 2015
krishnasri
on 31 Mar 2015
dpb
on 31 Mar 2015
"when i used pdist and when i calculated manually, the values were not matching..."
>> xyz=rand(3); % some random data sample coordinates
>> squareform(pdist(xyz))
ans =
0 0.2374 0.2435
0.2374 0 0.2397
0.2435 0.2397 0
>> del=diff(xyz); % compute differences vertically 2-1, 3-2
>> sqrt(dot(del,del,2)) % euclidean distance between those two
ans =
0.2374
0.2397
>>
NB: This is same as 1-2 and 2-3 from pdist; you can confirm that 1-3 is same as well.
Oh, and on your question re: the angles; I see nothing better than writing an anonymous function and using nchoosek(1:length(x),2) to generate the list of pairwise indices of the two vectors pairwise as inputs to evaluate the expression. You can write
theta=acos(sqrt(dot(a,b)/{|a||b|}))
in terms of the two indices for vectors a and b to compute the expression.
krishnasri
on 16 Apr 2015
dpb
on 16 Apr 2015
Just use the indices of the arrays...if the data are in X, then X(i,:) is point i, so X(i,1), X(i,2), X(i,3) --> [xi,yx,zi], respectively.
krishnasri
on 16 Apr 2015
dpb
on 17 Apr 2015
Depends on what you want as compare whom to whom...basically, read the two sheets into different arrays and then confound them together in the desired arrangement that satisfies pdist for the desired comparison. What isn't clear is whether you've got a position-by-position between the two or the "all possible comparisons" case.
krishnasri
on 17 Apr 2015
dpb
on 17 Apr 2015
That's covered by
doc pdist2
krishnasri
on 18 Apr 2015
dpb
on 18 Apr 2015
Looks like you've got a script pdist2 that's aliasing the TMW-supplied one. What does
which pdist2
show?
krishnasri
on 19 Apr 2015
Type it in at the command line...see
doc which
It'll show you which is the referenced pdist2 actually being called and in your above error the string "D:\pdist2.m" certainly makes it appear as if the problem is there is a script called PDIST2 which, if really so, is aliasing the TMW-supplied PDIST2 that you're trying to use.
The likely thing is that you named your top-level script pdist2 without realizing there is a builtin function of that name; if so rename that script to something else. Since Matlab is case-sensitive, use "PDIST2.m" or "pDist2.m" or some other name entirely, but anything but "pdist2.m".
PS:
"anything" above of course means "anything except pdis2.m or any other builtin function*.
Accepted Answer
More Answers (0)
Categories
Find more on k-Means and k-Medoids Clustering in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!