Error: type non-boolean (BitArray(1)) used in boolean context

1,381 views
Skip to first unread message

Isaac

unread,
Apr 23, 2014, 4:16:52 PM4/23/14
to julia...@googlegroups.com

Hi All,

 I am a new Julia-user and meet a problem when I transfer the Matlab code to Julia. I always get the error:type non-boolean (BitArray(1)) used in boolean context. Can anyone help me check the code and solve this problem? The codes have been attached.
I am using the julia-0.3.0-win64 binary release.
Any suggestions and comments would be highly appreciated.

Cheers,
Isaac


sample.jl
main (2).jl

Matt Bauman

unread,
Apr 23, 2014, 4:55:19 PM4/23/14
to julia...@googlegroups.com
In general, Julia is much more picky about what types of things can be used in if statements and && or || conditions than Matlab is.  They must be Bools in Julia, whereas Matlab tries to convert things to a scalar logical value.

Crazily enough, one of the things that Matlab converts to a scalar logical value (albeit not in && or || expressions) is a logical mask.  In Julia, you must explicitly convert the mask (a BitArray) to a Bool by calling `all` (Matlab's behavior) or `any`.  Check main, line 49.  See the documentation for more details: http://docs.julialang.org/en/latest/manual/control-flow/?highlight=bool#man-conditional-evaluation

Also note that braces don't denote scope, but rather create Any-typed arrays.  You don't want to be using them within your if blocks.

Stefan Karpinski

unread,
Apr 23, 2014, 4:54:47 PM4/23/14
to Julia Users
In Matlab, arrays of booleans can be used in conditionals and are, I believe, considered true if all the values in them are true and false otherwise. In Julia only actual boolean values (true or false) can be used in conditionals. You're using a 1-d boolean array somewhere in a conditional. Not sure where – I took a quick look, but nothing popped out at me.

Isaac

unread,
Apr 24, 2014, 5:52:04 AM4/24/14
to julia...@googlegroups.com
Thanks Matt and Stefan,
 
 Your comments are very helpful.
 I have solved the problem because I use a number to compare an array in line 49. The rand(1) in julia always produces an array rather than a number in Matlab. The types of variables should be used very carefully in Julia.

Isaac

Chris Foster

unread,
Apr 24, 2014, 7:26:42 AM4/24/14
to julia...@googlegroups.com
On Thu, Apr 24, 2014 at 7:52 PM, Isaac <dux...@gmail.com> wrote:
> The rand(1) in julia always produces an array rather than a number
> in Matlab.

In matlab a scalar is the same as a 1x1 matrix. Luckily julia doesn't
insist on any such madness... try rand() instead :-)

Isaac

unread,
Apr 24, 2014, 7:45:55 AM4/24/14
to julia...@googlegroups.com

Hi Chris,

 Thanks for your suggestion. rand() works well.
Reply all
Reply to author
Forward
0 new messages