From: Sean on
"Sergei Koulayev" <sergei.koulayev(a)mathworks.com> wrote in message <hruo0e$5f8$1(a)fred.mathworks.com>...
> Hi Steve,
>
> thanks for the reply. I don't know about others, but I always cared about logical arrays as simply arrays that contain just 0 and 1's. It is quite disturbing that logical arrays can behave differently from arrays consisting of 0 and 1's (double). You explanation is about how things work in Matlab, and its good to know. But why Matlab should be so obsessed with preserving "logicalness" of the array - that I don't understand. Given than most users probably treat those arrays just like I do.
>
> Really, what's this thing about "logicalness"? Its clearly not needed for the indexing purposes. For example, when I write X(ix), why should I care of ix is double or logical, as long as it contains 0 and 1's?
>

Let's say it's a double:
A = [4 5 6; 6 7 2];
idx = [1 1 0 0 1 1]; %double
A(idx) is now going to try and reference A(0) which in MATLAB is undefined. It would also be referencing A(1) multiple times. This would be pretty useless but it's very important for other indexing. Such as:
idx = [1 3 5];
A(idx);
ans = 4 5 6

I doubt anyone with much MATLAB experience has an issue with this functionality.
From: Mark Shore on
"Sergei Koulayev" <sergei.koulayev(a)mathworks.com> wrote in message <hruo0e$5f8$1(a)fred.mathworks.com>...
>
> Really, what's this thing about "logicalness"? Its clearly not needed for the indexing purposes. For example, when I write X(ix), why should I care of ix is double or logical, as long as it contains 0 and 1's?
>
> Sergei
>

Well, storage of only 1 byte per element rather than 8 bytes for one thing, and no way for floating point errors to mess things up for another.
From: Roger Stafford on
"Sergei Koulayev" <sergei.koulayev(a)mathworks.com> wrote in message <hruo0e$5f8$1(a)fred.mathworks.com>...
> Hi Steve,
>
> thanks for the reply. I don't know about others, but I always cared about logical arrays as simply arrays that contain just 0 and 1's. It is quite disturbing that logical arrays can behave differently from arrays consisting of 0 and 1's (double). You explanation is about how things work in Matlab, and its good to know. But why Matlab should be so obsessed with preserving "logicalness" of the array - that I don't understand. Given than most users probably treat those arrays just like I do.
>
> Really, what's this thing about "logicalness"? Its clearly not needed for the indexing purposes. For example, when I write X(ix), why should I care of ix is double or logical, as long as it contains 0 and 1's?
>
> Sergei

Sergei, I'll give you an example of why the distinction needs to be made between logical variables and numbers that are 1's and 0's. Suppose that x is equal to the vector [5,7]. Then x([true,true]) would need to be [5,7] according to the rules of logical indexing, while x([1,1]) of necessity has to be equal to [5,5] according to the rules of numerical indexing.

For this reason Mathworks had no choice but to eventually make this distinction between the two classes, logical and numerical. In the early version of matlab (4b) which I have, the distinction was *not* made and it led to much trouble for me in avoiding this pitfall.

Roger Stafford
From: Alan B on
"Roger Stafford" <ellieandrogerxyzzy(a)mindspring.com.invalid> wrote in message <hruq21$nef$1(a)fred.mathworks.com>...
> "Sergei Koulayev" <sergei.koulayev(a)mathworks.com> wrote in message <hruo0e$5f8$1(a)fred.mathworks.com>...
> > Hi Steve,
> >
> > thanks for the reply. I don't know about others, but I always cared about logical arrays as simply arrays that contain just 0 and 1's. It is quite disturbing that logical arrays can behave differently from arrays consisting of 0 and 1's (double). You explanation is about how things work in Matlab, and its good to know. But why Matlab should be so obsessed with preserving "logicalness" of the array - that I don't understand. Given than most users probably treat those arrays just like I do.
> >
> > Really, what's this thing about "logicalness"? Its clearly not needed for the indexing purposes. For example, when I write X(ix), why should I care of ix is double or logical, as long as it contains 0 and 1's?
> >
> > Sergei
>
> Sergei, I'll give you an example of why the distinction needs to be made between logical variables and numbers that are 1's and 0's. Suppose that x is equal to the vector [5,7]. Then x([true,true]) would need to be [5,7] according to the rules of logical indexing, while x([1,1]) of necessity has to be equal to [5,5] according to the rules of numerical indexing.
>
> For this reason Mathworks had no choice but to eventually make this distinction between the two classes, logical and numerical. In the early version of matlab (4b) which I have, the distinction was *not* made and it led to much trouble for me in avoiding this pitfall.
>
> Roger Stafford

Can you give an explanation for why Matlab shouldn't assume that a double array containing only 0s and 1s is intended as a logical index? It would encourage bad style, but as far as I can see it would be totally unambiguous.
From: Matt J on

> Can you give an explanation for why Matlab shouldn't assume that a double array containing only 0s and 1s is intended as a logical index? It would encourage bad style, but as far as I can see it would be totally unambiguous.
===================

To see that it would be ambiguous, consider the following example and the different behavior that each gives

>> a=10:10:50 %data

a =

10 20 30 40 50

>> a([1 1 1 1]) %treats the 1s as doubles

ans =

10 10 10 10


>> a(logical([1 1 1 1])) %treats the 1s as logicals

ans =

10 20 30 40