{"id":536,"date":"2015-11-07T21:48:53","date_gmt":"2015-11-08T02:48:53","guid":{"rendered":"http:\/\/jaewon.hwang.info\/jaewon\/?p=536"},"modified":"2015-11-07T23:03:36","modified_gmt":"2015-11-08T04:03:36","slug":"vectorizing-value-assignment-to-an-array-of-structures","status":"publish","type":"post","link":"https:\/\/jaewon.hwang.info\/jaewon\/2015\/11\/vectorizing-value-assignment-to-an-array-of-structures\/","title":{"rendered":"Vectorizing Value Assignment to an Array of Structures"},"content":{"rendered":"<fieldset>\n<legend><strong>MATLAB code:<\/strong><\/legend>\n<pre class=\"brush: matlab; toolbar: false;\">% create an array of 100 structures\r\nchan = struct('Index', num2cell(1:100));\r\n\r\n% assign new numbers (larger by 3) to the Index field without using a loop\r\n\r\n% Method 1\r\nchan = arrayfun(@(s) setfield(s,'Index', s.Index + 3), chan);\r\n\r\n% Method 2: This is faster than Method 1, but requires a temporary variable.\r\nnew_index = num2cell([chan.Index] + 3);\r\n[chan.Index] = new_index{:};\r\n\r\n% Method 3: Surprisingly using a loop is the fastest!!!\r\nfor m=1:100, chan(m).Index = chan(m).Index + 3; end\r\n\r\n% The fastest time out of 10 runs on my desktop with R2015b\r\n% Method 1: Elapsed time is 0.002056 seconds.\r\n% Method 2: Elapsed time is 0.000357 seconds.\r\n% Method 3: Elapsed time is 0.000124 seconds.<\/pre>\n<\/fieldset>\n","protected":false},"excerpt":{"rendered":"<p>MATLAB code: % create an array of 100 structures chan = struct(&#8216;Index&#8217;, num2cell(1:100)); % assign new numbers (larger by 3) to the Index field without using a loop % Method 1 chan = arrayfun(@(s) setfield(s,&#8217;Index&#8217;, s.Index + 3), chan); % Method 2: This is faster than Method 1, but requires a temporary variable. new_index = [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16],"tags":[77],"class_list":["post-536","post","type-post","status-publish","format-standard","hentry","category-computer","tag-matlab"],"_links":{"self":[{"href":"https:\/\/jaewon.hwang.info\/jaewon\/wp-json\/wp\/v2\/posts\/536","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/jaewon.hwang.info\/jaewon\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/jaewon.hwang.info\/jaewon\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/jaewon.hwang.info\/jaewon\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/jaewon.hwang.info\/jaewon\/wp-json\/wp\/v2\/comments?post=536"}],"version-history":[{"count":0,"href":"https:\/\/jaewon.hwang.info\/jaewon\/wp-json\/wp\/v2\/posts\/536\/revisions"}],"wp:attachment":[{"href":"https:\/\/jaewon.hwang.info\/jaewon\/wp-json\/wp\/v2\/media?parent=536"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/jaewon.hwang.info\/jaewon\/wp-json\/wp\/v2\/categories?post=536"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/jaewon.hwang.info\/jaewon\/wp-json\/wp\/v2\/tags?post=536"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}