how do i find the distance between points

if i have x=[1,5,11,26,46,56,66,76,81];
and I want to find the distance between all these points. eg
1,5 = 4
5,11= 6
46,56=10
and if distances are the same add it to a counter
eg
46,56,66
distance = 10
counter = 3

2 commentaires

Rik
Rik le 23 Avr 2018

What have you tried so far? It sounds like you can use diff to calculate the distance, and you might use it again to check for the number of repetitions.

Also, what exactly should your output look like?

so i've tried using diff(x)

x=[1,5,11,26,46,56,66,76,81];
xdist=diff(x)
xdist =
       4     6    15    20    10    10    10     5

but I'm not sure how to add the interverals of distance 10 together

so i want an output something like

counter = 3 % 3 intervals of distance 10
if counter ==3;
function 1 % run fuction 1 as counter is equal to 3

Connectez-vous pour commenter.

Réponses (1)

Image Analyst
Image Analyst le 23 Avr 2018
If you have the Statistics and Machine Learning Toolbox, you can get the distance of any element from any other element using pdist():
x = [1,5,11,26,46,56,66,76,81]';
distances1D = pdist(x);
distances = squareform(distances1D)
You get:
distances =
0 4 10 25 45 55 65 75 80
4 0 6 21 41 51 61 71 76
10 6 0 15 35 45 55 65 70
25 21 15 0 20 30 40 50 55
45 41 35 20 0 10 20 30 35
55 51 45 30 10 0 10 20 25
65 61 55 40 20 10 0 10 15
75 71 65 50 30 20 10 0 5
80 76 70 55 35 25 15 5 0
For example to get the distance of the second element (5) to the fourth element (26) look in the second row, fourth column and you'll see it's 21, which is abs(26-5).

Community Treasure Hunt

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

Start Hunting!

Translated by