{"id":95,"date":"2007-06-21T10:16:02","date_gmt":"2007-06-21T15:16:02","guid":{"rendered":"https:\/\/blogs.mathworks.com\/loren\/2007\/06\/21\/indexing-terminology\/"},"modified":"2007-06-21T12:38:37","modified_gmt":"2007-06-21T17:38:37","slug":"indexing-terminology","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/loren\/2007\/06\/21\/indexing-terminology\/","title":{"rendered":"Indexing Terminology"},"content":{"rendered":"<div xmlns:mwsh=\"https:\/\/www.mathworks.com\/namespace\/mcode\/v1\/syntaxhighlight.dtd\" class=\"content\">\r\n   <introduction>\r\n      <p>Someone recently asked me to discuss X-Y versus row-column indexing.<\/p>\r\n   <\/introduction>\r\n   <h3>Contents<\/h3>\r\n   <div>\r\n      <ul>\r\n         <li><a href=\"#1\">Two Conventions<\/a><\/li>\r\n         <li><a href=\"#2\">Matrix versus Cartesian Frameworks<\/a><\/li>\r\n         <li><a href=\"#7\">Plotting Matrices<\/a><\/li>\r\n         <li><a href=\"#10\">Creating an N-Dimensional Grid<\/a><\/li>\r\n         <li><a href=\"#11\">Creating an 2- or 3-Dimensional Grid<\/a><\/li>\r\n         <li><a href=\"#14\">Thoughts?<\/a><\/li>\r\n      <\/ul>\r\n   <\/div>\r\n   <h3>Two Conventions<a name=\"1\"><\/a><\/h3>\r\n   <p>When calling functions in MATLAB that create or reference arrays, there are two different conventions for specifying the\r\n      first two dimensions of an array:\r\n   <\/p>\r\n   <div>\r\n      <ul>\r\n         <li>matrix indexing, i.e., rows and columns<\/li>\r\n         <li>Cartesian , i.e., X and Y<\/li>\r\n      <\/ul>\r\n   <\/div>\r\n   <p>These different conventions get used depending on whether the array is strictly an array or when it has a probable physical\r\n      interpretation in terms of Cartesian grid.\r\n   <\/p>\r\n   <h3>Matrix versus Cartesian Frameworks<a name=\"2\"><\/a><\/h3>\r\n   <p>In MATLAB, we index into matrices using row and column indices.  That is, for matrix<\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">A = magic(3)<\/pre><pre style=\"font-style:oblique\">A =\r\n     8     1     6\r\n     3     5     7\r\n     4     9     2\r\n<\/pre><p>I can reference the last element in the second row like this:<\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">A(2,3)<\/pre><pre style=\"font-style:oblique\">ans =\r\n     7\r\n<\/pre><p>or<\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">A(2,end)<\/pre><pre style=\"font-style:oblique\">ans =\r\n     7\r\n<\/pre><p>and to retrieve the first element of columns 1 and 3, I type this:<\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">A(1,[1 3])<\/pre><pre style=\"font-style:oblique\">ans =\r\n     8     6\r\n<\/pre><p>Check out <a href=\"https:\/\/blogs.mathworks.com\/loren\/2006\/08\/09\/essence-of-indexing\/\">this post<\/a> to get a fairly complete picture of indexing in MATLAB.\r\n   <\/p>\r\n   <h3>Plotting Matrices<a name=\"7\"><\/a><\/h3>\r\n   <p>The options <tt>xy<\/tt> and <tt>ij<\/tt> for <a href=\"https:\/\/www.mathworks.com\/help\/matlab\/ref\/axis.html\"><tt>axis<\/tt><\/a> allow us to control whether to view the data just the way the matrix is displayed (the (1,1) element is in the upper left\r\n      corner):\r\n   <\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">colormap(jet(9))\r\nimage(A)\r\ncolorbar<\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/loren\/95\/indexTerm_01.png\"> <p>or we can view the image\/matrix as a top-down view of a physical \"map\" with the y-values now increasing along the vertical\r\n      axis.\r\n   <\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">axis <span style=\"color: #A020F0\">xy<\/span><\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/loren\/95\/indexTerm_02.png\"> <p>In images, the matrices column values plot vertically. In line plots, the columns are plotted vs. their indices, making columns\r\n      plot horizontally.\r\n   <\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">h = plot(A);\r\nlegend(h,{<span style=\"color: #A020F0\">'Column 1'<\/span>,<span style=\"color: #A020F0\">'Column 2'<\/span>,<span style=\"color: #A020F0\">'Column 3'<\/span>})<\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/loren\/95\/indexTerm_03.png\"> <h3>Creating an N-Dimensional Grid<a name=\"10\"><\/a><\/h3>\r\n   <p><a href=\"https:\/\/www.mathworks.com\/help\/matlab\/ref\/ndgrid.html\"><tt>ndgrid<\/tt><\/a> uses matrix indexing since it is designed to suit working in any number of array dimensions.\r\n   <\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">[ii, jj] = ndgrid(1:3,1:2)<\/pre><pre style=\"font-style:oblique\">ii =\r\n     1     1\r\n     2     2\r\n     3     3\r\njj =\r\n     1     2\r\n     1     2\r\n     1     2\r\n<\/pre><h3>Creating an 2- or 3-Dimensional Grid<a name=\"11\"><\/a><\/h3>\r\n   <p>On the other hand, <a href=\"https:\/\/www.mathworks.com\/help\/matlab\/ref\/mesh.html\"><tt>mesh<\/tt><\/a>, interprets its first inputs in a Cartesian framework since it is used primarily for working with 2- and 3-dimensional plots,\r\n      often depicting something over an area or volume.\r\n   <\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">[xx, yy] = meshgrid(1:3,1:2)<\/pre><pre style=\"font-style:oblique\">xx =\r\n     1     2     3\r\n     1     2     3\r\nyy =\r\n     1     1     1\r\n     2     2     2\r\n<\/pre><p>In the <b>Remarks<\/b> section of the documentation for <a href=\"https:\/\/www.mathworks.com\/help\/matlab\/ref\/meshgrid.html\"><tt>meshgrid<\/tt><\/a>, you will find a discussion of this, especially with respect to inverting the first two input arguments compared to <tt>ndgrid<\/tt>. There is related discussion in the <b>Description<\/b> section for the function <a href=\"https:\/\/www.mathworks.com\/help\/matlab\/ref\/mesh.html\"><tt>mesh<\/tt><\/a>.\r\n   <\/p>\r\n   <p>Functions designed for two or three dimension, such as <tt>contour<\/tt> and <tt>quiver<\/tt>, <i>usually<\/i> work in Cartesian coordinates. Functions that are designed to work in N-dimensions generally work with a matrix indexing\r\n      framework.  Be sure to check the help for functions you use to see which convention applies.\r\n   <\/p>\r\n   <h3>Thoughts?<a name=\"14\"><\/a><\/h3>\r\n   <p>Any ideas on what we can do, perhaps in examples or documentation (or eslewhere?) to help people out with this potential confusion?\r\n       If so, please post <a href=\"https:\/\/blogs.mathworks.com\/loren\/?p=95#respond\">here<\/a>.\r\n   <\/p><script language=\"JavaScript\">\r\n<!--\r\n\r\n    function grabCode_a773be6d30f34af8a0ee75c4af586609() {\r\n        \/\/ Remember the title so we can use it in the new page\r\n        title = document.title;\r\n\r\n        \/\/ Break up these strings so that their presence\r\n        \/\/ in the Javascript doesn't mess up the search for\r\n        \/\/ the MATLAB code.\r\n        t1='a773be6d30f34af8a0ee75c4af586609 ' + '##### ' + 'SOURCE BEGIN' + ' #####';\r\n        t2='##### ' + 'SOURCE END' + ' #####' + ' a773be6d30f34af8a0ee75c4af586609';\r\n    \r\n        b=document.getElementsByTagName('body')[0];\r\n        i1=b.innerHTML.indexOf(t1)+t1.length;\r\n        i2=b.innerHTML.indexOf(t2);\r\n \r\n        code_string = b.innerHTML.substring(i1, i2);\r\n        code_string = code_string.replace(\/REPLACE_WITH_DASH_DASH\/g,'--');\r\n\r\n        \/\/ Use \/x3C\/g instead of the less-than character to avoid errors \r\n        \/\/ in the XML parser.\r\n        \/\/ Use '\\x26#60;' instead of '<' so that the XML parser\r\n        \/\/ doesn't go ahead and substitute the less-than character. \r\n        code_string = code_string.replace(\/\\x3C\/g, '\\x26#60;');\r\n\r\n        author = 'Loren Shure';\r\n        copyright = 'Copyright 2007 The MathWorks, Inc.';\r\n\r\n        w = window.open();\r\n        d = w.document;\r\n        d.write('<pre>\\n');\r\n        d.write(code_string);\r\n\r\n        \/\/ Add author and copyright lines at the bottom if specified.\r\n        if ((author.length > 0) || (copyright.length > 0)) {\r\n            d.writeln('');\r\n            d.writeln('%%');\r\n            if (author.length > 0) {\r\n                d.writeln('% _' + author + '_');\r\n            }\r\n            if (copyright.length > 0) {\r\n                d.writeln('% _' + copyright + '_');\r\n            }\r\n        }\r\n\r\n        d.write('<\/pre>\\n');\r\n      \r\n      d.title = title + ' (MATLAB code)';\r\n      d.close();\r\n      }   \r\n      \r\n-->\r\n<\/script><p style=\"text-align: right; font-size: xx-small; font-weight:lighter;   font-style: italic; color: gray\"><br><a href=\"javascript:grabCode_a773be6d30f34af8a0ee75c4af586609()\"><span style=\"font-size: x-small;        font-style: italic;\">Get \r\n            the MATLAB code \r\n            <noscript>(requires JavaScript)<\/noscript><\/span><\/a><br><br>\r\n      Published with MATLAB&reg; 7.4<br><\/p>\r\n<\/div>\r\n<!--\r\na773be6d30f34af8a0ee75c4af586609 ##### SOURCE BEGIN #####\r\n%% Indexing Terminology\r\n% Someone recently asked me to discuss X-Y versus\r\n% row-column indexing.\r\n%% Two Conventions\r\n% When calling functions in MATLAB that create or reference arrays,\r\n% there are two different conventions for specifying the first two\r\n% dimensions of an array:  \r\n%\r\n% * matrix indexing, i.e., rows and columns\r\n% * Cartesian , i.e., X and Y\r\n%\r\n% These different conventions get used depending on whether the array is\r\n% strictly an array or when it has a probable physical interpretation in\r\n% terms of Cartesian grid.\r\n%% Matrix versus Cartesian Frameworks\r\n% In MATLAB, we index into matrices using row and column indices.  That is,\r\n% for matrix\r\nA = magic(3)\r\n%%\r\n% I can reference the last element in the second row like this:\r\nA(2,3)\r\n%%\r\n% or\r\nA(2,end)\r\n%%\r\n% and to retrieve the first element of columns 1 and 3, I type this:\r\nA(1,[1 3])\r\n%%\r\n% Check out\r\n% <https:\/\/blogs.mathworks.com\/loren\/2006\/08\/09\/essence-of-indexing\/ this post>\r\n% to get a fairly complete picture of indexing in MATLAB.\r\n%% Plotting Matrices\r\n% The options |xy| and |ij| for\r\n% <https:\/\/www.mathworks.com\/help\/matlab\/ref\/axis.html |axis|>\r\n% allow us to control whether to view the data just the way the matrix is\r\n% displayed (the (1,1) element is in the upper left corner):\r\ncolormap(jet(9))\r\nimage(A)\r\ncolorbar\r\n%%\r\n% or we can view the image\/matrix as a top-down view of a physical \"map\"\r\n% with the y-values now increasing along the vertical axis.\r\naxis xy\r\n%%\r\n% In images, the matrices column values plot vertically. In line\r\n% plots, the columns are plotted vs. their indices, making columns plot\r\n% horizontally.\r\nh = plot(A);\r\nlegend(h,{'Column 1','Column 2','Column 3'})\r\n%% Creating an N-Dimensional Grid\r\n%  \r\n% <https:\/\/www.mathworks.com\/help\/matlab\/ref\/ndgrid.html |ndgrid|>\r\n% uses matrix indexing since it is designed to suit working in any number\r\n% of array dimensions.\r\n[ii, jj] = ndgrid(1:3,1:2)\r\n%% Creating an 2- or 3-Dimensional Grid\r\n% On the other hand, \r\n% <https:\/\/www.mathworks.com\/help\/matlab\/ref\/mesh.html |mesh|>,\r\n% interprets its first inputs in a Cartesian framework since it is used\r\n% primarily for working with 2- and 3-dimensional plots, often depicting\r\n% something over an area or volume.\r\n[xx, yy] = meshgrid(1:3,1:2)\r\n%%\r\n% In the *Remarks* section of the documentation for \r\n% <https:\/\/www.mathworks.com\/help\/matlab\/ref\/meshgrid.html |meshgrid|>,\r\n% you will find a discussion of this, especially with respect to inverting\r\n% the first two input arguments compared to |ndgrid|.\r\n% There is related discussion in the *Description* section for the function\r\n% <https:\/\/www.mathworks.com\/help\/matlab\/ref\/mesh.html |mesh|>.\r\n%% \r\n% Functions designed for two or three dimension,\r\n% such as |contour| and |quiver|, _usually_ work in Cartesian coordinates.\r\n% Functions that are designed to work in N-dimensions generally work with a\r\n% matrix indexing framework.  Be sure to check the help for functions you\r\n% use to see which convention applies.\r\n%% Thoughts?\r\n% Any ideas on what we can do, perhaps in examples or documentation (or\r\n% eslewhere?) to help people out with this potential confusion?  If so,\r\n% please post <https:\/\/blogs.mathworks.com\/loren\/?p=95#respond here>.\r\n\r\n\r\n##### SOURCE END ##### a773be6d30f34af8a0ee75c4af586609\r\n-->","protected":false},"excerpt":{"rendered":"<p>\r\n   \r\n      Someone recently asked me to discuss X-Y versus row-column indexing.\r\n   \r\n   Contents\r\n   \r\n      \r\n         Two Conventions\r\n     ... <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/loren\/2007\/06\/21\/indexing-terminology\/\">read more >><\/a><\/p>","protected":false},"author":39,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[14,4],"tags":[],"_links":{"self":[{"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/posts\/95"}],"collection":[{"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/users\/39"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/comments?post=95"}],"version-history":[{"count":0,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/posts\/95\/revisions"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/media?parent=95"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/categories?post=95"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/tags?post=95"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}