Thursday, March 24, 2011

3-24-11: "Neutral Site" MATLAB Script

I'm not a big college basketball fan. I casually follow my sister's school, the University of North Carolina at Wilmington, but since they didn't make the NCAA Tournament (or the NIT or the one below that), I decided to throw $5 into a now broken bracket and watch a select few games during the tournament.

One thing I couldn't help but notice is that one team always seems to have a home court advantage over the other teams. I realize that with 16 teams, some one is going to be closer, so I don't really think there's much the NCAA can do about it within reason. But it got me thinking: how could I write a script that would figure out the best neutral site given a group of teams?

I threw something together in MATLAB (code below) in a couple hours. It's very simple and is absolutely the most inefficient and brute force way to go about solving the problem, but it is scalable. I ran through it a few times with six locations and got it to simulate in about 50 seconds. Not great, but this isn't a really time-critical application.

I assumed all teams are just points on a 1001 x 1001 grid (with the origin at the center), and chugged through all 1,002,001 points on the grid and calculated the distance to each set point (team location). The user enters how many teams and then enters the coordinates for each team (I didn't make it very simple for the user; something to work on). For each test point (all 1,002,001 of them) I have it calculate the Coefficient of Variation for the set of distances between each team. The point with the lowest COV is the selected "neutral site" as it is roughly equally far from each team. The script displays the neutral site's coordinates and COV, and plots all of the points with the neutral site filled in.

%% Neutral Site Calculator

% Determines the ideal "neutral" site among an assortment of points

% The number of sites is user configurable

% The site locations are entered by the user

% The script assumes all sites are located on a set grid of points

% Author: Pat Canny



disp('Neutral Site Calculator')

disp('Assume a 1000 x 1000 grid')

%% User Inputs

num_sites = input('Enter number of sites: ');

while num_sites <=1

disp('Number of sites must be greater than one')

num_sites = input('Enter number of sites: ');


set_point_x_array = zeros(1,num_sites);

for i=1:num_sites

set_point_x_array(i) = input('Enter x coordinate for site : ');


set_point_y_array = zeros(1,num_sites);

for i=1:num_sites

set_point_y_array(i) = input('Enter y coordinate for site : ');



%% Calculate average distance (vector magnitude) from each test point

test_point_x_array = -500:500;

test_point_y_array = -500:500;

grid_size = length(test_point_x_array)*length(test_point_y_array);

distance_matrix = zeros(length(test_point_x_array),length(test_point_y_array));

d = zeros(1,num_sites); % initialize vector magnitude array


for j = 1:length(test_point_y_array) % y dimension

for k = 1:length(test_point_x_array) % x dimension

for i = 1:num_sites % calculate vector magnitude to each set point

x = set_point_x_array(i);

y = set_point_y_array(i);

test_x = k-501;

test_y = 501-j;

d(i) = sqrt((test_x - x)^2 + (test_y-y)^2); % vector magnitude


distance_cov_sqd = (std(d) / mean(d))^2;

distance_matrix(j,k) = distance_cov_sqd;




min_distance_cov_sqd = min(distance_matrix(:));

[m,n] = find(distance_matrix == min_distance_cov_sqd);

neutral_site_x = mean(n)-501;

neutral_site_y = 501-mean(m);


%% Display results

str1 = ['The neutral site is located at: (',num2str(neutral_site_x),',',num2str(neutral_site_y),')'];


str2 = ['The minimum distance COV is: ',num2str(min_distance_cov_sqd)];


close all


scatter(set_point_x_array, set_point_y_array)

hold on

scatter(neutral_site_x, neutral_site_y,'filled')

grid on

No comments: