{"id":93,"date":"2007-06-05T17:48:22","date_gmt":"2007-06-05T22:48:22","guid":{"rendered":"https:\/\/blogs.mathworks.com\/loren\/2007\/06\/05\/indexed-assignment\/"},"modified":"2016-08-03T14:40:22","modified_gmt":"2016-08-03T19:40:22","slug":"indexed-assignment","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/loren\/2007\/06\/05\/indexed-assignment\/","title":{"rendered":"Indexed Assignment"},"content":{"rendered":"<div xmlns:mwsh=\"https:\/\/www.mathworks.com\/namespace\/mcode\/v1\/syntaxhighlight.dtd\" class=\"content\">\r\n   <introduction>\r\n      <p>Recently some folks at The MathWorks wondered about the difference of behavior of variable creation versus assigning values\r\n         into an existing array.  I thought I'd address this topic live from The MathWorks Aerospace and Defense Conference '07.\r\n      <\/p>\r\n   <\/introduction>\r\n   <h3>Contents<\/h3>\r\n   <div>\r\n      <ul>\r\n         <li><a href=\"#3\">The Question<\/a><\/li>\r\n         <li><a href=\"#4\">An Example<\/a><\/li>\r\n         <li><a href=\"#7\">The Answer<\/a><\/li>\r\n         <li><a href=\"#11\">Arithmetic<\/a><\/li>\r\n         <li><a href=\"#12\">Thoughts?<\/a><\/li>\r\n      <\/ul>\r\n   <\/div>\r\n   <p>Here's a picture of the audience.<\/p>\r\n   <p><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/loren\/93\/audience.jpg\"> <\/p>\r\n   <h3>The Question<a name=\"3\"><\/a><\/h3>\r\n   <p>Why doesn&#8217;t changing an element of a logical array change its logicalness? Most arithmetic operations remove the logicalness\r\n      from an array; it would seem to me that changing an element of the array (at least to something other than 0 or 1) should\r\n      either throw an error or similarly modify its logicalness.\r\n   <\/p>\r\n   <h3>An Example<a name=\"4\"><\/a><\/h3>\r\n   <p>Let's show an example.  First create a logical array.<\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">clear <span style=\"color: #A020F0\">all<\/span>\r\nA = magic(3);\r\nB = A &gt; 6;\r\nwhos<\/pre><pre style=\"font-style:oblique\">  Name      Size            Bytes  Class      Attributes\r\n\r\n  A         3x3                72  double               \r\n  B         3x3                 9  logical              \r\n\r\n<\/pre><p>Now let's do some arithmetic with logical arrays mixed with doubles.<\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">C = B + 1;\r\nD = B;\r\nD(1:5) = 2\r\nwhos <span style=\"color: #A020F0\">A<\/span> <span style=\"color: #A020F0\">B<\/span> <span style=\"color: #A020F0\">C<\/span> <span style=\"color: #A020F0\">D<\/span><\/pre><pre style=\"font-style:oblique\">D =\r\n     1     1     0\r\n     1     1     1\r\n     1     1     0\r\n  Name      Size            Bytes  Class      Attributes\r\n\r\n  A         3x3                72  double               \r\n  B         3x3                 9  logical              \r\n  C         3x3                72  double               \r\n  D         3x3                 9  logical              \r\n\r\n<\/pre><p>What you see is that <tt>A<\/tt> and <tt>C<\/tt> are double arrays, while <tt>B<\/tt> and <tt>D<\/tt> are logical.\r\n   <\/p>\r\n   <h3>The Answer<a name=\"7\"><\/a><\/h3>\r\n   <p>Indexed assignment, that is, <tt>D(1:5) = 2<\/tt> has always preserved the type of the array being indexed.  Only values may change, not the type. You'd only get an error\r\n      if there was not a valid conversion routine from the type on the right-hand side to the type stored on the left.\r\n   <\/p>\r\n   <p>If you simply concatenate values, you also find that the output is logical:<\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">E = [D ; B]<\/pre><pre style=\"font-style:oblique\">E =\r\n     1     1     0\r\n     1     1     1\r\n     1     1     0\r\n     1     0     0\r\n     0     0     1\r\n     0     1     0\r\n<\/pre><p>To see this with another datatype, let's look at string array.<\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">firstname = <span style=\"color: #A020F0\">'Loren'<\/span>\r\nlastname = [83   104   117   114   101]\r\nname = firstname\r\nname(end+2:end+1+length(lastname)) = lastname<\/pre><pre style=\"font-style:oblique\">firstname =\r\nLoren\r\nlastname =\r\n    83   104   117   114   101\r\nname =\r\nLoren\r\nname =\r\nLoren Shure\r\n<\/pre><p>If I just use straight concatenation, here's what happens<\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">newname = [firstname, <span style=\"color: #A020F0\">' '<\/span>, lastname]<\/pre><pre style=\"font-style:oblique\">newname =\r\nLoren Shure\r\n<\/pre><h3>Arithmetic<a name=\"11\"><\/a><\/h3>\r\n   <p>If I perform arithmetic between mixed types, where one is numeric and one is not, I instead get a numeric answer.<\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">name - 32<\/pre><pre style=\"font-style:oblique\">ans =\r\n  Columns 1 through 8\r\n    44    79    82    69    78   -32    51    72\r\n  Columns 9 through 11\r\n    85    82    69\r\n<\/pre><h3>Thoughts?<a name=\"12\"><\/a><\/h3>\r\n   <p>The fact that you get results with different datatypes depends on whether you are doing a numerical calculation or assigning\r\n      to an array.  Has the behavior been what you need or have you found it confusing?  Let me know <a href=\"https:\/\/blogs.mathworks.com\/loren\/?p=93#respond\">here<\/a>.\r\n   <\/p><script language=\"JavaScript\">\r\n<!--\r\n\r\n    function grabCode_a436a4781a954655bea9a2a4b5e21069() {\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='a436a4781a954655bea9a2a4b5e21069 ' + '##### ' + 'SOURCE BEGIN' + ' #####';\r\n        t2='##### ' + 'SOURCE END' + ' #####' + ' a436a4781a954655bea9a2a4b5e21069';\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_a436a4781a954655bea9a2a4b5e21069()\"><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\na436a4781a954655bea9a2a4b5e21069 ##### SOURCE BEGIN #####\r\n%% Indexed Assignment\r\n% Recently some folks at The MathWorks wondered about the difference of\r\n% behavior of variable creation versus assigning values into an existing\r\n% array.  I thought I'd address this topic live from \r\n% <https:\/\/www.mathworks.com\/industries\/aerospace\/aerodef_conf07\/?s_cid=HP_E_C_AERO The MathWorks Aerospace and Defense Conference '07>.\r\n%%\r\n% Here's a picture of the audience.\r\n%%\r\n% <<audience.jpg>>\r\n%% The Question\r\n% Why doesn\u00e2\u20ac\u2122t changing an element of a logical array change its\r\n% logicalness? Most arithmetic operations remove the logicalness from an\r\n% array; it would seem to me that changing an element of the array (at\r\n% least to something other than 0 or 1) should either throw an error or\r\n% similarly modify its logicalness.\r\n%% An Example\r\n% Let's show an example.  First create a logical array.\r\nclear all\r\nA = magic(3);\r\nB = A > 6;\r\nwhos\r\n%%\r\n% Now let's do some arithmetic with logical arrays mixed with doubles.\r\nC = B + 1;\r\nD = B;\r\nD(1:5) = 2\r\nwhos A B C D\r\n%%\r\n% What you see is that |A| and |C| are double arrays, while |B| and |D| are logical.\r\n%% The Answer\r\n% Indexed assignment, that is, |D(1:5) = 2| has always preserved the type\r\n% of the array being indexed.  Only values may change, not the type. You'd\r\n% only get an error if there was not a valid conversion routine from the\r\n% type on the right-hand side to the type stored on the left.\r\n%% \r\n% If you simply concatenate values, you also find that the output is\r\n% logical:\r\nE = [D ; B]\r\n%%\r\n% To see this with another datatype, let's look at string array.\r\nfirstname = 'Loren'\r\nlastname = [83   104   117   114   101]\r\nname = firstname\r\nname(end+2:end+1+length(lastname)) = lastname\r\n%% \r\n% If I just use straight concatenation, here's what happens\r\nnewname = [firstname, ' ', lastname]\r\n%% Arithmetic\r\n% If I perform arithmetic between mixed types, where one is numeric and one\r\n% is not, I instead get a numeric answer.\r\nname - 32\r\n%% Thoughts?\r\n% The fact that you get results with different datatypes depends on whether\r\n% you are doing a numerical calculation or assigning to an array.  Has the\r\n% behavior been what you need or have you found it confusing?  Let me know\r\n% <https:\/\/blogs.mathworks.com\/loren\/?p=93#respond here>.\r\n\r\n\r\n##### SOURCE END ##### a436a4781a954655bea9a2a4b5e21069\r\n-->","protected":false},"excerpt":{"rendered":"<p>\r\n   \r\n      Recently some folks at The MathWorks wondered about the difference of behavior of variable creation versus assigning values\r\n         into an existing array.  I thought I'd address this... <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/loren\/2007\/06\/05\/indexed-assignment\/\">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,18],"tags":[],"_links":{"self":[{"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/posts\/93"}],"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=93"}],"version-history":[{"count":1,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/posts\/93\/revisions"}],"predecessor-version":[{"id":1917,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/posts\/93\/revisions\/1917"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/media?parent=93"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/categories?post=93"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/tags?post=93"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}