{"id":48,"date":"2006-03-15T21:51:04","date_gmt":"2006-03-16T02:51:04","guid":{"rendered":"https:\/\/blogs.mathworks.com\/steve\/?p=48"},"modified":"2019-10-22T09:10:39","modified_gmt":"2019-10-22T13:10:39","slug":"quick-tip-use-the-functions-true-and-false","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/steve\/2006\/03\/15\/quick-tip-use-the-functions-true-and-false\/","title":{"rendered":"Quick tip: Use the functions <tt>true<\/tt> and <tt>false<\/tt>"},"content":{"rendered":"<p>\r\nAll I've been doing this week is writing. Unfortunately, I haven't been writing blog postings. Between my other work responsibilities and an upcoming trip, I have almost no time left for this blog until sometime next week.\r\n<\/p>\r\n\r\n<p>\r\nJust to keep it going, though, here's a quick tip. I saw some code recently that looked something like this:\r\n<\/p>\r\n\r\n<pre class=\"code\">\r\nB = zeros(size(A));\r\nB = logical(B);\r\n<\/pre>\r\n\r\n<p>\r\nThe intent of the code was to initialize an all-zero binary image with the same size as <tt>A<\/tt>. Note, however, that the output of the <tt>zeros<\/tt> function in the first line is a double-precision array, which requires eight bytes per element. Logical arrays in MATLAB, on the other hand, use only one byte per element. It's quicker and more memory efficient to initialize B using the <tt>false<\/tt> function.\r\n<\/p>\r\n\r\n<p>\r\nThe functions <tt>true<\/tt> and <tt>false<\/tt> are most often seen without input arguments, in which case they return a scalar logical 1 or a scalar logical 0, respectively. But they also support an optional size input. So the following code works just as well for initializing the binary image:\r\n<\/p>\r\n\r\n<pre class=\"code\">\r\nB = false(size(A));\r\n<\/pre>\r\n\r\n<p>\r\n<em>Note<\/em>: The <tt>true<\/tt> and <tt>false<\/tt> functions were introduced in MATLAB 6.5.\r\n<\/p>","protected":false},"excerpt":{"rendered":"<p>\r\nAll I've been doing this week is writing. Unfortunately, I haven't been writing blog postings. Between my other work responsibilities and an upcoming trip, I have almost no time left for this blog... <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/steve\/2006\/03\/15\/quick-tip-use-the-functions-true-and-false\/\">read more >><\/a><\/p>","protected":false},"author":42,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[1],"tags":[102,100],"_links":{"self":[{"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/posts\/48"}],"collection":[{"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/users\/42"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/comments?post=48"}],"version-history":[{"count":1,"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/posts\/48\/revisions"}],"predecessor-version":[{"id":3494,"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/posts\/48\/revisions\/3494"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/media?parent=48"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/categories?post=48"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/tags?post=48"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}