# Calculating distance between two points from a matrix in 2d and 3d

10 Ansichten (letzte 30 Tage)
lisa Liang am 21 Jul. 2020
Kommentiert: paul harder am 8 Apr. 2021
Hi everyone!
I am new to MatLab and coding. I have a set of points in x and y coordinates, and they are sorted in a 701*14 matrix called 'rawdata'. The first two column are x and y coordinate for point#1 and the next two are the x and y coordinates for point#2 (as shown in screenshot). I want to calculate the distance between point#1 and point#2 for each row and have the resulted distance in matrix. I think I'd calculate distance by sqrt((x2 - x1) ^ 2 + (y2 - y1) ^ 2) in 2d or sqrt((x2 - x1) ^ 2 + (y2 - y1) ^ 2 + (z2 - z1) ^ 2)in 3d? But how do I write in a formatt that matlab can read and calculate?
Sorry I am a coding newb. And thank you very much for your help in advance!
##### 0 Kommentare-2 ältere Kommentare anzeigen-2 ältere Kommentare ausblenden

Melden Sie sich an, um zu kommentieren.

### Akzeptierte Antwort

waqas am 21 Jul. 2020
This would be the implementation of your formula. But, NaN would be a problem for you since 3rd and 4th columns are NaN and would make the output distance NaN too.
dist = sqrt(((A(:,3)-A(:,1)).^2 + (A(:,4)-A(:,1)).^2))
##### 2 KommentareKeine anzeigenKeine ausblenden
lisa Liang am 24 Jul. 2020
Thank you very much!
paul harder am 8 Apr. 2021
just to clarify a minor typo, the last 1 should be a 2
dist = sqrt(((A(:,3)-A(:,1)).^2 + (A(:,4)-A(:,2)).^2))

Melden Sie sich an, um zu kommentieren.

### Kategorien

Mehr zu NaNs finden Sie in Help Center und File Exchange

### Community Treasure Hunt

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

Start Hunting!

Translated by