{"id":3898,"date":"2016-07-14T08:00:15","date_gmt":"2016-07-14T12:00:15","guid":{"rendered":"https:\/\/blogs.mathworks.com\/community\/?p=3898"},"modified":"2016-07-22T11:13:31","modified_gmt":"2016-07-22T15:13:31","slug":"flag-math-on-bastille-day","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/community\/2016\/07\/14\/flag-math-on-bastille-day\/","title":{"rendered":"Flag Math on Bastille Day"},"content":{"rendered":"<p>Let me start with a graphical riddle. In what sense is the following equivalence true?<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/community\/files\/mlxfileimages-5.gif\" \/><\/p>\n<p>To learn the answer, read on!<\/p>\n<p>I recently came across <a href=\"https:\/\/nickhigham.wordpress.com\/2016\/06\/29\/the-one-line-maze-program-in-matlab\/\">Nick Higham&#8217;s fun post on one-liners in MATLAB<\/a>. That piece and the approach of Bastille Day on July 14th made me think of another one-line program.<\/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;\">image([3 2 1]);colormap(flag)\r\n<\/pre>\n<p><img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/community\/files\/mlxfileimages-10.gif\" \/><\/p>\n<p>Pretty impressive, considering that it&#8217;s only 29 characters long. Is this the shortest flag program we can write?<\/p>\n<p>As I thought about flags and MATLAB, I gave myself the following task. Using only the colormap &#8220;flag&#8221; and images of vectors, how many national flags can we construct? Probably more than you might guess. Let&#8217;s go on a little tour.<\/p>\n<h3>Tidying Up the Flag<\/h3>\n<p>Proper flags shouldn&#8217;t have numbers running their edges, so I&#8217;d like to hide the tick marks and labels. Since I plan to do this multiple times in the script below, I&#8217;m going to use an anonymous function for compactness. It&#8217;s not necessary, but I like how it keeps the code tidy.<\/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;\">notick = @(ax) set(ax,<span style=\"color: #a020f0;\">'XTick'<\/span>,[],<span style=\"color: #a020f0;\">'YTick'<\/span>,[])\r\n<\/pre>\n<pre class=\"output\" style=\"font-family: monospace; border: none; background-color: white; color: rgba(64, 64, 64, 1);\">notick = \r\n\u00a0\u00a0\u00a0\u00a0@(ax)set(ax,'XTick',[],'YTick',[])\r\n\r\n\r\n<\/pre>\n<h3>France<\/h3>\n<p>In honor of Bastille Day, we start with France.<\/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;\">france = [3 2 1]\r\n<\/pre>\n<pre class=\"output\" style=\"font-family: monospace; border: none; background-color: white; color: rgba(64, 64, 64, 1);\">france = \r\n\r\n     3     2     1\r\n\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;\">image(france)\r\ntitle(<span style=\"color: #a020f0;\">'Vive la France!'<\/span>)\r\nnotick(gca)\r\n<\/pre>\n<p><img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/community\/files\/mlxfileimages-2.gif\" \/><\/p>\n<p>France is the only red, white, and blue tricolor with vertical bars. But if we look at horizontal patterns, we find many more.<\/p>\n<h3>Netherlands<\/h3>\n<p>Netherlands is a <a href=\"https:\/\/www.mathworks.com\/help\/matlab\/ref\/rot90.html\">rotated<\/a> version of France.<\/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;\">netherlands = rot90(france)\r\n<\/pre>\n<pre class=\"output\" style=\"font-family: monospace; border: none; background-color: white; color: rgba(64, 64, 64, 1);\">netherlands = \r\n\r\n     1\r\n     2\r\n     3\r\n\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;\">image(netherlands);\r\ntitle(<span style=\"color: #a020f0;\">'The Netherlands'<\/span>)\r\nnotick(gca)\r\n<\/pre>\n<p><img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/community\/files\/mlxfileimages-1.gif\" \/><\/p>\n<h3>Luxembourg<\/h3>\n<p>Luxembourg, next door to the Netherlands, uses essentially the same flag. A pedantic Luxembourger or professional vexillologist may protest that the colors are slightly lighter and the aspect ratio is slightly different. We will gloss over these details.<\/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;\">luxembourg = netherlands\r\n<\/pre>\n<pre class=\"output\" style=\"font-family: monospace; border: none; background-color: white; color: rgba(64, 64, 64, 1);\">luxembourg = \r\n\r\n     1\r\n     2\r\n     3\r\n\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;\">image(luxembourg)\r\ntitle(<span style=\"color: #a020f0;\">'Luxembourg'<\/span>)\r\nnotick(gca)\r\n<\/pre>\n<p><img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/community\/files\/mlxfileimages-3.gif\" \/><\/p>\n<h3>Thailand<\/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;\">thailand = [netherlands; flipud(netherlands)]\r\n<\/pre>\n<pre class=\"output\" style=\"font-family: monospace; border: none; background-color: white; color: rgba(64, 64, 64, 1);\">thailand = \r\n\r\n     1\r\n     2\r\n     3\r\n     3\r\n     2\r\n     1\r\n\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;\">image(thailand)\r\ntitle(<span style=\"color: #a020f0;\">'Thailand'<\/span>)\r\nnotick(gca)\r\n<\/pre>\n<p><img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/community\/files\/mlxfileimages.gif\" \/><\/p>\n<p>Now we see the significance of the graphical riddle that we listed at the top of this post.<\/p>\n<h3>Russia<\/h3>\n<p>Russia&#8217;s flag is a variant on the three horizontal bands. We&#8217;ll use <a href=\"https:\/\/www.mathworks.com\/help\/matlab\/ref\/circshift.html\">circshift<\/a> to do a circular permutation of the colors in the Netherlands flag.<\/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;\">russia = circshift(netherlands,2)\r\n<\/pre>\n<pre class=\"output\" style=\"font-family: monospace; border: none; background-color: white; color: rgba(64, 64, 64, 1);\">russia = \r\n\r\n     2\r\n     3\r\n     1\r\n\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;\">image(russia)\r\ntitle(<span style=\"color: #a020f0;\">'Russia'<\/span>)\r\nnotick(gca)\r\n<\/pre>\n<p><img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/community\/files\/mlxfileimages-4.gif\" \/><\/p>\n<h3>Austria<\/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;\">austria = netherlands([1 2 1])\r\n<\/pre>\n<pre class=\"output\" style=\"font-family: monospace; border: none; background-color: white; color: rgba(64, 64, 64, 1);\">austria = \r\n\r\n     1\r\n     2\r\n     1\r\n\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;\">image(austria)\r\ntitle(<span style=\"color: #a020f0;\">'Austria'<\/span>)\r\nnotick(gca)\r\n<\/pre>\n<p><img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/community\/files\/mlxfileimages-7.gif\" \/><\/p>\n<h3>Indonesia<\/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;\">indonesia = netherlands(1:2)\r\n<\/pre>\n<pre class=\"output\" style=\"font-family: monospace; border: none; background-color: white; color: rgba(64, 64, 64, 1);\">indonesia = \r\n\r\n     1\r\n     2\r\n\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;\">image(indonesia)\r\ntitle(<span style=\"color: #a020f0;\">'Indonesia'<\/span>)\r\nnotick(gca)\r\n<\/pre>\n<p><img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/community\/files\/mlxfileimages-6.gif\" \/><\/p>\n<h3>Monaco<\/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;\">monaco = indonesia\r\n<\/pre>\n<pre class=\"output\" style=\"font-family: monospace; border: none; background-color: white; color: rgba(64, 64, 64, 1);\">monaco = \r\n\r\n     1\r\n     2\r\n\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;\">image(monaco);\r\ntitle(<span style=\"color: #a020f0;\">'Monaco'<\/span>)\r\nnotick(gca)\r\n<\/pre>\n<p><img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/community\/files\/mlxfileimages-9.gif\" \/><\/p>\n<h3>Poland<\/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;\">poland = flipud(monaco)\r\n<\/pre>\n<pre class=\"output\" style=\"font-family: monospace; border: none; background-color: white; color: rgba(64, 64, 64, 1);\">poland = \r\n\r\n     2\r\n     1\r\n\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;\">image(poland);\r\ntitle(<span style=\"color: #a020f0;\">'Poland'<\/span>)\r\nnotick(gca)\r\n<\/pre>\n<p><img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/community\/files\/mlxfileimages-8.gif\" \/><\/p>\n<p>There you have it. A veritable Tour d&#8217;Image. I count nine separate vector flags using only red, white, or blue. Did I miss any?<\/p>\n<p>As an exercise for the reader, express all flags in terms of the vector &#8220;france&#8221;.<\/p>\n<h3>Addendum #1: Costa Rica<\/h3>\n<p>In the comments, Matt Tearle points out another flag that fits the model. It&#8217;s a kind of dual to Thailand. I am now convinced that vexillology conventions should always occur in the Netherlands, its flag being the indispensable element at the center of all flag alchemy.<\/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;\">costarica = [flipud(netherlands); netherlands]\r\n<\/pre>\n<pre class=\"output\" style=\"font-family: monospace; border: none; background-color: white; color: rgba(64, 64, 64, 1);\">costarica = \r\n\r\n     3\r\n     2\r\n     1\r\n     1\r\n     2\r\n     3\r\n\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;\">image(costarica);\r\ntitle(<span style=\"color: #a020f0;\">'Costa Rica'<\/span>)\r\nnotick(gca)\r\n<\/pre>\n<p><img decoding=\"async\" loading=\"lazy\" width=\"560\" height=\"420\" class=\"alignnone size-full wp-image-3932\" src=\"https:\/\/blogs.mathworks.com\/community\/files\/flags_12.png\" alt=\"flags_12\" \/><\/p>\n<h3>Addendum #2<\/h3>\n<p>And Richard Alcock from MathWorks UK writes in to give us this timely and poignant example.<\/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;\">reflex_blue = [0 51 153]\/255;\r\nyellow = [255 204 0]\/255;\r\n[x, y] = pol2cart(linspace(0, 2*pi, 13), 1\/3); plot(x, y, <span style=\"color: #a020f0;\">'p'<\/span>, <span style=\"color: #a020f0;\">'MarkerSize'<\/span>, 20, <span style=\"color: #0000ff;\">...<\/span>\r\n    <span style=\"color: #a020f0;\">'MarkerEdgeColor'<\/span>, yellow, <span style=\"color: #a020f0;\">'MarkerFaceColor'<\/span>, yellow); axis(<span style=\"color: #a020f0;\">'equal'<\/span>); set(gca, <span style=\"color: #a020f0;\">'Color'<\/span>, reflex_blue,  <span style=\"color: #0000ff;\">...<\/span>\r\n    <span style=\"color: #a020f0;\">'XTick'<\/span>, [], <span style=\"color: #a020f0;\">'YTick'<\/span>, [], <span style=\"color: #0000ff;\">...<\/span>\r\n    <span style=\"color: #a020f0;\">'Xlim'<\/span>, [-.75 .75], <span style=\"color: #a020f0;\">'Ylim'<\/span>, [-.5, .5])\r\n<\/pre>\n<p><img decoding=\"async\" loading=\"lazy\" width=\"560\" height=\"420\" class=\"alignnone size-full wp-image-3933\" src=\"https:\/\/blogs.mathworks.com\/community\/files\/flags_13.png\" alt=\"flags_13\" \/><\/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;\"><\/pre>\n<h3>Addendum #3: Yemen<\/h3>\n<p>Sean de Wolski reminded me that black is one of the four colors of the &#8220;flag&#8221; colormap, thereby letting us add the Yemeni flag to our list. I thought the addition of black might give us a few more flags, but Yemen seems to be the end of it. <\/p>\n<pre class=\"matlab-code\" id=\"matlabcode\" style=\"background-color: #F7F7F7;font-family: monospace;font-weight:normal;border-style: solid; border-width: 1px ;border-color:#E9E9E9;padding-top:5px;padding-bottom:5px;line-height:150%;\">yemen = [1; 2; 4];\r\nimage(yemen)\r\ntitle(<span style=\"color:rgb(160, 32, 240);\">'Yemen'<\/span>)\r\nnotick(gca)\r\n<\/pre>\n<p><img decoding=\"async\" loading=\"lazy\" width=\"560\" height=\"420\" src=\"https:\/\/blogs.mathworks.com\/community\/files\/flags_14.png\" alt=\"flags_14\" class=\"alignnone size-full wp-image-3958\" \/><\/p>\n","protected":false},"excerpt":{"rendered":"<div class=\"overview-image\"><img decoding=\"async\"  class=\"img-responsive\" src=\"https:\/\/blogs.mathworks.com\/community\/files\/mlxfileimages-5.gif\" onError=\"this.style.display ='none';\" \/><\/div>\n<p>Let me start with a graphical riddle. In what sense is the following equivalence true?<\/p>\n<p>To learn the answer, read on!<br \/>\nI recently came across Nick Higham&#8217;s fun post on one-liners in MATLAB. That&#8230; <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/community\/2016\/07\/14\/flag-math-on-bastille-day\/\">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\/3898"}],"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=3898"}],"version-history":[{"count":7,"href":"https:\/\/blogs.mathworks.com\/community\/wp-json\/wp\/v2\/posts\/3898\/revisions"}],"predecessor-version":[{"id":3959,"href":"https:\/\/blogs.mathworks.com\/community\/wp-json\/wp\/v2\/posts\/3898\/revisions\/3959"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/community\/wp-json\/wp\/v2\/media?parent=3898"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/community\/wp-json\/wp\/v2\/categories?post=3898"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/community\/wp-json\/wp\/v2\/tags?post=3898"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}