Borrar filtros
Borrar filtros

Average distance from the origin

1 visualización (últimos 30 días)
TS
TS el 4 de Mayo de 2015
Comentada: Star Strider el 4 de Mayo de 2015
matrix=load('Data');
x = matrix(:,1);
y = matrix(:,2);
distances = sqrt((x-0).^2 + (y-0).^2)
sum(distances)./(size(matrix))
Hey, so I just wanted to make sure I'm doing this correctly. What I'm trying to do is find the average distance between points in a matrix and the origin. I got an answer I'm not quite sure about being correct, so I wanted to be safe.

Respuesta aceptada

Star Strider
Star Strider el 4 de Mayo de 2015
You seem to be doing it correctly, but if you can use the built-in functions, I would use hypot and mean:
distances = hypot(x,y);
avg_dist = mean(distances);
  2 comentarios
TS
TS el 4 de Mayo de 2015
Oh! Thank you! I completely forgot that there were built in functions for this!
Star Strider
Star Strider el 4 de Mayo de 2015
My pleasure!

Iniciar sesión para comentar.

Más respuestas (1)

Chad Greene
Chad Greene el 4 de Mayo de 2015
Looks right, but you could write it more simply:
matrix=load('Data');
x = matrix(:,1);
y = matrix(:,2);
distances = hypot(x,y);
mean(distances)

Categorías

Más información sobre Creating and Concatenating Matrices en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by