Calculating distance between points from imported data table.

Hello everyone, I have two tables, one with X coordinates of a point, and the other with the respective Y coordinates. They are all sorted by time, meaning that the first value of the column happened before the one after it (even though I do not have the time showing). I want to calculate the distance between those points. However, I am not getting the right results. Can someone help me out?
My code is as follows:
X = xlsread('Results','A1:A191'); Y = xlsread('Results','B1:B191'); i = 2:length(X); j = 2:length(Y); if i == j Distance = ((X(i)-X(i-1)).^2 + (Y(j)-Y(j-1)).^2).^(1/2); end

답변 (1개)

Image Analyst
Image Analyst 2018년 9월 5일
If you have the stats toolbox, you can easily and simply use pdist2() to find the distance of every point to every point.
distances = pdist2([x,y], [x,y]);
After that you can get whatever distance you want from the distances array.
Does that help?

댓글 수: 2

Ricardo Whitaker
Ricardo Whitaker 2018년 9월 5일
편집: Ricardo Whitaker 2018년 9월 5일
I believe the function pdist2() it is the function I should be using, but when I runt he code now the Distance output is a matrix of 190 x 190, and that is because it is reading the distance between every single point in the list. How can I specify that I want the distance between one point to its following points?
New Code is below: X = xlsread('Results','A1:A191'); Y = xlsread('Results','B1:B191'); i = 2:length(X); j = 2:length(Y); if i == j Distance = pdist2([X(i),Y(j)],[X(i-1),Y(j-1)]); end
Maybe you simply want the diff() function to compute differences between adjacent elements???

댓글을 달려면 로그인하십시오.

질문:

2018년 9월 5일

댓글:

2018년 9월 5일

Community Treasure Hunt

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

Start Hunting!

Translated by