필터 지우기
필터 지우기

Average distance from the origin

조회 수: 1 (최근 30일)
TS
TS 2015년 5월 4일
댓글: Star Strider 2015년 5월 4일
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.

채택된 답변

Star Strider
Star Strider 2015년 5월 4일
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
TS
TS 2015년 5월 4일
Oh! Thank you! I completely forgot that there were built in functions for this!
Star Strider
Star Strider 2015년 5월 4일
My pleasure!

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

추가 답변 (1개)

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

카테고리

Help CenterFile Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by