Average distance from the origin
5 次查看(过去 30 天)
显示 更早的评论
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.
0 个评论
采纳的回答
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);
更多回答(1 个)
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)
1 个评论
Chad Greene
2015-5-4
Also, plot the data and verify for yourself.
plot(x,y,'bo')
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Creating and Concatenating Matrices 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!