Define a function named maxDistToCenter that takes 2 arguments as input: a singl
ID: 2293147 • Letter: D
Question
Define a function named maxDistToCenter that takes 2 arguments as input: a single point, called the center, and a list of other points. The first line of the function should look like: function result maxDistToCenter(center, other_pts) You may assume that center is a 1 x 2 vector containing the r- and y-coordinates of the center point and that other_pts is a N x 2 matri The function should return the sum distance between the center and the other points. For example, x containing the x- and y-coordinates of N points, one point on each row maxDistToCenter([1, 2]. [1, 2]) should return 0 (the distance between (1, 2) and (1, 2)) maxDistToCenter([0, 0).13, 4]) should return 5 (the distance between (0,0) and (3,4)) maxDistToCenter(0,0), [1, 3:2, 2:3. 1) should return v10+ 8+ V10 the distance sum between (0, 0) and (1, 3), (2, 2), and (3, 1)) Note: you will want to use MATLAB's max functionExplanation / Answer
function result=maxDistToCenter(center,other_pts)
[row_center,col_center]=size(center); % center is a 1X2 vector
[row_other_pts,col_other_pts]=size(other_pts); % other_pts is a NX2 vector
N=row_other_pts;
result=0;
if N==1
result=sqrt(((other_pts(1)-center(1))^2)+((other_pts(2)-center(2))^2));
else
for i=1:N
result=result+sqrt(((other_pts(i)-center(1))^2)+((other_pts(i+N)-center(2))^2));
end
end
end
%%
maxDistToCenter([1 2],[1 2])
ans =
0
maxDistToCenter([0 0],[3 4])
ans =
5
maxDistToCenter([0 0],[1 3;2 2;3 1])
ans =
9.1530
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.