{"id":2003,"date":"2016-08-22T08:53:43","date_gmt":"2016-08-22T13:53:43","guid":{"rendered":"https:\/\/blogs.mathworks.com\/loren\/?p=2003"},"modified":"2016-09-05T09:00:04","modified_gmt":"2016-09-05T14:00:04","slug":"indexing-with-parentheses","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/loren\/2016\/08\/22\/indexing-with-parentheses\/","title":{"rendered":"Indexing with Parentheses"},"content":{"rendered":"<div class=\"content\"><!--introduction--><p>I have talked about indexing a bunch of times <a href=\"https:\/\/blogs.mathworks.com\/loren\/category\/indexing\/?s_tid=Blog_loren_Category\">in the past<\/a>.  Recently I have visited quite a few customers who still get tripped up a bit sometimes.  So I thought I'd try again.<\/p><!--\/introduction--><h3>Contents<\/h3><div><ul><li><a href=\"#5e1842af-c8a7-4176-8cfa-53fe2e901277\">Arrays<\/a><\/li><li><a href=\"#8301781f-182a-4e7a-bee8-ac109a0cdc53\">Where People Trip<\/a><\/li><li><a href=\"#dbce5621-bab3-45bd-82eb-e0c6cf9d8a9b\">Function Calls<\/a><\/li><li><a href=\"#4134b3eb-6b76-48e3-af00-7f20d69d3299\">Numeric Arrays<\/a><\/li><li><a href=\"#3a76f896-1713-454d-907b-016d2d713a94\">Cell Arrays<\/a><\/li><li><a href=\"#b3173d5f-05e6-4bab-a293-2b0b5533ea9a\">Tables<\/a><\/li><li><a href=\"#3f5423cc-928b-40b0-9c20-a94f802bcf9e\">Structures<\/a><\/li><li><a href=\"#d0676052-1974-418b-9800-c3bcce26c25c\">Follow Up<\/a><\/li><\/ul><\/div><h4>Arrays<a name=\"5e1842af-c8a7-4176-8cfa-53fe2e901277\"><\/a><\/h4><p>What are arrays?  In MATLAB, they are containers that hold information. And they are \"regular\".  By that, I mean that they have a uniform layout, equal numbers of elements in each row, each column, each page, etc. as you march along the dimensions.  How do you get information into them, and out from them?<\/p><h4>Where People Trip<a name=\"8301781f-182a-4e7a-bee8-ac109a0cdc53\"><\/a><\/h4><p>One of the common places I see people trip is accessing information from <a href=\"https:\/\/www.mathworks.com\/help\/matlab\/ref\/cell.html\">cell arrays<\/a> (and sometimes similarly for <a href=\"https:\/\/www.mathworks.com\/help\/matlab\/ref\/struct.html\">structures<\/a>).<\/p><h4>Function Calls<a name=\"dbce5621-bab3-45bd-82eb-e0c6cf9d8a9b\"><\/a><\/h4><p>Parentheses mean multiple things in MATLAB.  For example, you enclose the input arguments to a function between parentheses, like this:<\/p><pre class=\"codeinput\">image(magic(7))\r\naxis <span class=\"string\">square<\/span>\r\n<\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/loren\/2016\/indexingPart12016_01.png\" alt=\"\"> <h4>Numeric Arrays<a name=\"4134b3eb-6b76-48e3-af00-7f20d69d3299\"><\/a><\/h4><p>Another use of parentheses is to create a subset of an array. Let's suppose we have a matrix (2-D) and we want to create a new matrix from some particular rows and columns.<\/p><pre class=\"codeinput\">Anumeric = magic(5)\r\nAmine = Anumeric([1 4],[2 5])\r\n<\/pre><pre class=\"codeoutput\">Anumeric =\r\n    17    24     1     8    15\r\n    23     5     7    14    16\r\n     4     6    13    20    22\r\n    10    12    19    21     3\r\n    11    18    25     2     9\r\nAmine =\r\n    24    15\r\n    12     3\r\n<\/pre><p>We now have the 2x2 matrix defined by rows 1 and 4 and columns 2 and 5 from the original matrix <tt>A<\/tt>.<\/p><h4>Cell Arrays<a name=\"3a76f896-1713-454d-907b-016d2d713a94\"><\/a><\/h4><p>Similarly, we create a new cell array from an existing one, but indexing with parentheses for the rows and columns we want.<\/p><pre class=\"codeinput\">Cellofstuff = {Amine, <span class=\"string\">'loren'<\/span>, <span class=\"string\">'shure'<\/span>; 17, 51, 153}\r\nCmine = Cellofstuff([1 2],[1 3])\r\n<\/pre><pre class=\"codeoutput\">Cellofstuff = \r\n    [2x2 double]    'loren'    'shure'\r\n    [        17]    [   51]    [  153]\r\nCmine = \r\n    [2x2 double]    'shure'\r\n    [        17]    [  153]\r\n<\/pre><h4>Tables<a name=\"b3173d5f-05e6-4bab-a293-2b0b5533ea9a\"><\/a><\/h4><p>And tables work similarly.  First I'm going to load in data from a MAT-file, into a <tt>struct<\/tt> rather than into separate variables.<\/p><pre class=\"codeinput\">Structpatient = load(<span class=\"string\">'patients.mat'<\/span>);\r\n<\/pre><p>Next convert it to a <tt>table<\/tt>.<\/p><pre class=\"codeinput\">Tpatient = struct2table(Structpatient);\r\n<\/pre><pre class=\"codeinput\">whos <span class=\"string\">T*<\/span>\r\n<\/pre><pre class=\"codeoutput\">  Name            Size            Bytes  Class    Attributes\r\n\r\n  Tmine           5x6              4641  table              \r\n  Tpatient      100x10            59738  table              \r\n\r\n<\/pre><p>The table <tt>Tpatient<\/tt> is longer than I want to display, but I can easily look at a piece of it.  How?  By indexing with parentheses again to create a smaller table.<\/p><pre class=\"codeinput\">Tmine = Tpatient(1:5, [1 2 5 6 8 end])\r\n<\/pre><pre class=\"codeoutput\">Tmine = \r\n     Gender      LastName     Smoker    Systolic    Height    SelfAssessedHealthStatus\r\n    ________    __________    ______    ________    ______    ________________________\r\n    'Male'      'Smith'       true      124         71        'Excellent'             \r\n    'Male'      'Johnson'     false     109         69        'Fair'                  \r\n    'Female'    'Williams'    false     125         64        'Good'                  \r\n    'Female'    'Jones'       false     117         67        'Fair'                  \r\n    'Female'    'Brown'       false     122         64        'Good'                  \r\n<\/pre><h4>Structures<a name=\"3f5423cc-928b-40b0-9c20-a94f802bcf9e\"><\/a><\/h4><p>I most commonly see user code with scalar structures, by which I mean a structure with fields that contain other MATLAB stuff.  But, since this is MATLAB, structs can also be arrays themselves.  I think about them as cell arrays with one 'extra' dimension of indices being names, the rest being the usual indexing.<\/p><pre class=\"codeinput\">Sdata(1).FirstName = <span class=\"string\">'Loren'<\/span>;\r\nSdata(1).LastName = <span class=\"string\">'Shure'<\/span>;\r\nSdata(1).Height = 150;\r\nSdata(2).FirstName = <span class=\"string\">'Miss'<\/span>;\r\nSdata(2).LastName = <span class=\"string\">'Piggy'<\/span>;\r\nSdata(2).Height = 45;\r\nSdata(3).FirstName = <span class=\"string\">'Big'<\/span>;\r\nSdata(3).LastName = <span class=\"string\">'Bird'<\/span>;\r\nSdata(3).Height = 275;\r\n<\/pre><p>To create a struct with only Sesame Street characters, I use parentheses again.<\/p><pre class=\"codeinput\">Ssesame = Sdata(1,2:3)\r\n<\/pre><pre class=\"codeoutput\">Ssesame = \r\n1x2 struct array with fields:\r\n    FirstName\r\n    LastName\r\n    Height\r\n<\/pre><p>or equivalently<\/p><pre>   Ssesame = Sdata(2:3)<\/pre><p>but NOT<\/p><pre>   Ssesame = Sdata(2:3,1)<\/pre><h4>Follow Up<a name=\"d0676052-1974-418b-9800-c3bcce26c25c\"><\/a><\/h4><p>I plan to follow this up with discussions of other sorts of indexing. Hope you find this helpful.  You can let me know <a href=\"https:\/\/blogs.mathworks.com\/loren\/?p=2003#respond\">here<\/a>.<\/p><script language=\"JavaScript\"> <!-- \r\n    function grabCode_1d605816955c4b5d904c95f72a7d2173() {\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='1d605816955c4b5d904c95f72a7d2173 ' + '##### ' + 'SOURCE BEGIN' + ' #####';\r\n        t2='##### ' + 'SOURCE END' + ' #####' + ' 1d605816955c4b5d904c95f72a7d2173';\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        copyright = 'Copyright 2016 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 copyright line at the bottom if specified.\r\n        if (copyright.length > 0) {\r\n            d.writeln('');\r\n            d.writeln('%%');\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     --> <\/script><p style=\"text-align: right; font-size: xx-small; font-weight:lighter;   font-style: italic; color: gray\"><br><a href=\"javascript:grabCode_1d605816955c4b5d904c95f72a7d2173()\"><span style=\"font-size: x-small;        font-style: italic;\">Get \r\n      the MATLAB code <noscript>(requires JavaScript)<\/noscript><\/span><\/a><br><br>\r\n      Published with MATLAB&reg; R2016a<br><\/p><\/div><!--\r\n1d605816955c4b5d904c95f72a7d2173 ##### SOURCE BEGIN #####\r\n%% Indexing with Parentheses\r\n% I have talked about indexing a bunch of times\r\n% <https:\/\/blogs.mathworks.com\/loren\/category\/indexing\/?s_tid=Blog_loren_Category\r\n% in the past>.  Recently I have visited quite a few customers who still\r\n% get tripped up a bit sometimes.  So I thought I'd try again.  \r\n%\r\n%% Arrays\r\n% What are arrays?  In MATLAB, they are containers that hold information.\r\n% And they are \"regular\".  By that, I mean that they have a uniform layout,\r\n% equal numbers of elements in each row, each column, each page, etc. as\r\n% you march along the dimensions.  How do you get information into them,\r\n% and out from them?\r\n%% Where People Trip\r\n% One of the common places I see people trip is accessing information from\r\n% <https:\/\/www.mathworks.com\/help\/matlab\/ref\/cell.html cell arrays> (and\r\n% sometimes similarly for\r\n% <https:\/\/www.mathworks.com\/help\/matlab\/ref\/struct.html structures>).\r\n%% Function Calls\r\n% Parentheses mean multiple things in MATLAB.  For example, you enclose the\r\n% input arguments to a function between parentheses, like this:\r\nimage(magic(7))\r\naxis square\r\n%% Numeric Arrays\r\n% Another use of parentheses is to create a subset of an array.\r\n% Let's suppose we have a matrix (2-D) and we want to create a new matrix\r\n% from some particular rows and columns.\r\nAnumeric = magic(5)\r\nAmine = Anumeric([1 4],[2 5])\r\n%%\r\n% We now have the 2x2 matrix defined by rows 1 and 4 and columns 2 and 5\r\n% from the original matrix |A|.\r\n%% Cell Arrays\r\n% Similarly, we create a new cell array from an existing one, but indexing\r\n% with parentheses for the rows and columns we want.\r\nCellofstuff = {Amine, 'loren', 'shure'; 17, 51, 153}\r\nCmine = Cellofstuff([1 2],[1 3])\r\n%% Tables\r\n% And tables work similarly.  First I'm going to load in data from a\r\n% MAT-file, into a |struct| rather than into separate variables.\r\nStructpatient = load('patients.mat');\r\n%%\r\n% Next convert it to a |table|.\r\nTpatient = struct2table(Structpatient);\r\n%%\r\nwhos T*\r\n%%\r\n% The table |Tpatient| is longer than I want to display, but I can easily\r\n% look at a piece of it.  How?  By indexing with parentheses again to\r\n% create a smaller table.\r\nTmine = Tpatient(1:5, [1 2 5 6 8 end])\r\n%% Structures\r\n% I most commonly see user code with scalar structures, by which I mean a\r\n% structure with fields that contain other MATLAB stuff.  But, since this\r\n% is MATLAB, structs can also be arrays themselves.  I think about them as\r\n% cell arrays with one 'extra' dimension of indices being names, the rest\r\n% being the usual indexing.\r\nSdata(1).FirstName = 'Loren';\r\nSdata(1).LastName = 'Shure';\r\nSdata(1).Height = 150;\r\nSdata(2).FirstName = 'Miss';\r\nSdata(2).LastName = 'Piggy';\r\nSdata(2).Height = 45;\r\nSdata(3).FirstName = 'Big';\r\nSdata(3).LastName = 'Bird';\r\nSdata(3).Height = 275;\r\n%%\r\n% To create a struct with only Sesame Street characters, I use parentheses\r\n% again.\r\nSsesame = Sdata(1,2:3)\r\n%%\r\n% or equivalently\r\n%\r\n%     Ssesame = Sdata(2:3)\r\n%\r\n% but NOT\r\n%\r\n%     Ssesame = Sdata(2:3,1)\r\n%\r\n%% Follow Up\r\n% I plan to follow this up with discussions of other sorts of indexing.\r\n% Hope you find this helpful.  You can let me know\r\n% <https:\/\/blogs.mathworks.com\/loren\/?p=2003#respond here>.\r\n##### SOURCE END ##### 1d605816955c4b5d904c95f72a7d2173\r\n-->","protected":false},"excerpt":{"rendered":"<div class=\"overview-image\"><img decoding=\"async\"  class=\"img-responsive\" src=\"https:\/\/blogs.mathworks.com\/images\/loren\/2016\/indexingPart12016_01.png\" onError=\"this.style.display ='none';\" \/><\/div><!--introduction--><p>I have talked about indexing a bunch of times <a href=\"https:\/\/blogs.mathworks.com\/loren\/category\/indexing\/?s_tid=Blog_loren_Category\">in the past<\/a>.  Recently I have visited quite a few customers who still get tripped up a bit sometimes.  So I thought I'd try again.... <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/loren\/2016\/08\/22\/indexing-with-parentheses\/\">read more >><\/a><\/p>","protected":false},"author":39,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[4],"tags":[],"_links":{"self":[{"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/posts\/2003"}],"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=2003"}],"version-history":[{"count":2,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/posts\/2003\/revisions"}],"predecessor-version":[{"id":2005,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/posts\/2003\/revisions\/2005"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/media?parent=2003"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/categories?post=2003"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/tags?post=2003"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}