{"id":6028,"date":"2019-01-22T16:28:05","date_gmt":"2019-01-22T21:28:05","guid":{"rendered":"https:\/\/blogs.mathworks.com\/community\/?p=6028"},"modified":"2019-01-22T16:28:05","modified_gmt":"2019-01-22T21:28:05","slug":"football-squares-updated-for-superbowl-liii","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/community\/2019\/01\/22\/football-squares-updated-for-superbowl-liii\/","title":{"rendered":"Football Squares Updated for Superbowl LIII"},"content":{"rendered":"<p>I wrote about Football Squares <a href=\"https:\/\/blogs.mathworks.com\/community\/2013\/01\/07\/football-squares-with-matlab\/?\">a long time ago<\/a>. In light of the upcoming Superbowl, a few people have asked me to brush up my code. As a bonus, it&#8217;s been so long since the last time I did this that I can show off some new MATLAB functionality. Here goes&#8230;<\/p>\n<h3>The game<\/h3>\n<p>Is this game is popular with other sports in other countries? I have no idea. But it&#8217;s a simple way to let a group of people wager on the outcome of a game.<\/p>\n<p>First you make a 10-by-10 grid. Each square in the grid corresponds to a pair of one-digit numbers. These one-digit numbers, in turn, correspond to the last digit in the final score of one of the two teams. Before the game, everyone buys one or more squares until they&#8217;ve all been sold. Now, if the Alligators (team A) go on to defeat the Buckaroos (team B) by a score of 17 to 10, then the owner of the square at location (7,0) would be the winner.<\/p>\n<p>As you can imagine, some score pairs are much more likely than others. For this reason, you buy the squares <em>before<\/em> the numbered labels are applied. In other words, it&#8217;s dumb luck whether or not you win. The best kind of beer-and-ballgame wager!<\/p>\n<p>I&#8217;ll spell out how it works in this Live Script.<\/p>\n<h3>Draw the grid<\/h3>\n<pre id=\"matlabcode\" class=\"matlab-code\" style=\"background-color: #f7f7f7; font-family: monospace; font-weight: normal; padding-top: 5px; padding-bottom: 5px; line-height: 150%; border: 1px solid #E9E9E9;\">a = invhilb(10)&lt;0;\r\n<\/pre>\n<p>Why <a href=\"https:\/\/www.mathworks.com\/help\/matlab\/ref\/invhilb.html\"> INVHILB<\/a>? See <a href=\"http:\/\/www.mathworks.com\/matlabcentral\/cody\/problems\/4-make-a-checkerboard-matrix\">this Cody problem<\/a>.<\/p>\n<pre id=\"matlabcode\" class=\"matlab-code\" style=\"background-color: #f7f7f7; font-family: monospace; font-weight: normal; padding-top: 5px; padding-bottom: 5px; line-height: 150%; border: 1px solid #E9E9E9;\">tick = 0:9;\r\nimagesc(tick,tick,a)\r\ncolormap([1; 0.8]*[1 1 1])\r\nset(gca, <span style=\"color: #0000ff;\">...<\/span>\r\n    <span style=\"color: #a020f0;\">'FontSize'<\/span>,12, <span style=\"color: #0000ff;\">...<\/span>\r\n    <span style=\"color: #a020f0;\">'XAxisLocation'<\/span>,<span style=\"color: #a020f0;\">'top'<\/span>, <span style=\"color: #0000ff;\">...<\/span>\r\n    <span style=\"color: #a020f0;\">'XTick'<\/span>,tick, <span style=\"color: #0000ff;\">...<\/span>\r\n    <span style=\"color: #a020f0;\">'YTick'<\/span>,tick, <span style=\"color: #0000ff;\">...<\/span>\r\n    <span style=\"color: #a020f0;\">'XTickLabel'<\/span>,<span style=\"color: #a020f0;\">'?'<\/span>, <span style=\"color: #0000ff;\">...<\/span>\r\n    <span style=\"color: #a020f0;\">'YTickLabel'<\/span>,<span style=\"color: #a020f0;\">'?'<\/span>)\r\naxis <span style=\"color: #a020f0;\">square<\/span>\r\nxlabel(\"Last Digit of the Patriots Score\")\r\nylabel(\"Last Digit of Rams Score\")\r\n<\/pre>\n<p><img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/community\/files\/football_1.png\" \/><\/p>\n<p>So you can see that we don&#8217;t actually know which numbers will correspond to each square.<\/p>\n<h3>Pick the squares<\/h3>\n<p>We&#8217;ll use a random name generator from the web to come up with a list of players. <a href=\"https:\/\/www.mathworks.com\/help\/matlab\/ref\/webread.html\"> WEBREAD<\/a> is a nifty command: it&#8217;s automatically turning the returned JSON into a structure for me.<\/p>\n<pre id=\"matlabcode\" class=\"matlab-code\" style=\"background-color: #f7f7f7; font-family: monospace; font-weight: normal; padding-top: 5px; padding-bottom: 5px; line-height: 150%; border: 1px solid #E9E9E9;\">url = <span style=\"color: #a020f0;\">'https:\/\/uinames.com\/api\/?amount=12&amp;region=United%20States'<\/span>;\r\nnames = webread(url);\r\n<\/pre>\n<p>These are all the people coming to your party. Don&#8217;t worry, they&#8217;re a lot of fun.<\/p>\n<pre id=\"matlabcode\" class=\"matlab-code\" style=\"background-color: #f7f7f7; font-family: monospace; font-weight: normal; padding-top: 5px; padding-bottom: 5px; line-height: 150%; border: 1px solid #E9E9E9;\">allNames = string({names.name});\r\ndisp(allNames.join(<span style=\"color: #a020f0;\">', '<\/span>))\r\n<\/pre>\n<pre class=\"output\" style=\"font-family: monospace; border: none; background-color: white; color: rgba(64, 64, 64, 1);\">Thomas, Kathleen, Matthew, Jean, Christine, Kathy, Martha, Scott, Brian, Olivia, Harry, Ralph\r\n<\/pre>\n<p>Now we&#8217;ll simulate people buying up the squares. Say they&#8217;re paying one dollar for each square, for a nice $100 jackpot.<\/p>\n<pre id=\"matlabcode\" class=\"matlab-code\" style=\"background-color: #f7f7f7; font-family: monospace; font-weight: normal; padding-top: 5px; padding-bottom: 5px; line-height: 150%; border: 1px solid #E9E9E9;\">groupSize = 12;\r\nnameIndexGrid = randi(groupSize,[10 10]);\r\n<span style=\"color: #0000ff;\">for<\/span> i = 1:numel(a)\r\n    [row,col] = ind2sub([10,10],i);\r\n    text(col-1,row-1,allNames(nameIndexGrid(col,row)),<span style=\"color: #0000ff;\">...<\/span>\r\n        <span style=\"color: #a020f0;\">'FontSize'<\/span>, 6, <span style=\"color: #0000ff;\">...<\/span>\r\n        <span style=\"color: #a020f0;\">'Color'<\/span>,<span style=\"color: #a020f0;\">'blue'<\/span>, <span style=\"color: #0000ff;\">...<\/span>\r\n        <span style=\"color: #a020f0;\">'HorizontalAlignment'<\/span>,<span style=\"color: #a020f0;\">'center'<\/span>);\r\n<span style=\"color: #0000ff;\">end<\/span>\r\n<\/pre>\n<p><img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/community\/files\/football_2.png\" \/><\/p>\n<h3>Assign the score digits<\/h3>\n<p>Now that the squares have all been purchased, it&#8217;s time to assign the actual digits to the grid. This is a perfect task for <a href=\"https:\/\/www.mathworks.com\/help\/matlab\/ref\/randperm.html\"> RANDPERM<\/a>.<\/p>\n<pre id=\"matlabcode\" class=\"matlab-code\" style=\"background-color: #f7f7f7; font-family: monospace; font-weight: normal; padding-top: 5px; padding-bottom: 5px; line-height: 150%; border: 1px solid #E9E9E9;\">xLabels = randperm(10)-1;\r\nyLabels = randperm(10)-1;\r\nset(gca, <span style=\"color: #0000ff;\">...<\/span>\r\n    <span style=\"color: #a020f0;\">'XTickLabel'<\/span>,string(xLabels), <span style=\"color: #0000ff;\">...<\/span>\r\n    <span style=\"color: #a020f0;\">'YTickLabel'<\/span>,string(yLabels))\r\n<\/pre>\n<p><img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/community\/files\/football_3.png\" \/><\/p>\n<h3>What are the last digits of the final score?<\/h3>\n<p>Now I&#8217;m going to come up with a fake final score. Even though this is a simulation, I won&#8217;t reveal who wins. No point in stirring up trouble! We&#8217;re based in Boston, so we&#8217;re fond of the Patriots. But believe me, we know that&#8217;s not a &#8230;<em>ahem<\/em>&#8230; widely held opinion across the country.<\/p>\n<pre id=\"matlabcode\" class=\"matlab-code\" style=\"background-color: #f7f7f7; font-family: monospace; font-weight: normal; padding-top: 5px; padding-bottom: 5px; line-height: 150%; border: 1px solid #E9E9E9;\">patriotsFinalScore = randi(30);\r\nramsFinalScore = randi(30);\r\n<\/pre>\n<p>Let&#8217;s make a little anonymous function here, because anonymous functions are cool.<\/p>\n<pre id=\"matlabcode\" class=\"matlab-code\" style=\"background-color: #f7f7f7; font-family: monospace; font-weight: normal; padding-top: 5px; padding-bottom: 5px; line-height: 150%; border: 1px solid #E9E9E9;\">getLastDigit = @(score) score - 10*(floor(score\/10));\r\n<\/pre>\n<p>What are the last digits in the score?<\/p>\n<pre id=\"matlabcode\" class=\"matlab-code\" style=\"background-color: #f7f7f7; font-family: monospace; font-weight: normal; padding-top: 5px; padding-bottom: 5px; line-height: 150%; border: 1px solid #E9E9E9;\">patriotsLastDigit = getLastDigit(patriotsFinalScore)\r\n<\/pre>\n<pre class=\"output\" style=\"font-family: monospace; border: none; background-color: white; color: rgba(64, 64, 64, 1);\">patriotsLastDigit = 6\r\n<\/pre>\n<pre id=\"matlabcode\" class=\"matlab-code\" style=\"background-color: #f7f7f7; font-family: monospace; font-weight: normal; padding-top: 5px; padding-bottom: 5px; line-height: 150%; border: 1px solid #E9E9E9;\">ramsLastDigit = getLastDigit(ramsFinalScore)\r\n<\/pre>\n<pre class=\"output\" style=\"font-family: monospace; border: none; background-color: white; color: rgba(64, 64, 64, 1);\">ramsLastDigit = 6\r\n<\/pre>\n<pre id=\"matlabcode\" class=\"matlab-code\" style=\"background-color: #f7f7f7; font-family: monospace; font-weight: normal; padding-top: 5px; padding-bottom: 5px; line-height: 150%; border: 1px solid #E9E9E9;\">ix1 = find(patriotsLastDigit==xLabels);\r\nix2 = find(ramsLastDigit==yLabels);\r\nset(findobj(<span style=\"color: #a020f0;\">'Type'<\/span>,<span style=\"color: #a020f0;\">'text'<\/span>),<span style=\"color: #a020f0;\">'Color'<\/span>,0.6*[1 1 1])\r\npatch(ix1-1+[0 1 1 0 0]-0.5,ix2-1+[0 0 1 1 0]-0.5,[1 0.7 0.7],<span style=\"color: #a020f0;\">'EdgeColor'<\/span>,<span style=\"color: #a020f0;\">'none'<\/span>)\r\ntext(ix1-1,ix2-1,allNames(nameIndexGrid(ix1,ix2))+\" wins<span style=\"color: #b38c00;\">!\",...<\/span>\r\n        <span style=\"color: #a020f0;\">'FontSize'<\/span>,18, <span style=\"color: #0000ff;\">...<\/span>\r\n        <span style=\"color: #a020f0;\">'FontWeight'<\/span>,<span style=\"color: #a020f0;\">'bold'<\/span>, <span style=\"color: #0000ff;\">...<\/span>\r\n        <span style=\"color: #a020f0;\">'Color'<\/span>,<span style=\"color: #a020f0;\">'red'<\/span>, <span style=\"color: #0000ff;\">...<\/span>\r\n        <span style=\"color: #a020f0;\">'HorizontalAlignment'<\/span>,<span style=\"color: #a020f0;\">'center'<\/span>);\r\n<\/pre>\n<p><img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/community\/files\/football_4.png\" \/><\/p>\n","protected":false},"excerpt":{"rendered":"<div class=\"overview-image\"><img decoding=\"async\"  class=\"img-responsive\" src=\"https:\/\/blogs.mathworks.com\/community\/files\/football_1.png\" onError=\"this.style.display ='none';\" \/><\/div>\n<p>I wrote about Football Squares a long time ago. In light of the upcoming Superbowl, a few people have asked me to brush up my code. As a bonus, it&#8217;s been so long since the last time I did this&#8230; <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/community\/2019\/01\/22\/football-squares-updated-for-superbowl-liii\/\">read more >><\/a><\/p>\n","protected":false},"author":69,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[1],"tags":[],"_links":{"self":[{"href":"https:\/\/blogs.mathworks.com\/community\/wp-json\/wp\/v2\/posts\/6028"}],"collection":[{"href":"https:\/\/blogs.mathworks.com\/community\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blogs.mathworks.com\/community\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/community\/wp-json\/wp\/v2\/users\/69"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/community\/wp-json\/wp\/v2\/comments?post=6028"}],"version-history":[{"count":2,"href":"https:\/\/blogs.mathworks.com\/community\/wp-json\/wp\/v2\/posts\/6028\/revisions"}],"predecessor-version":[{"id":6032,"href":"https:\/\/blogs.mathworks.com\/community\/wp-json\/wp\/v2\/posts\/6028\/revisions\/6032"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/community\/wp-json\/wp\/v2\/media?parent=6028"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/community\/wp-json\/wp\/v2\/categories?post=6028"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/community\/wp-json\/wp\/v2\/tags?post=6028"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}