tashuhka
tashuhka

Reputation: 5126

Difference between boolean and logical

Out of curiosity, if I type these lines in MATLAB:

a = logical([12 0 1.2]);
b = boolean([12 0 1.2]);

The output variables a and b are the same (same value and type). Hence, is there any difference between boolean and logical?

Upvotes: 14

Views: 8030

Answers (4)

chappjc
chappjc

Reputation: 30589

Just to be very clear: There is no such thing as a boolean data type in MATLAB (at least not anymore).

Explicitly demonstrating what the OP stated:

>> a = logical([12 0 1.2]);
>> b = boolean([12 0 1.2]);
>> whos a b
  Name      Size            Bytes  Class      Attributes

  a         1x3                 3  logical              
  b         1x3                 3  logical   

boolean is not a recognized type:

>> cast(a,'boolean')
Error using cast
Unsupported class for conversion. 

As other answers have said, the boolean function comes with Simulink and is little more than an alias for a logical cast.

Upvotes: 4

Etienne
Etienne

Reputation: 1012

A quick look at the boolean function can give you a good answer to your question:

If you type : edit boolean in the matlab console you get:

function y = boolean(x)
%BOOLEAN Creates a boolean vector.
%   This function is typically used in Simulink parameter dialogs, such as
%   the Constant block dialog.  This function generates a logical vector,
%   which is treated as a boolean value in Simulink.  Now that logical is a
%   MATLAB type, this function is essentially just an alias.
%
%   Y = BOOLEAN(X) Converts the vector X into a boolean vector.
%
%   Example: 
%      boolean([0 1 1]) returns [0 1 1]
%
%   See also LOGICAL.

%   Copyright 1990-2012 The MathWorks, Inc.

narginchk(1,1);

if ~isreal(x)
    DAStudio.error('Simulink:utility:BooleanCannotBeComplex');
end

y = logical(x);

If you look at the last line of this function, you can see that the boolean function call the logical function.

Upvotes: 14

Ragav Venkatesan
Ragav Venkatesan

Reputation: 186

Boolean is just an alias for logical now since logical is also a MATLAB type. Boolean is still used in Simulink. Infact, boolean in itself just simply calls the function logical.

Upvotes: 1

nkjt
nkjt

Reputation: 7817

logical is a MATLAB built-in, boolean is a Simulink function.

Part of the return from typing help boolean:

This function is typically used in Simulink parameter dialogs, such as the Constant block dialog. This function generates a logical vector, which is treated as a boolean value in Simulink. Now that logical is a MATLAB type, this function is essentially just an alias.

If you type edit boolean at the command line, you will see that it basically just calls logical on the input.

Upvotes: 9

Related Questions