20060315-Quick tip: Use the functions true and false

来源:互联网 发布:mac下python开发工具 编辑:程序博客网 时间:2024/05/19 16:35

原文:http://blogs.mathworks.com/steve/2006/03/15/quick-tip-use-the-functions-true-and-false/

I saw some code recently that looked something like this:

B = zeros(size(A));B = logical(B);

The intent of the code was to initialize an all-zero binary image with the same size asA. Note, however, that the output of the zeros function in the first line is a double-precision array, which requireseight bytes per element. Logical arrays in MATLAB, on the other hand, useonly one byte per element. It's quicker and more memory efficient to initialize B using thefalse function.

The functions true and false 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:

B = false(size(A));
0 0