Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a Matlab function mindistance=findclosest(pointset,testpoint). For this fu

ID: 1716479 • Letter: W

Question

Write a Matlab function mindistance=findclosest(pointset,testpoint). For this function, the array pointset may have any number of rows, and has two columns; each row gives the location of a point in two-dimensions (in the plane). Meanwhile, the array testpoint has one row and two columns, which gives the location of a test point in two dimensions. Your Matlab function should return a scalar number, which is the minimum among the distances between testpoint and each point in pointset (where distance is calculated in the usual way using the Pythagorean theorem).

Explanation / Answer

Solution :

%the function definition is :

function [mindistance]=findclosest(pointset,testpoint)
% Summary of this function goes here
%   Detailed explanation goes here

count = size(pointset);

mindistance=inf;
for i=1:count
temp = [pointset(i,1),pointset(i,2)];
X = [temp;testpoint];
distance = pdist(X,'euclidean');

if distance <= mindistance
    mindistance = distance;
end

end


%************************sample run :

X = [0 1;1 0;-1 0;0 -1; -100 100];

t = [0 .5]

findclosest(X,t)

ans =

    0.5000 % which is obvious.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote