From: Matt J on
"Sergei Koulayev" <sergei.koulayev(a)mathworks.com> wrote in message <hrv3lh$44d$1(a)fred.mathworks.com>...

> Matt, what would you say about my last post? Do you agree that there should be at least a warning when someone tries to write double into logical?
==============

No. You seem to think the user should be free to treat logicals as equivalent to double arrays of 0s and 1s, but I don't really see why.

>Can you think of any reasons of why preserving logicalness takes precedent?
==============

It's a universal rule in MATLAB that whenever you write a scalar of type A to an array element of type B, then a conversion from A to B takes place. Deviating from this rule for logicals only would create confusion and doesn't really make sense.

Consider for example

>> a=uint8(1:3)

a =

1 2 3

>> a(1)=pi

a =

3 2 3

You seem to think the result of this should have been

a =

3.1416 2.0000 3.0000

but it seems purely subjective to me as to which casting precedence is the better one.
From: Walter Roberson on
Sergei Koulayev wrote:
> Really, when I write a(a==0) = 2 -
> I expect to have 2's at the place of zeros. I understand logicalness is
> an important thing, but having correct computations that meet user's
> expectations is the need number one.

*My* expectation is that when I make an indexed assignment into a
variable, that the variable will not change type underneath me according
to the type of what was assigned in.

What next
A = 'Hello'; A(3) = 37;
I wrote a number in there, so the character string should become
numeric?? Or only if the number written in is non-integral or negative
or exceeds 65537 and is thus not representable as a character?
(Characters have been 16 bits internally since unicode was fully
integrated.)

A = uint8(250:255); A(2) = -5.5;
Now -5.5 is not representable as uint8, so A should become.. what, int8?
int16? float? double ??

The rule in Matlab is that a variable will change its type to the type
of the assigned value only if the entire variable (unsubscripted) is
named as the destination, such as
A = uint8(250:255); A = -5.5; %A becomes double
but not
A = uint8(250:255); A(:) = -5.5; %A stays uint8 and gets assigned all 0

Everybody knows and expects that if you assign a floating point value to
a seletion of an integral array that the floating point value will be
clamped to the range representable by the integral type and that the
value will be rounded. Why do you expect that a different mechanism be
used for logical??

From: Sergei Koulayev on
"Steven Lord" <slord(a)mathworks.com> wrote in message <hrv49e$fd7$1(a)fred.mathworks.com>...
.....

> I think that's a very consistent rule (and so I estimate about 30 seconds
> after I post, someone will remind me of a case I've forgotten where it
> doesn't hold true. "Subscripting-as-evaluation", like function handles,
> doesn't count, and neither does the case where a class author specifically
> and explicitly overloads subscripted assignment.) I don't want to have the
> special case "unless RHS contains values that don't 'fit' in which case we
> convert everything to the class of RHS." at the end of that rule unless it's
> absolutely necessary, and IMO it's not.
>
> --
> Steve Lord
> slord(a)mathworks.com
> comp.soft-sys.matlab (CSSM) FAQ: http://matlabwiki.mathworks.com/MATLAB_FAQ
>

Steve: I know we are talking different languages here, you are the developer, I'm the user. As a user, I don't mind bumping up from logical to double, or from int8 to double, if that's necessary to give me a result I want. Just imagine that I create a variable X somewhere on the line #12 and it just happened to be stored at Integer. Or Logical. Then later at the line #133 I try to write X(3)=pi. I really want this "pi" to be there! Instead of doing just what I told him to do - just put the number in there - Matlab is *invisibly* correcting my operation, giving me a wrong answer that I didn't expect. Who would be happy about that? I want "what you see is what you get" as a consistent behavior.
From: Walter Roberson on
Sergei Koulayev wrote:
> Do you agree that there
> should be at least a warning when someone tries to write double into
> logical?

Not unless you are also going to start giving a warning every time
someone uses a double (other than 0 or 1) in a logical context such as

if VarName %same as if all(VarName ~= 0)

or

B = ~~A; %same as B = (A ~= 0)


If we are going to have warnings, then let us be consistent and have the
warnings whenever anyone _evaluates_ a double in a logical context and
that double does not happen to be be 0.0 or 1.0.

Wouldn't that be a squawk, the amount of code that would have to be
touched up to avoid triggering "dbstop if warning"...

if logical(VarName) %makes me think of if islogical(VarName)
From: Matt J on
"Sergei Koulayev" <sergei.koulayev(a)mathworks.com> wrote in message <hrv5v5$742$1(a)fred.mathworks.com>...

>Then later at the line #133 I try to write X(3)=pi. I really want this "pi" to be there!
Instead of doing just what I told him to do - just put the number in there - Matlab is *invisibly* correcting my operation, giving me a wrong answer that I didn't expect.
====================

But then why wouldn't you define X (or convert it) to a data type that can hold pi. You say you don't want MATLAB to invisibly alter your choice of data, but you do seem to want it to invisibly alter your choice of data type. Why is one better than the other?