From: Sergei Koulayev on
People,

check this out. I always thought I could treat 0-1 resulting from the logical operations as numbers. But apparently not. Why? It is completely unintuitive and dangerous. Can somebody give me a meaning of the following example?

a = rand(100,1);
i = a<0.5;
i = i.^2;
i(i==0) = 2;
unique(i)

ans =

1
2

i = a<0.5;
i(i==0) = 2;
unique(i)

ans =

1
From: Sean on
"Sergei Koulayev" <sergei.koulayev(a)mathworks.com> wrote in message <hrujad$fmn$1(a)fred.mathworks.com>...
> People,
>
> check this out. I always thought I could treat 0-1 resulting from the logical operations as numbers. But apparently not. Why? It is completely unintuitive and dangerous. Can somebody give me a meaning of the following example?
>
> a = rand(100,1);
> i = a<0.5;
> i = i.^2;
> i(i==0) = 2;
> unique(i)
>
> ans =
>
> 1
> 2
>
> i = a<0.5;
> i(i==0) = 2;
> unique(i)
>
> ans =
>
> 1

Try this:
%%%
a = rand(100,1);
i = a<0.5;
i = i.^2;
i(i==0) = 2;
class(i)
unique(i)

%%%
a = rand(100,1);
i = a<0.5;
i(i==0) = 2;
class(i)
unique(i)

%% Explanation
1st case: Squaring the i-vector turns it into a double so that values can be anything.
2nd case: The vector is logical so saying anything other than zero is true and it turns it to 1.
From: Steven Lord on

"Sergei Koulayev" <sergei.koulayev(a)mathworks.com> wrote in message
news:hrujad$fmn$1(a)fred.mathworks.com...
> People,
>
> check this out. I always thought I could treat 0-1 resulting from the
> logical operations as numbers.

Of course.

> But apparently not. Why? It is completely unintuitive and dangerous. Can
> somebody give me a meaning of the following example?
>
> a = rand(100,1);
> i = a<0.5;
> i = i.^2;

The above line converts i from being a _logical_ array into a _double_
array. [You can confirm this by looking at WHOS or by asking for its
CLASS.] When you do so ...

> i(i==0) = 2;

this replaces all 0's in the double array with the double value 2.
Therefore the array has two unique values, as your unique call indicated.

> unique(i)
>
> ans =
>
> 1
> 2
>
> i = a<0.5;
> i(i==0) = 2;

When you perform the above line, you're assigning the double value 2 into a
_logical_ array. Therefore, the double value 2 needs to be converted into a
logical value, and what is it converted into?

>> x = logical(2)

x =

1

The LOGICAL function converts 0 into false, errors if given a NaN or a
complex value, and converts all other values (1, 2, pi, -73, Inf, 1.234567,
etc.) into true. Therefore, when you perform the above operation the
logical array i contains only true values, and so ...

> unique(i)
>
> ans =
>
> 1

there is only one value in your logical array, true. This is the correct
behavior.

--
Steve Lord
slord(a)mathworks.com
comp.soft-sys.matlab (CSSM) FAQ: http://matlabwiki.mathworks.com/MATLAB_FAQ


From: Matt Fig on
Nothing seems out of sorts to me. In the first case you are assigning a double to a double. In the second case you are assigning a double to a logical, so MATLAB preserves the logical. Sort of like this:

A = 3;
A(2) = uint8(9);
A(3) = 'r';
whos A

Now as for the warning in your case, yet none in the above example, I question that.
From: Sergei Koulayev on
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

> When you perform the above line, you're assigning the double value 2 into a
> _logical_ array. Therefore, the double value 2 needs to be converted into a
> logical value, and what is it converted into?
>
> >> x = logical(2)
>
> x =
>
> 1
>
> The LOGICAL function converts 0 into false, errors if given a NaN or a
> complex value, and converts all other values (1, 2, pi, -73, Inf, 1.234567,
> etc.) into true. Therefore, when you perform the above operation the
> logical array i contains only true values, and so ...
>
> > unique(i)
> >
> > ans =
> >
> > 1
>
> there is only one value in your logical array, true. This is the correct
> behavior.
>
> --
> Steve Lord
> slord(a)mathworks.com
> comp.soft-sys.matlab (CSSM) FAQ: http://matlabwiki.mathworks.com/MATLAB_FAQ
>